74 lines
No EOL
1.7 KiB
Rust
74 lines
No EOL
1.7 KiB
Rust
use std::io;
|
|
|
|
fn main()
|
|
{
|
|
let maze = [[' ', ' ', 'X'], [' ', ' ', ' '], [' ', 'E', ' ']];
|
|
let mut x = 0;
|
|
let mut y = 0;
|
|
|
|
loop
|
|
{
|
|
if maze[x][y] == 'E'
|
|
{
|
|
println!("You found the exit.");
|
|
break;
|
|
}
|
|
|
|
let mut moves = Vec::new();
|
|
if x > 0 && maze[x - 1][y] != 'X'
|
|
{
|
|
// north
|
|
moves.push("North");
|
|
}
|
|
if x < 2 && maze[x + 1][y] != 'X'
|
|
{
|
|
// south
|
|
moves.push("South");
|
|
}
|
|
if y > 0 && maze[x][y - 1] != 'X'
|
|
{
|
|
// west
|
|
moves.push("West");
|
|
}
|
|
if y < 2 && maze[x][y + 1] != 'X'
|
|
{
|
|
// east
|
|
moves.push("East");
|
|
}
|
|
println!("There are open doors: {}", moves.join(", "));
|
|
println!("Which door do you want to walk through?");
|
|
|
|
let mut input = String::new();
|
|
io::stdin().read_line(&mut input).expect("Failed to read input");
|
|
input = input.trim().to_uppercase();
|
|
|
|
match input.as_str()
|
|
{
|
|
"N" if x > 0 && maze[x - 1][y] != 'X' =>
|
|
{
|
|
x = x - 1;
|
|
}
|
|
"S" if x < 2 && maze[x + 1][y] != 'X' =>
|
|
{
|
|
x = x + 1;
|
|
}
|
|
"E" if y < 2 && maze[x][y + 1] != 'X' =>
|
|
{
|
|
y = y + 1;
|
|
}
|
|
"W" if y > 0 && maze[x][y - 1] != 'X' =>
|
|
{
|
|
y = y - 1;
|
|
}
|
|
"Q" =>
|
|
{
|
|
println!("You quit the game.");
|
|
break;
|
|
}
|
|
_ =>
|
|
{
|
|
println!("Invalid move.");
|
|
}
|
|
}
|
|
}
|
|
} |