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
use console::{style, Term};
use crate::{util::get_char_input, Play};
use std::{
collections::BTreeSet,
io::{stdout, Write},
};
pub struct GuessTheWord;
impl Play for GuessTheWord {
fn start(&self) {
let word = eff_wordlist::large::random_word();
let mut unique_chars = BTreeSet::from_iter(word.chars());
unique_chars.remove(&' ');
let mut guessed_chars: Vec<char> = Vec::with_capacity(26);
let mut guess_left = 10;
let alphabets: [char; 26] = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
];
let term = Term::stdout();
term.clear_screen().expect("Failed to clear screen");
loop {
for c in word.chars() {
if guessed_chars.contains(&c) || c == ' ' {
print!("{c}");
} else {
print!("_")
}
}
println!("\nGuesses left: {}", style(guess_left).red());
print!("From ");
for c in alphabets {
if guessed_chars.contains(&c) {
print!("_")
} else {
print!("{c}");
}
print!(" ");
}
println!();
print!("You pick: ");
stdout().flush().expect("Failed to flush");
let input = get_char_input();
if input.is_none() {
term.clear_screen().expect("Failed to clear screen");
continue;
}
let input = input.unwrap();
if guessed_chars.contains(&input) {
term.clear_screen().expect("Failed to clear screen");
println!("You have entered a guessed character\n");
continue;
}
guessed_chars.push(input);
if unique_chars.contains(&input) {
unique_chars.remove(&input);
} else {
guess_left -= 1;
}
if unique_chars.is_empty() {
println!("You win!\nThe word is: {word}\n");
break;
}
if guess_left == 0 {
println!("You lose!\nThe word is: {word}\n");
break;
}
println!();
term.clear_screen().expect("Failed to clear screen");
}
}
fn name(&self) -> &'static str {
"Guess the Word"
}
}