diff options
-rw-r--r-- | src/game.rs | 56 | ||||
-rw-r--r-- | tests/official-runner-matching.rs | 2 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/game.rs b/src/game.rs index dad72cd..0d57028 100644 --- a/src/game.rs +++ b/src/game.rs @@ -98,7 +98,49 @@ impl GameBoard { } pub fn simulate(&mut self, moves: [Command; 2]) -> SimulationOutcome { + // TODO: Command validation + // TODO: Move collision case + /*if let [Command::Move(x1, y1), Command::Move(x2, y2)] = moves { + + }*/ + + for player_index in 0..moves.len() { + if let Command::Move(x, y) = moves[player_index] { + let worm = self.players[player_index].active_worm_mut(); + worm.position.x = x; + worm.position.y = y; + } + } + + for player_index in 0..moves.len() { + if let Command::Dig(x, y) = moves[player_index] { + if let Some(c) = self.map.at(Point2d::new(x, y)) { + *c = CellType::Air; + } + } + } + + for player_index in 0..moves.len() { + if let Command::Shoot(dir) = moves[player_index] { + // TODO: Shoot + } + } + for player in &mut self.players { + // Remove dead worms + for worm_index in (0..player.worms.len()).rev() { + if player.worms[worm_index].health <= 0 { + player.worms.remove(worm_index); + if player.active_worm >= worm_index { + if player.active_worm > 0 { + player.active_worm -= 1; + } else { + player.active_worm = player.worms.len()-1; + } + } + } + } + // Update the active worm player.active_worm = (player.active_worm + 1) % player.worms.len(); } SimulationOutcome::Continue @@ -111,4 +153,18 @@ impl Player { .iter() .find(|w| w.id == id) } + + fn active_worm_mut(&mut self) -> &mut Worm { + &mut self.worms[self.active_worm] + } +} + +impl Map { + fn at(&mut self, p: Point2d<i8>) -> Option<&mut CellType> { + if p.y < 0 || p.x < 0 || p.y as u8 >= self.size || p.x as u8 >= self.size { + None + } else { + Some(&mut self.cells[p.y as usize * self.size as usize + p.x as usize]) + } + } } diff --git a/tests/official-runner-matching.rs b/tests/official-runner-matching.rs index 12b6010..e5602a2 100644 --- a/tests/official-runner-matching.rs +++ b/tests/official-runner-matching.rs @@ -8,6 +8,8 @@ use std::io::prelude::*; #[test] fn simulates_the_same_match() { + // TODO: Also assert map state + let replays = Path::new("tests/replays/"); for replay in replays.read_dir().expect("read_dir failed") { let replay = replay.expect("error on replay").path(); |