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
extern crate rand;
extern crate thiserror;
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-2020.";
pub const TITLE: &str = "ASCII-Hangman for Kids\n";
pub const LIVES: u8 = 7;
pub const CONF_TEMPLATE: &str = "# Add own secrets here, one per line.\r
\r
guess me\r
_good l_uck\r
_3*_7_=21_\r
_der Hund = _the dog\r
";
#[derive(Debug)]
pub struct Backend {
dict: Dict,
game: Game,
image: Image,
}
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::new(&config)?;
let secret = dict.get_random_secret().unwrap();
let game = Game::new(&secret, LIVES, dict.is_empty());
let mut image = Image::new(&config)?;
image.update(&game);
Ok(Self { dict, game, 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());
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(' '));
if self.game.state != State::Defeat && self.game.state != State::DefeatGameOver {
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()
}
}