From bd75edba47ec4ea205a58eac40d4e924e78227ac Mon Sep 17 00:00:00 2001 From: jay-0331 Date: Wed, 30 Mar 2022 12:31:19 +0530 Subject: [PATCH] Added Rust Conversion of Hi-Lo --- 47_Hi-Lo/rust/Cargo.toml | 9 +++++ 47_Hi-Lo/rust/README.md | 3 ++ 47_Hi-Lo/rust/src/main.rs | 73 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 47_Hi-Lo/rust/Cargo.toml create mode 100644 47_Hi-Lo/rust/README.md create mode 100644 47_Hi-Lo/rust/src/main.rs diff --git a/47_Hi-Lo/rust/Cargo.toml b/47_Hi-Lo/rust/Cargo.toml new file mode 100644 index 00000000..d39f6641 --- /dev/null +++ b/47_Hi-Lo/rust/Cargo.toml @@ -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" \ No newline at end of file diff --git a/47_Hi-Lo/rust/README.md b/47_Hi-Lo/rust/README.md new file mode 100644 index 00000000..fc6468b9 --- /dev/null +++ b/47_Hi-Lo/rust/README.md @@ -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/) diff --git a/47_Hi-Lo/rust/src/main.rs b/47_Hi-Lo/rust/src/main.rs new file mode 100644 index 00000000..0b821c3b --- /dev/null +++ b/47_Hi-Lo/rust/src/main.rs @@ -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; + } + } +}