Merge branch 'main-loop' into 'master'
Main loop See merge request mhart/DancingDroids!43
This commit is contained in:
		
						commit
						ca1303a8eb
					
				
							
								
								
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -218,6 +218,27 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||||
|     let mut hash: HashMap<robot::Position, u32> = HashMap::new(); |     let mut hash: HashMap<robot::Position, u32> = HashMap::new(); | ||||||
|     create_hash_map(&robot_pool, &mut hash); |     create_hash_map(&robot_pool, &mut hash); | ||||||
| 
 | 
 | ||||||
|  |     loop { | ||||||
|  |         let mut piouff: u32 = 0; | ||||||
|  |         for r in &mut robot_pool { | ||||||
|  |             if robot::is_piouff(&r) { | ||||||
|  |                 piouff += 1; | ||||||
|  |             } else { | ||||||
|  |                 hash.remove(&r.p); | ||||||
|  |                 r.execute_instruction(); | ||||||
|  |                 check_map(&r, &world)?; | ||||||
|  |                 check_collisions(&r, &hash)?; | ||||||
|  |                 hash.insert(robot::Position { x: r.p.x, y: r.p.y }, r.id); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         if piouff == robot_pool.len() as u32 { | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     for r in &robot_pool { | ||||||
|  |         println!("Robot id: {}: Final position: ({}, {})", r.id, r.p.x, r.p.y); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     Ok(()) |     Ok(()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										23
									
								
								src/robot.rs
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								src/robot.rs
									
									
									
									
									
								
							| @ -69,6 +69,14 @@ pub fn is_instructions(v: &Vec<char>) -> bool { | |||||||
|     true |     true | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /// Check if a robot is piouff.
 | ||||||
|  | pub fn is_piouff(r: &Robot) -> bool { | ||||||
|  |     if r.i.len() == 0 { | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  |     false | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| mod tests { | mod tests { | ||||||
|     use super::*; |     use super::*; | ||||||
| @ -121,4 +129,19 @@ mod tests { | |||||||
|         let v = vec!['F', 'R', 'L', 'Z']; |         let v = vec!['F', 'R', 'L', 'Z']; | ||||||
|         assert!(is_instructions(&v)); |         assert!(is_instructions(&v)); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     #[test] | ||||||
|  |     fn test_piouf() { | ||||||
|  |         let mut r: Robot = Robot::new( | ||||||
|  |             0, | ||||||
|  |             Orientation::N, | ||||||
|  |             Position { x: 1, y: 2 }, | ||||||
|  |             vec!['R', 'F', 'L', 'F'], | ||||||
|  |         ); | ||||||
|  |         r.i.pop(); | ||||||
|  |         r.i.pop(); | ||||||
|  |         r.i.pop(); | ||||||
|  |         r.i.pop(); | ||||||
|  |         assert!(is_piouff(&r)); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Martin HART
						Martin HART