diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game.rs | 19 | ||||
-rw-r--r-- | src/game/player.rs | 23 | ||||
-rw-r--r-- | src/json.rs | 7 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/strategy.rs | 1 |
5 files changed, 42 insertions, 10 deletions
diff --git a/src/game.rs b/src/game.rs index a4a4a23..ffa8cb9 100644 --- a/src/game.rs +++ b/src/game.rs @@ -40,18 +40,20 @@ impl GameBoard { let player = Player { active_worm: json.active_worm_index().unwrap(), moves_score: json.my_player.score - json.my_player.health_score(), + select_moves: json.my_player.remaining_worm_selections, worms: json.my_player.worms.iter().map(|w| Worm { id: w.id, health: w.health, position: Point2d::new(w.position.x, w.position.y), weapon_damage: commando_damage, - weapon_range: commando_range - }).collect() + weapon_range: commando_range, + }).collect(), }; let opponent = Player { active_worm: 0, moves_score: json.opponents[0].score - json.opponents[0].health_score(), + select_moves: json.opponents[0].remaining_worm_selections, worms: json.opponents.iter().flat_map(|o| &o.worms).map(|w| Worm { id: w.id, health: w.health, @@ -138,7 +140,7 @@ impl GameBoard { pub fn simulate(&mut self, moves: [Command; 2]) { let actions = [moves[0].action, moves[1].action]; - // TODO: simulate_select + self.simulate_select(moves); self.simulate_moves(actions); self.simulate_digs(actions); // TODO: simulate_bombs @@ -160,6 +162,17 @@ impl GameBoard { }; } + fn simulate_select(&mut self, moves: [Command; 2]) { + moves.iter().zip(self.players.iter_mut()) + .for_each(|(m, player)| { + if let Some(worm) = m.worm { + debug_assert!(player.select_moves > 0, "Could not make select move, out of select tokens"); + player.select_moves = player.select_moves.saturating_sub(1); + player.active_worm = player.find_worm_position(worm).unwrap_or(0); + } + }); + } + fn simulate_moves(&mut self, actions: [Action; 2]) { match actions { [Action::Move(p1), Action::Move(p2)] if p1.x == p2.x && p1.y == p2.y => { diff --git a/src/game/player.rs b/src/game/player.rs index 1704a27..32e45bf 100644 --- a/src/game/player.rs +++ b/src/game/player.rs @@ -5,7 +5,8 @@ use crate::geometry::*; pub struct Player { pub moves_score: i32, pub active_worm: usize, - pub worms: ArrayVec<[Worm; 3]> + pub select_moves: u8, + pub worms: ArrayVec<[Worm; 3]>, } #[derive(Debug, PartialEq, Eq, Clone)] @@ -14,10 +15,16 @@ pub struct Worm { pub health: i32, pub position: Point2d<i8>, pub weapon_damage: i32, - pub weapon_range: u8 + pub weapon_range: u8, } impl Player { + pub fn find_worm_position(&self, id: i32) -> Option<usize> { + self.worms + .iter() + .position(|w| w.id == id) + } + pub fn find_worm(&self, id: i32) -> Option<&Worm> { self.worms .iter() @@ -106,7 +113,8 @@ mod test { let mut player = Player { active_worm: 1, moves_score: 0, - worms: worms + select_moves: 0, + worms }; player.clear_dead_worms(); @@ -145,7 +153,8 @@ mod test { let mut player = Player { active_worm: 1, moves_score: 0, - worms: worms + worms, + select_moves: 0, }; player.clear_dead_worms(); @@ -184,7 +193,8 @@ mod test { let mut player = Player { active_worm: 0, moves_score: 0, - worms: worms + worms, + select_moves: 0, }; player.clear_dead_worms(); @@ -209,7 +219,8 @@ mod test { let mut player = Player { active_worm: 0, moves_score: 0, - worms: worms + worms, + select_moves: 0 }; player.clear_dead_worms(); diff --git a/src/json.rs b/src/json.rs index 09095b0..252481c 100644 --- a/src/json.rs +++ b/src/json.rs @@ -35,6 +35,7 @@ pub struct Player { pub score: i32, pub health: i32, pub worms: Vec<PlayerWorm>, + pub remaining_worm_selections: u8, } impl Player { @@ -60,6 +61,7 @@ pub struct Opponent { pub id: i32, pub score: i32, pub worms: Vec<OpponentWorm>, + pub remaining_worm_selections: u8, } impl Opponent { @@ -177,6 +179,8 @@ mod test { "id": 1, "score": 100, "health": 300, + "currentWormId": 1, + "remainingWormSelections": 1, "worms": [ { "id": 1, @@ -198,6 +202,7 @@ mod test { { "id": 2, "score": 100, + "remainingWormSelections": 2, "worms": [ { "id": 1, @@ -290,6 +295,7 @@ mod test { id: 1, score: 100, health: 300, + remaining_worm_selections: 1, worms: vec![PlayerWorm { id: 1, health: 100, @@ -305,6 +311,7 @@ mod test { opponents: vec![Opponent { id: 2, score: 100, + remaining_worm_selections: 2, worms: vec![OpponentWorm { id: 1, health: 100, @@ -1,3 +1,5 @@ +#![warn(clippy::all)] + pub mod command; pub mod json; pub mod geometry; diff --git a/src/strategy.rs b/src/strategy.rs index 31a1bee..4c86dfd 100644 --- a/src/strategy.rs +++ b/src/strategy.rs @@ -1,6 +1,5 @@ use crate::command::{Command, Action}; use crate::game::{GameBoard, SimulationOutcome}; -use crate::geometry::*; use std::cmp; use std::collections::HashMap; |