-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple_month_game.rs
115 lines (92 loc) · 3.13 KB
/
simple_month_game.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Notice add this dependencies to your Cargo.toml
// rand = "0.8.5"
// ansi_term = "0.12"
extern crate ansi_term;
use ansi_term::Color;
use rand::Rng;
use std::io;
use std::time::Instant;
const NUMBER_OF_ROUNDS: i32 = 10;
struct GameStats {
start_time: Instant,
end_time: Instant,
score: i32,
}
fn main() {
println!("{}", Color::Yellow.paint("GUESS THE MONTH GAME!"));
println!("{}", Color::RGB(220, 220, 220)
.paint("Enter a number from 1 to 12 to guess."));
let game_stats = game_loop();
print_stats(&game_stats);
}
fn game_loop() -> GameStats {
let mut rng = rand::thread_rng();
let mut rounds = NUMBER_OF_ROUNDS;
let mut score = 0;
let months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
];
let start_time = Instant::now();
while rounds > 0 {
let random_number = rng.gen_range(1..=12);
let month = months[random_number as usize - 1];
println!(
"{}{}",
Color::Yellow.paint("What month is "),
Color::Green.paint(month)
);
let mut user_input = String::new();
io::stdin()
.read_line(&mut user_input)
.expect("Failed to read line");
let user_guess = user_input.trim();
if let Ok(guess) = user_guess.parse::<i32>() {
if (1..=12).contains(&guess) {
if guess == random_number {
println!("{}", Color::Green.paint("Correct!"));
score += 1;
rounds -= 1;
} else {
println!("{}", Color::Red.paint("Incorrect!"));
rounds -= 1;
}
} else {
println!("{}", Color::Red.paint("Please use a number from 1 to 12!"));
}
} else {
println!("{}", Color::Red.paint("Invalid input. Please enter a number."));
}
}
let end_time = Instant::now();
GameStats {
start_time,
end_time,
score,
}
}
fn print_stats(stats: &GameStats) {
println!();
println!("{}", Color::Yellow.paint("STATS:"));
println!("{}{}", Color::Yellow.paint("Rounds: "),
Color::Green.paint(NUMBER_OF_ROUNDS.to_string()));
println!("{}{}", Color::Yellow.paint("Score: "),
Color::Green.paint((stats.score * 10).to_string()));
println!("{}{}", Color::Yellow.paint("Correct Answers: "),
Color::Green.paint(stats.score.to_string()));
println!("{}{}", Color::Yellow.paint("Wrong Answers: "),
Color::Red.paint((NUMBER_OF_ROUNDS - stats.score).to_string()));
let correct_percent = (stats.score * 100) / NUMBER_OF_ROUNDS;
println!(
"{}{}%",
Color::Yellow.paint("Correct percentage: "),
Color::Green.paint(correct_percent.to_string())
);
let elapsed_time = stats.end_time.duration_since(stats.start_time);
let elapsed_secs = elapsed_time.as_secs();
println!(
"{}{}sec",
Color::Yellow.paint("Time: "),
Color::Green.paint(elapsed_secs.to_string())
);
}