diff --git a/guessing_game/src/main.rs b/guessing_game/src/main.rs index c367dd2..5ac4c7f 100644 --- a/guessing_game/src/main.rs +++ b/guessing_game/src/main.rs @@ -1,21 +1,33 @@ -use std::io; use rand::Rng; +use std::cmp::Ordering; +use std::io; fn main() { println!("Guess the number!"); - let secret_number = rand::thread_rng().gen_range(1..=100); - println!("psst. the secret number is {secret_number}"); + loop { + println!("Please input your guess."); + let mut guess = String::new(); + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); - println!("Please input your guess."); + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; - let mut guess = String::new(); - io::stdin() - .read_line(&mut guess) - .expect("Failed to read line"); + println!("You guessed {guess}"); - println!("You guessed {guess}"); - -} \ No newline at end of file + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too Small"), + Ordering::Greater => println!("too big"), + Ordering::Equal => { + println!("you win"); + break; + } + } + } +}