mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 07:40:50 -08:00
9
82_Stars/rust/Cargo.toml
Normal file
9
82_Stars/rust/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "stars"
|
||||
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.3"
|
||||
3
82_Stars/rust/README.md
Normal file
3
82_Stars/rust/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Rust](https://www.rust-lang.org/)
|
||||
85
82_Stars/rust/src/main.rs
Normal file
85
82_Stars/rust/src/main.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use rand::Rng;
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"{: >39}\n{: >57}\n\n\n",
|
||||
"STARS", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
);
|
||||
// STARS - PEOPLE'S COMPUTER CENTER, MENLO PARK, CA
|
||||
// A IS LIMIT ON NUMBER, M IS NUMBER OF GUESSES
|
||||
let a: u32 = 101;
|
||||
let m: u32 = 7;
|
||||
let mut need_instrut = String::new();
|
||||
|
||||
println!("DO YOU WANT INSTRUCTIONS?");
|
||||
io::stdin()
|
||||
.read_line(&mut need_instrut)
|
||||
.expect("Failed to get input");
|
||||
|
||||
if need_instrut[..1].to_ascii_lowercase().eq("y") {
|
||||
println!("I AM THINKING OF A WHOLE NUMBER FROM 1 TO {}", a - 1);
|
||||
println!("TRY TO GUESS MY NUMBER. AFTER YOU GUESS, I");
|
||||
println!("WILL TYPE ONE OR MORE STARS (*). THE MORE");
|
||||
println!("STARS I TYPE, THE CLOSER YOU ARE TO MY NUMBER.");
|
||||
println!("ONE STAR (*) MEANS FAR AWAY, SEVEN STARS (*******)");
|
||||
println!("MEANS REALLY CLOSE! YOU GET {} GUESSES.\n\n", m);
|
||||
}
|
||||
|
||||
loop {
|
||||
println!("\nOK, I AM THINKING OF A NUMBER, START GUESSING.\n");
|
||||
let rand_number: i32 = rand::thread_rng().gen_range(1..a) as i32; // generates a random number between 1 and 100
|
||||
|
||||
// GUESSING BEGINS, HUMAN GETS M GUESSES
|
||||
for i in 0..m {
|
||||
let mut guess = String::new();
|
||||
println!("YOUR GUESS?");
|
||||
io::stdin()
|
||||
.read_line(&mut guess)
|
||||
.expect("Failed to get input");
|
||||
let guess: i32 = match guess.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => {
|
||||
println!("PLEASE ENTER A NUMBER VALUE.\n");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if guess == rand_number {
|
||||
print!("");
|
||||
for _i in 0..50 {
|
||||
print!("*");
|
||||
}
|
||||
println!("!!!");
|
||||
println!("YOU GOT IT IN {} GUESSES!!! LET'S PLAY AGAIN...\n", i + 1);
|
||||
break;
|
||||
} else {
|
||||
match_guess(rand_number - guess);
|
||||
}
|
||||
|
||||
if i == 6 {
|
||||
println!(
|
||||
"SORRY, THAT'S {} GUESSES. THE NUMBER WAS {}",
|
||||
m, rand_number
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn match_guess(diff: i32) {
|
||||
if diff.abs() >= 64 {
|
||||
println!("*\n");
|
||||
} else if diff.abs() >= 32 {
|
||||
println!("**\n");
|
||||
} else if diff.abs() >= 16 {
|
||||
println!("***\n");
|
||||
} else if diff.abs() >= 8 {
|
||||
println!("****\n");
|
||||
} else if diff.abs() >= 4 {
|
||||
println!("*****\n");
|
||||
} else if diff.abs() >= 2 {
|
||||
println!("******\n");
|
||||
} else {
|
||||
println!("*******\n");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user