summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2019-04-25 17:21:09 +0200
committerJustin Worthe <justin@worthe-it.co.za>2019-04-25 17:21:09 +0200
commit4978b4f27d7a0e6058d04e7184600bf835070a8b (patch)
treebe3a5618fef822ca349f0179abeb35b5b2083115
parent510767263a0060ad13b2488a9402b1d176ad65ef (diff)
Implement move and dig commands
-rw-r--r--src/game.rs56
-rw-r--r--tests/official-runner-matching.rs2
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();