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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use rand::Rng;
use std::collections::HashMap;
use std::fmt;
use state::*;
use state::grids::*;
mod spectators;
pub use self::spectators::*;
#[derive(Debug, PartialEq, Clone)]
pub enum State {
Start,
Round,
End,
InvalidTransition(Box<State>, Event),
}
#[derive(Debug, PartialEq, Clone)]
pub enum Event {
Turn(HashMap<String, Direction>),
}
pub struct Game {
state: State,
rng: Box<Rng>,
grid: Grid,
game_state: GameState,
round_state: RoundState,
}
impl Game {
pub fn new(rng: Box<Rng>, grid: Grid) -> Self {
let mut game = Game {
state: State::Start,
rng: rng,
grid: grid,
game_state: GameState::new(grid),
round_state: RoundState::default(),
};
let mut round_state = RoundState::default();
game.manage_food(&mut round_state);
game.round_state = round_state;
game
}
pub fn add_player(&mut self, desired_name: String) -> String {
let mut final_name = desired_name;
while self.game_state.players.contains(&final_name) {
final_name += "_";
}
self.game_state.players.insert(final_name.clone());
let head = self.grid.random_cell(&mut self.rng);
let snake = Snake::new(vec![head]);
self.round_state
.snakes
.insert(final_name.clone(), snake);
final_name
}
pub fn next(&mut self, event: Event) {
self.state = match (self.state.clone(), event) {
(State::Start, Event::Turn(directions)) |
(State::Round, Event::Turn(directions)) => {
self.advance_round(directions);
if self.concluded() {
State::End
} else {
State::Round
}
}
(s, e) => State::InvalidTransition(Box::new(s), e),
};
}
pub fn concluded(&self) -> bool {
let number_of_living_snakes = self.round_state.snakes.len();
match number_of_living_snakes {
0 | 1 => true,
_ => false,
}
}
pub fn state(&self) -> &State {
&self.state
}
pub fn game_state(&self) -> &GameState {
&self.game_state
}
pub fn round_state(&self) -> &RoundState {
&self.round_state
}
fn advance_round(&mut self, moves: HashMap<String, Direction>) -> RoundState {
let mut next_round: RoundState = self.round_state.clone();
next_round.eaten.clear();
next_round.directions.clear();
next_round.casualties.clear();
self.snake_movement(&mut next_round, moves);
self.remove_snakes(&mut next_round);
self.snake_eating(&mut next_round);
self.manage_food(&mut next_round);
self.snake_collisions(&mut next_round);
self.remove_snakes(&mut next_round);
self.snake_grid_bounds(&mut next_round);
self.remove_snakes(&mut next_round);
next_round.round_number += 1;
self.round_state = next_round.clone();
next_round
}
fn snake_movement(&mut self,
next_round: &mut RoundState,
mut moves: HashMap<String, Direction>) {
for (name, snake) in &mut next_round.snakes {
match moves.remove(name) {
Some(direction) => {
snake.step_in_direction(direction);
next_round.directions.insert(name.clone(), direction);
}
_ => {
let cause_of_death = CauseOfDeath::NoMoveMade;
next_round
.casualties
.insert(name.clone(), cause_of_death);
}
}
}
}
fn snake_eating(&mut self, next_round: &mut RoundState) {
for (name, snake) in &mut next_round.snakes {
if next_round.food.contains(&snake.segments[0]) {
snake.grow();
next_round.eaten.insert(name.clone(), snake.segments[0]);
}
}
}
fn snake_collisions(&mut self, next_round: &mut RoundState) {
for (name, snake) in &next_round.snakes {
for coll_snake in next_round.snakes.values() {
if snake != coll_snake && snake.has_collided_into(coll_snake) {
next_round
.casualties
.insert(name.clone(), CauseOfDeath::CollidedWithSnake);
break;
}
}
}
}
fn snake_grid_bounds(&mut self, next_round: &mut RoundState) {
for (name, snake) in &next_round.snakes {
for &segment in &snake.segments {
if !self.grid.is_within_bounds(segment) {
next_round
.casualties
.insert(name.clone(), CauseOfDeath::CollidedWithBounds);
}
}
}
}
fn remove_snakes(&mut self, next_round: &mut RoundState) {
for name in next_round.casualties.keys() {
if let Some(dead_snake) = next_round.snakes.remove(name) {
if let Some((_, headless_segments)) = dead_snake.segments.split_first() {
let corpse_food: Vec<&Vector> = headless_segments
.iter()
.filter(|&s| self.grid.is_within_bounds(*s))
.collect();
next_round.food.extend(corpse_food);
}
}
}
}
fn manage_food(&mut self, next_round: &mut RoundState) {
for food in next_round.eaten.values() {
next_round.food.remove(food);
}
if next_round.food.len() < 1 {
let new_food = self.grid.random_cell(&mut self.rng);
next_round.food.insert(new_food);
}
}
}
impl fmt::Debug for Game {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Game")
.field("state", &self.state)
.field("grid", &self.grid)
.field("game_state", &self.game_state)
.field("round_state", &self.round_state)
.finish()
}
}