some cheat animation
This commit is contained in:
		
							parent
							
								
									2e05fd3ed3
								
							
						
					
					
						commit
						af8671bad0
					
				
							
								
								
									
										51
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -198,36 +198,38 @@ fn display_grid( | ||||
|     w: &world::World, | ||||
|     robot_pool: &Vec<robot::Robot>, | ||||
|     h: &HashMap<robot::Position, u32>, | ||||
| ) { | ||||
|     for i in (0..w.y).rev() { | ||||
| ) -> String { | ||||
|     let mut grid = String::new(); | ||||
|     for i in (0..w.y + 1).rev() { | ||||
|         if i < 10 { | ||||
|             print!("{}  ", i); | ||||
|             grid = format!("{}{}  ", grid, i); | ||||
|         } else { | ||||
|             print!("{} ", i); | ||||
|             grid = format!("{}{} ", grid, i); | ||||
|         } | ||||
|         for j in 0..w.x { | ||||
|         for j in 0..w.x + 1 { | ||||
|             match h.get(&robot::Position { x: j, y: i }) { | ||||
|                 Some(id) => match robot_pool[(id - 1) as usize].o { | ||||
|                     robot::Orientation::N => print!("↑  "), | ||||
|                     robot::Orientation::E => print!("→  "), | ||||
|                     robot::Orientation::S => print!("↓  "), | ||||
|                     robot::Orientation::W => print!("←  "), | ||||
|                     robot::Orientation::N => grid = format!("{}↑  ", grid), | ||||
|                     robot::Orientation::E => grid = format!("{}→  ", grid), | ||||
|                     robot::Orientation::S => grid = format!("{}↓  ", grid), | ||||
|                     robot::Orientation::W => grid = format!("{}←  ", grid), | ||||
|                 }, | ||||
| 
 | ||||
|                 None => print!(".  "), | ||||
|                 None => grid = format!("{}.  ", grid), | ||||
|             } | ||||
|         } | ||||
|         println!(); | ||||
|         grid = format!("{}\n", grid); | ||||
|     } | ||||
|     print!("   "); | ||||
|     for j in 0..w.x { | ||||
|     grid = format!("{}   ", grid); | ||||
|     for j in 0..w.x + 1 { | ||||
|         if j < 10 { | ||||
|             print!("{}  ", j); | ||||
|             grid = format!("{}{}  ", grid, j); | ||||
|         } else { | ||||
|             print!("{} ", j); | ||||
|             grid = format!("{}{} ", grid, j); | ||||
|         } | ||||
|     } | ||||
|     println!(); | ||||
|     grid = format!("{}\n", grid); | ||||
|     grid | ||||
| } | ||||
| 
 | ||||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||||
| @ -262,11 +264,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||||
| 
 | ||||
|     let mut hash: HashMap<robot::Position, u32> = HashMap::new(); | ||||
|     create_hash_map(&robot_pool, &mut hash); | ||||
|     println!("World {{ x_max = {}; y_max = {} }}", world.x, world.y); | ||||
|     robot::print_robots(&robot_pool); | ||||
|     println!("Initial state"); | ||||
|     println!("=============="); | ||||
|     display_grid(&world, &robot_pool, &hash); | ||||
|     let mut print_all = format!("World {{ x_max = {}; y_max = {} }}\n", world.x, world.y); | ||||
|     print_all = format!("{}{}", print_all, robot::print_robots(&robot_pool)); | ||||
|     print_all = format!("{}Initial state\n", print_all); | ||||
|     print_all = format!("{}==============\n", print_all); | ||||
|     print_all = format!("{}{}", print_all, display_grid(&world, &robot_pool, &hash)); | ||||
|     loop { | ||||
|         let mut piouff: u32 = 0; | ||||
|         for r in &mut robot_pool { | ||||
| @ -280,13 +282,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | ||||
|                 hash.insert(robot::Position { x: r.p.x, y: r.p.y }, r.id); | ||||
|             } | ||||
|         } | ||||
|         std::thread::sleep(std::time::Duration::from_millis(600)); | ||||
|         print!("\x1B[2J\x1B[1;1H"); | ||||
|         print!("{}", display_grid(&world, &robot_pool, &hash)); | ||||
|         if piouff == robot_pool.len() as u32 { | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
|     print!("\x1B[2J\x1B[1;1H"); | ||||
|     print!("{}", print_all); | ||||
|     println!("Final state"); | ||||
|     println!("============"); | ||||
|     display_grid(&world, &robot_pool, &hash); | ||||
|     print!("{}", display_grid(&world, &robot_pool, &hash)); | ||||
|     for r in &robot_pool { | ||||
|         println!("Robot id: {}: Final position: ({}, {})", r.id, r.p.x, r.p.y); | ||||
|     } | ||||
|  | ||||
							
								
								
									
										12
									
								
								src/robot.rs
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/robot.rs
									
									
									
									
									
								
							| @ -95,11 +95,12 @@ pub fn is_piouff(r: &Robot) -> bool { | ||||
| } | ||||
| 
 | ||||
| /// Print robots id, position and instructions.
 | ||||
| pub fn print_robots(robot_pool: &Vec<Robot>) { | ||||
|     println!("Robots ["); | ||||
| pub fn print_robots(robot_pool: &Vec<Robot>) -> String { | ||||
|     let mut res = format!("Robots [\n"); | ||||
|     for r in robot_pool { | ||||
|         println!( | ||||
|             "{{ id = {}, x = {}; y = {}; orientation: {}, instructions: {:?}, }},", | ||||
|         res = format!( | ||||
|             "{}{{ id = {}, x = {}; y = {}; orientation: {}, instructions: {:?}, }},\n", | ||||
|             res, | ||||
|             r.id, | ||||
|             r.p.x, | ||||
|             r.p.y, | ||||
| @ -112,7 +113,8 @@ pub fn print_robots(robot_pool: &Vec<Robot>) { | ||||
|             r.i | ||||
|         ); | ||||
|     } | ||||
|     println!("]"); | ||||
|     res = format!("{}]\n", res); | ||||
|     res | ||||
| } | ||||
| 
 | ||||
| #[cfg(test)] | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Volodymyr Patuta
						Volodymyr Patuta