Created
March 19, 2020 08:27
-
-
Save Canop/eacb06cbf9135a040f3839b5360e9ff6 to your computer and use it in GitHub Desktop.
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
use { | |
crossterm::style::Colorize, | |
rand::Rng, | |
std::io::*, | |
}; | |
fn int() -> i64 { | |
rand::thread_rng().gen_range(10, 50) | |
} | |
struct QA { | |
question: String, | |
answer: i64, | |
} | |
impl QA { | |
fn new() -> Self { | |
let (a, b) = (int(), int()); | |
Self { | |
question: format!("{} + {}", a, b), | |
answer: a + b, | |
} | |
} | |
} | |
fn main() { | |
let stdin = stdin(); | |
let mut input = stdin.lock().lines().map(|l| l.unwrap()); | |
let (mut good, mut bad) = (0, 0); | |
loop { | |
let qa = QA::new(); | |
println!("{} = ?", qa.question); | |
match input.next().unwrap().parse::<i64>() { | |
Ok(answer) => { | |
if answer == qa.answer { | |
println!("{}", "Right!".green()); | |
good += 1; | |
} else { | |
println!("{}", "Wrong".red()); | |
bad += 1; | |
} | |
} | |
Err(_) => { // anything not a number quits | |
println!("You got {} / {}", good, good + bad ); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment