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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
#[macro_use] extern crate serde_json; #[macro_use] mod piece; #[macro_use] mod game_move; mod location; mod player; #[cfg(test)] mod test; use serde_json::{Value, Error}; pub use piece::Piece; pub use game_move::GameMove; pub use location::Location; pub use player::Player; #[derive(PartialEq, Debug)] pub enum GameStatus { Playing, Win(Player), Draw } #[derive(PartialEq, Debug)] pub struct Game{ pieces: Vec<Piece>, turn: u8, next_player: Player } impl Game { pub fn new() -> Game { Game{ pieces: Self::get_new_pieces(), turn: 0, next_player: Player::One } } pub fn load(serialised_game: &str) -> Result<Game, Error> { let v: Value = serde_json::from_str(serialised_game)?; let pieces = Self::unwrap_pieces(v["pieces"].clone()); Ok(Game{ pieces: pieces, turn: v["turn"].as_u64().unwrap() as u8, next_player: Self::unwrap_player(v["player_turn"].as_u64().unwrap()) }) } pub fn get_json(&self) -> String { let pieces = self.wrap_pieces(); let json = json!({ "pieces": pieces, "turn": self.turn, "player_turn": Self::wrap_player(self.next_player) }); String::from(json.to_string()) } pub fn get_pieces(&self) -> Vec<Piece> { return self.pieces.clone(); } pub fn get_turn(&self) -> u8 { return self.turn; } pub fn get_next_player(&self) -> Player { return self.next_player.clone(); } pub fn get_status(&self) -> GameStatus { if self.get_player_captured_count(Player::One) > 6 { return GameStatus::Win(Player::Two); } if self.get_player_captured_count(Player::Two) > 6 { return GameStatus::Win(Player::One); } GameStatus::Playing } pub fn submit(&mut self, game_move: GameMove) -> bool { if self.get_status() != GameStatus::Playing { return false; } let old_location = game_move.get_from(); let new_location = game_move.get_to(); let player = game_move.get_player(); let remove = game_move.get_remove(); if !self.is_valid_move(player, old_location, new_location) { return false; } if !self.is_valid_removal(player, remove) { return false; } self.pieces = self.get_updated_pieces(player, old_location, new_location); if self.is_three_in_a_row(player, new_location) { self.pieces = self.get_updated_with_removed(player, remove); } self.turn += 1; self.next_player = Self::switch_player(self.next_player); true } fn is_valid_move( &self, player: Player, old_location: Location, new_location: Location ) -> bool { if player != self.next_player { return false; } if old_location != Location::Hand && self.is_in_placement_phase() { return false; } if self.is_location_occupied(new_location) { return false; } if !self.is_in_placement_phase() && !self.is_next_door(old_location, new_location) { return false; } true } fn is_valid_removal(&self, player: Player, removal: Option<Location>) -> bool { match removal { None => true, Some(location) => { let other_player = Self::switch_player(player); !self.is_three_in_a_row(other_player, location) || !self.does_player_have_non_mill_pieces(other_player) } } } fn is_in_placement_phase(&self) -> bool { self.pieces.iter().any(|&piece| { piece.get_location() == Location::Hand }) } fn is_location_occupied(&self, new_location: Location) -> bool { self.pieces.iter().any(|&piece| { piece.get_location() == new_location }) } fn is_next_door(&self, new_location: Location, old_location: Location) -> bool { let rows = Location::get_rows(old_location).unwrap(); for (a, b) in rows { if a == new_location || b == new_location { return true; } } false } fn does_player_have_non_mill_pieces(&self, player: Player) -> bool { self.pieces.iter().filter(|&piece| { piece.get_player() == player && piece.get_location() != Location::Captured && piece.get_location() != Location::Hand && !self.is_three_in_a_row(player, piece.get_location()) }).count() > 0 } fn get_updated_pieces( &self, player: Player, old_location: Location, new_location: Location ) -> Vec<Piece> { let mut piece_moved = false; self.pieces.iter().map(|&piece| { if !piece_moved && piece.get_location() == old_location && piece.get_player() == player { piece_moved = true; Piece::new(player, new_location) } else { piece.clone() } }).collect() } fn get_updated_with_removed(&self, player: Player, remove: Option<Location>) -> Vec<Piece> { match remove { Some(location) => { self.pieces.iter().map(|&piece| { if piece.get_location() == location && piece.get_player() != player { Piece::new(piece.get_player(), Location::Captured) } else { piece.clone() } }).collect() }, _ => self.pieces.clone() } } pub fn is_three_in_a_row(&self, player: Player, new_location: Location) -> bool { let new_location_output = format!("{:?}", new_location); let rows = Location::get_rows(new_location).expect(&new_location_output); for (a, b) in rows { if self.does_piece_exist(a, player) && self.does_piece_exist(b, player) { return true; } } false } fn does_piece_exist(&self, location: Location, player: Player) -> bool { self.pieces.iter().any(|&piece| { piece.get_location() == location && piece.get_player() == player }) } fn get_player_captured_count(&self, player: Player) -> u8 { self.pieces.iter().filter(|&piece| { piece.get_location() == Location::Captured && piece.get_player() == player }).count() as u8 } fn wrap_pieces(&self) -> Vec<Value> { self.pieces.iter().map(|&piece| { json!({ "player": Self::wrap_player(piece.get_player()), "location": piece.get_location().to_str() }) }).collect() } fn get_new_pieces() -> Vec<Piece> { vec!( piece!(One, Hand), piece!(One, Hand), piece!(One, Hand), piece!(One, Hand), piece!(One, Hand), piece!(One, Hand), piece!(One, Hand), piece!(One, Hand), piece!(One, Hand), piece!(Two, Hand), piece!(Two, Hand), piece!(Two, Hand), piece!(Two, Hand), piece!(Two, Hand), piece!(Two, Hand), piece!(Two, Hand), piece!(Two, Hand), piece!(Two, Hand) ) } fn unwrap_pieces(v: Value) -> Vec<Piece> { v.as_array().unwrap().iter().map(|ref x| { Piece::new( Self::unwrap_player(x["player"].as_u64().unwrap()), Location::from_str(x["location"].as_str().unwrap()) ) }).collect() } fn unwrap_player(player: u64) -> Player { match player { 1 => Player::One, 2 => Player::Two, _ => Player::One } } fn wrap_player(player: Player) -> u64 { match player { Player::Two => 2, _ => 1 } } fn switch_player(player: Player) -> Player { match player { Player::One => Player::Two, _ => Player::One } } }