Created
November 28, 2021 19:05
-
-
Save kevincolyer/e0b9f3b37d4097a9c9ebb8472f933e90 to your computer and use it in GitHub Desktop.
Guess the number game in Rust language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate text_io; | |
use std::io::stdout; | |
use std::io::Write; | |
use text_io::read; | |
use rand::Rng; | |
fn main() { | |
let mut rng = rand::thread_rng(); | |
let num: u8 = rng.gen_range(1..101); | |
let maxguesses = 7; | |
println!("I'm thinking of a number from 1 to 100. What number am I thinking of?"); | |
for count in (1..=maxguesses).rev() { | |
print!("\nYou have {} guesses\nWhat is your guess?\n>", count); | |
stdout().flush().unwrap(); | |
let input: u8 = read!(); | |
// echo back input | |
print!("{}: ", input); | |
if input > num { | |
println!("Too high!") | |
} else if input < num { | |
println!("Too low!") | |
} else { | |
println!("That's it! I was thinking of {}!", num); | |
return; | |
} | |
} | |
println!("You lose! The number was {}", num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment