mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-25 12:25:10 -08:00
rust port
This commit is contained in:
9
51_Hurkle/rust/Cargo.toml
Normal file
9
51_Hurkle/rust/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
103
51_Hurkle/rust/src/game.rs
Normal file
103
51_Hurkle/rust/src/game.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use std::io;
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
type Position = (u8, u8);
|
||||
|
||||
pub struct Game {
|
||||
hurkle: Position,
|
||||
tries: u8,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub fn new() -> Self {
|
||||
let x: u8 = rand::thread_rng().gen_range(1..=10);
|
||||
let y: u8 = rand::thread_rng().gen_range(1..=10);
|
||||
let hurkle = (x, y);
|
||||
|
||||
Game { hurkle, tries: 0 }
|
||||
}
|
||||
|
||||
pub fn update(&mut self) -> bool {
|
||||
if self.tries >= 5 {
|
||||
println!("SORRY, THAT'S {} GUESSES.", self.tries);
|
||||
println!("THE HURKLE IS AT {}, {}", self.hurkle.0, self.hurkle.1);
|
||||
return true;
|
||||
}
|
||||
self.tries += 1;
|
||||
self.process_guess(self.get_guess())
|
||||
}
|
||||
|
||||
fn get_guess(&self) -> Position {
|
||||
let mut pos = (0, 0);
|
||||
|
||||
'guess: loop {
|
||||
println!("GUESS # {}?", self.tries);
|
||||
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("**Failed to read line**");
|
||||
|
||||
let input: Vec<&str> = input.trim().split(",").collect();
|
||||
|
||||
let mut is_y = false;
|
||||
for a in input {
|
||||
match a.parse::<u8>() {
|
||||
Ok(a) => {
|
||||
if a > 10 || a == 0 {
|
||||
println!("GUESS AXIS CANNOT BE ZERO OR LARGER THAN TEN!");
|
||||
break;
|
||||
}
|
||||
if is_y {
|
||||
pos.1 = a;
|
||||
break 'guess;
|
||||
} else {
|
||||
pos.0 = a;
|
||||
is_y = true;
|
||||
}
|
||||
}
|
||||
Err(e) => println!("INVALID GUESS - TRY AGAIN! Error: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pos
|
||||
}
|
||||
|
||||
fn process_guess(&self, p: Position) -> bool {
|
||||
if p == self.hurkle {
|
||||
println!("\nYOU FOUND HIM IN {} GUESSES!", self.tries);
|
||||
return true;
|
||||
}
|
||||
|
||||
let (x, y) = (p.0, p.1);
|
||||
let (hx, hy) = (self.hurkle.0, self.hurkle.1);
|
||||
|
||||
let mut dir_x = "WEST";
|
||||
let mut dir_y = "SOUTH";
|
||||
|
||||
let mut set_y_dir = || {
|
||||
if y < hy {
|
||||
dir_y = "NORTH";
|
||||
} else {
|
||||
dir_y = "";
|
||||
}
|
||||
};
|
||||
|
||||
if x > hx {
|
||||
set_y_dir();
|
||||
} else if x < hx {
|
||||
dir_x = "EAST";
|
||||
set_y_dir();
|
||||
} else {
|
||||
dir_x = "";
|
||||
set_y_dir();
|
||||
}
|
||||
|
||||
println!("GO {}{}\n", dir_y, dir_x);
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
29
51_Hurkle/rust/src/main.rs
Normal file
29
51_Hurkle/rust/src/main.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use game::Game;
|
||||
|
||||
mod game;
|
||||
|
||||
fn main() {
|
||||
println!("\n\n\t\tHURKLE");
|
||||
println!("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
|
||||
println!("A HURKLE IS HIDING ON A 10 BY 10 GRID. HOMEBASE");
|
||||
println!("ON THE GRID IS POINT 0,0 IN THE SOUTHWEST CORNER,");
|
||||
println!("AND ANY POINT ON THE GRID IS DESIGNATED BY A");
|
||||
println!("PAIR OF WHOLE NUMBERS SEPERATED BY A COMMA. THE FIRST");
|
||||
println!("NUMBER IS THE HORIZONTAL POSITION AND THE SECOND NUMBER");
|
||||
println!("IS THE VERTICAL POSITION. YOU MUST TRY TO");
|
||||
println!("GUESS THE HURKLE'S GRIDPOINT. YOU GET 5 TRIES.");
|
||||
println!("AFTER EACH TRY, I WILL TELL YOU THE APPROXIMATE");
|
||||
println!("DIRECTION TO GO TO LOOK FOR THE HURKLE.\n");
|
||||
|
||||
loop {
|
||||
let mut game = Game::new();
|
||||
|
||||
loop {
|
||||
if game.update() {
|
||||
println!("\nLET'S PLAY AGAIN. HURKLE IS HIDING.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user