mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-05 09:39:32 -08:00
Added Rust Conversion of Hi-Lo
This commit is contained in:
9
47_Hi-Lo/rust/Cargo.toml
Normal file
9
47_Hi-Lo/rust/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "guessing_game"
|
||||
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
47_Hi-Lo/rust/README.md
Normal file
3
47_Hi-Lo/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/)
|
||||
73
47_Hi-Lo/rust/src/main.rs
Normal file
73
47_Hi-Lo/rust/src/main.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use rand::Rng;
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"{: >39}\n{: >57}\n\n\n",
|
||||
"HI LO", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
);
|
||||
println!("THIS IS THE GAME OF HI LO.\n");
|
||||
println!("YOU WILL HAVE 6 TRIES TO GUESS THE AMOUNT OF MONEY IN THE");
|
||||
println!("HI LO JACKPOT, WHICH IS BETWEEN 1 AND 100 DOLLARS. IF YOU");
|
||||
println!("GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!");
|
||||
println!("THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY. HOWEVER,");
|
||||
println!("IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS.\n");
|
||||
|
||||
let mut total: u32 = 0;
|
||||
loop {
|
||||
let jackpot_amount = rand::thread_rng().gen_range(1..101); // generates a random number between 1 and 100
|
||||
for i in 0..6 {
|
||||
println!("YOUR GUESS?");
|
||||
|
||||
let mut guess = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut guess)
|
||||
.expect("Failed to read the line");
|
||||
|
||||
// this converts the input string into unsigned 32bit number and if the input entered is not a number
|
||||
// it will again prompt the user to enter the guess number
|
||||
let guess: u32 = match guess.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => {
|
||||
println!("PLEASE ENTER A NUMBER VALUE.\n");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// compare it with the jackpot amount
|
||||
if guess == jackpot_amount {
|
||||
println!("\nGOT IT!!!!!!!!!! YOU WIN {} DOLLARS.", jackpot_amount);
|
||||
total += jackpot_amount;
|
||||
println!("YOUR TOTAL WINNINGS ARE NOW {} DOLLARS.\n", total);
|
||||
break;
|
||||
} else if guess < jackpot_amount {
|
||||
println!("YOUR GUESS IS TOO LOW.\n");
|
||||
} else {
|
||||
println!("YOUR GUESS IS TOO HIGH.\n");
|
||||
}
|
||||
|
||||
// if 6 tries are over make total jackpot amount to zero
|
||||
if i == 5 {
|
||||
total = 0;
|
||||
println!(
|
||||
"YOU BLEW IT...TOO BAD...THE NUMBER WAS {}\n",
|
||||
jackpot_amount
|
||||
);
|
||||
}
|
||||
}
|
||||
println!("PLAY AGAIN (YES OR NO)?");
|
||||
let mut tocontinue = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut tocontinue)
|
||||
.expect("Error Getting your input");
|
||||
let tocontinue = tocontinue.trim().to_ascii_uppercase();
|
||||
if tocontinue.eq("YES") {
|
||||
println!("\n");
|
||||
continue;
|
||||
} else {
|
||||
println!("\nSO LONG. HOPE YOU ENJOYED YOURSELF!!!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user