Skip to content

Commit

Permalink
add guessing game file
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphonseMehounme committed Nov 22, 2024
1 parent bfaacc0 commit bfeb1a3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Rust/guessing_game.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {
println!("--- Guess The Number! ---");

let secret_number = rand::thread_rng().gen_range(1..=100);

println!("The secret secret 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");

let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};

println!("You guessed: {}", guess);

match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small"),
Ordering::Greater => println!("Too Big"),
Ordering::Equal => {
println!("You win, Congratulations :)");
break;
}
}
}
}

0 comments on commit bfeb1a3

Please sign in to comment.