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,
|
w: &world::World,
|
||||||
robot_pool: &Vec<robot::Robot>,
|
robot_pool: &Vec<robot::Robot>,
|
||||||
h: &HashMap<robot::Position, u32>,
|
h: &HashMap<robot::Position, u32>,
|
||||||
) {
|
) -> String {
|
||||||
for i in (0..w.y).rev() {
|
let mut grid = String::new();
|
||||||
|
for i in (0..w.y + 1).rev() {
|
||||||
if i < 10 {
|
if i < 10 {
|
||||||
print!("{} ", i);
|
grid = format!("{}{} ", grid, i);
|
||||||
} else {
|
} 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 }) {
|
match h.get(&robot::Position { x: j, y: i }) {
|
||||||
Some(id) => match robot_pool[(id - 1) as usize].o {
|
Some(id) => match robot_pool[(id - 1) as usize].o {
|
||||||
robot::Orientation::N => print!("↑ "),
|
robot::Orientation::N => grid = format!("{}↑ ", grid),
|
||||||
robot::Orientation::E => print!("→ "),
|
robot::Orientation::E => grid = format!("{}→ ", grid),
|
||||||
robot::Orientation::S => print!("↓ "),
|
robot::Orientation::S => grid = format!("{}↓ ", grid),
|
||||||
robot::Orientation::W => print!("← "),
|
robot::Orientation::W => grid = format!("{}← ", grid),
|
||||||
},
|
},
|
||||||
|
|
||||||
None => print!(". "),
|
None => grid = format!("{}. ", grid),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!();
|
grid = format!("{}\n", grid);
|
||||||
}
|
}
|
||||||
print!(" ");
|
grid = format!("{} ", grid);
|
||||||
for j in 0..w.x {
|
for j in 0..w.x + 1 {
|
||||||
if j < 10 {
|
if j < 10 {
|
||||||
print!("{} ", j);
|
grid = format!("{}{} ", grid, j);
|
||||||
} else {
|
} else {
|
||||||
print!("{} ", j);
|
grid = format!("{}{} ", grid, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!();
|
grid = format!("{}\n", grid);
|
||||||
|
grid
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
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();
|
let mut hash: HashMap<robot::Position, u32> = HashMap::new();
|
||||||
create_hash_map(&robot_pool, &mut hash);
|
create_hash_map(&robot_pool, &mut hash);
|
||||||
println!("World {{ x_max = {}; y_max = {} }}", world.x, world.y);
|
let mut print_all = format!("World {{ x_max = {}; y_max = {} }}\n", world.x, world.y);
|
||||||
robot::print_robots(&robot_pool);
|
print_all = format!("{}{}", print_all, robot::print_robots(&robot_pool));
|
||||||
println!("Initial state");
|
print_all = format!("{}Initial state\n", print_all);
|
||||||
println!("==============");
|
print_all = format!("{}==============\n", print_all);
|
||||||
display_grid(&world, &robot_pool, &hash);
|
print_all = format!("{}{}", print_all, display_grid(&world, &robot_pool, &hash));
|
||||||
loop {
|
loop {
|
||||||
let mut piouff: u32 = 0;
|
let mut piouff: u32 = 0;
|
||||||
for r in &mut robot_pool {
|
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);
|
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 {
|
if piouff == robot_pool.len() as u32 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
print!("\x1B[2J\x1B[1;1H");
|
||||||
|
print!("{}", print_all);
|
||||||
println!("Final state");
|
println!("Final state");
|
||||||
println!("============");
|
println!("============");
|
||||||
display_grid(&world, &robot_pool, &hash);
|
print!("{}", display_grid(&world, &robot_pool, &hash));
|
||||||
for r in &robot_pool {
|
for r in &robot_pool {
|
||||||
println!("Robot id: {}: Final position: ({}, {})", r.id, r.p.x, r.p.y);
|
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.
|
/// Print robots id, position and instructions.
|
||||||
pub fn print_robots(robot_pool: &Vec<Robot>) {
|
pub fn print_robots(robot_pool: &Vec<Robot>) -> String {
|
||||||
println!("Robots [");
|
let mut res = format!("Robots [\n");
|
||||||
for r in robot_pool {
|
for r in robot_pool {
|
||||||
println!(
|
res = format!(
|
||||||
"{{ id = {}, x = {}; y = {}; orientation: {}, instructions: {:?}, }},",
|
"{}{{ id = {}, x = {}; y = {}; orientation: {}, instructions: {:?}, }},\n",
|
||||||
|
res,
|
||||||
r.id,
|
r.id,
|
||||||
r.p.x,
|
r.p.x,
|
||||||
r.p.y,
|
r.p.y,
|
||||||
@ -112,7 +113,8 @@ pub fn print_robots(robot_pool: &Vec<Robot>) {
|
|||||||
r.i
|
r.i
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
println!("]");
|
res = format!("{}]\n", res);
|
||||||
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
Reference in New Issue
Block a user