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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
mod ascii_art;
mod dictionary;
pub mod game;
mod image;
mod secret;
use crate::dictionary::ConfigParseError;
use crate::dictionary::Dict;
use crate::game::Game;
use crate::game::State;
use crate::image::Image;
pub const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
pub const AUTHOR: &str = "(c) Jens Getreu, 2016-2021.";
pub const TITLE: &str = "ASCII-Hangman for Kids\n";
pub const CHANGE_IMAGE_MAX: usize = 5;
pub const LIVES: u8 = 7;
pub const CONF_TEMPLATE: &str = "# Add own secrets here; one per line.\r
\r
secrets:\r
- guess me\r
- _good l_uck\r
- \"_der Hund:_| the dog\"\r
- _3*_7_=21_\r
\r
\r
# Uncomment 3 lines to use an optional custom image:\r
\r
#image: |1\r
# ::\r
# C|__|\r
";
pub const CONF_TEMPLATE_SHORT: &str = "# Replace the sample secrets with your own; one per line.\r
\r
secrets:\r
- guess me\r
- _good l_uck\r
- \"_der Hund:_| the dog\"\r
- _3*_7_=21_\r
";
#[derive(Debug)]
pub struct Backend {
dict: Dict,
game: Game,
image: Image,
change_image: Option<usize>,
}
pub trait HangmanBackend {
fn new(config: &str) -> Result<Self, ConfigParseError>
where
Self: std::marker::Sized;
fn process_user_input(&mut self, inp: &str);
fn render_image(&self) -> String;
fn get_image_dimension(&self) -> (u8, u8);
fn render_secret(&self) -> String;
fn render_game_lifes(&self) -> String;
fn render_game_last_guess(&self) -> String;
fn render_instructions(&self) -> String;
fn get_state(&self) -> State;
}
impl HangmanBackend for Backend {
fn new(config: &str) -> Result<Self, ConfigParseError> {
let mut dict = Dict::from(config)?;
let secret = dict.get_random_secret().unwrap();
let game = Game::new(&secret, LIVES, dict.is_empty());
let mut change_image = None;
let mut image = Image::from_yaml(config).or_else(|_| {
change_image = Some(0);
Image::new()
})?;
image.update(&game);
Ok(Self {
dict,
game,
image,
change_image,
})
}
fn process_user_input(&mut self, inp: &str) {
match self.game.state {
State::Victory => {
let secret = self.dict.get_random_secret().unwrap();
self.game = Game::new(&secret, LIVES, self.dict.is_empty());
if let Some(n) = self.change_image {
if n == CHANGE_IMAGE_MAX - 1 {
if let Ok(new_image) = Image::new() {
self.image = new_image;
};
self.change_image = Some(0);
} else {
self.change_image = Some(n + 1);
};
};
self.image.update(&self.game);
}
State::VictoryGameOver => {}
State::Defeat | State::DefeatGameOver => {
self.dict.add((self.game.secret).to_raw_string());
let secret = self.dict.get_random_secret().unwrap();
self.game = Game::new(&secret, LIVES, self.dict.is_empty());
self.image.update(&self.game);
}
State::Ongoing => {
self.game.guess(inp.chars().next().unwrap_or(' '));
self.image.update(&self.game);
}
}
}
fn render_image(&self) -> String {
format!("{}", self.image)
}
#[allow(dead_code)]
fn get_image_dimension(&self) -> (u8, u8) {
self.image.dimension
}
fn render_secret(&self) -> String {
format!("{}", self.game.secret)
}
fn render_game_lifes(&self) -> String {
format!("Lifes: {}", self.game.lifes)
}
fn render_game_last_guess(&self) -> String {
format!("Last guess: {}", self.game.last_guess)
}
fn render_instructions(&self) -> String {
match self.game.state {
State::Victory => String::from("Congratulations! You won!"),
State::VictoryGameOver => String::from("Congratulations! You won!"),
State::Defeat | State::DefeatGameOver => String::from("You lost."),
State::Ongoing => String::from("Type a letter, then press [Enter]:"),
}
}
fn get_state(&self) -> State {
self.game.state.clone()
}
}