Finish cleanup of Assignment 6, complete of Rust, and most of 396's assignments.
This commit is contained in:
parent
5bdbe09523
commit
70d1cf9b35
8 changed files with 371 additions and 152 deletions
74
maze.rs
Normal file
74
maze.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue