From 510767263a0060ad13b2488a9402b1d176ad65ef Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 25 Apr 2019 16:50:06 +0200 Subject: Test that match replay matches my simulation --- src/command.rs | 4 ++-- src/game.rs | 47 +++++++++++++++++++++++++++++------------------ src/json.rs | 3 ++- src/lib.rs | 5 +++++ src/main.rs | 15 ++++++--------- 5 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 src/lib.rs (limited to 'src') diff --git a/src/command.rs b/src/command.rs index 99de608..a510120 100644 --- a/src/command.rs +++ b/src/command.rs @@ -3,8 +3,8 @@ use crate::geometry::Direction; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Command { - Move(u32, u32), - Dig(u32, u32), + Move(i8, i8), + Dig(i8, i8), Shoot(Direction), DoNothing, } diff --git a/src/game.rs b/src/game.rs index 626a377..dad72cd 100644 --- a/src/game.rs +++ b/src/game.rs @@ -3,42 +3,42 @@ use crate::command::Command; use crate::json; pub struct GameBoard { - players: [Player; 2], - powerups: Vec, - map: Map, + pub players: [Player; 2], + pub powerups: Vec, + pub map: Map, } -struct Player { - active_worm: usize, - worms: Vec +pub struct Player { + pub active_worm: usize, + pub worms: Vec } -struct Worm { - id: i32, - health: i32, - position: Point2d, - weapon_damage: i32, - weapon_range: u8 +pub struct Worm { + pub id: i32, + pub health: i32, + pub position: Point2d, + pub weapon_damage: i32, + pub weapon_range: u8 } -enum Powerup { +pub enum Powerup { Health(Point2d, i32) } -struct Map { - size: u8, +pub struct Map { + pub size: u8, /// This is 2d, each row is size long - cells: Vec + pub cells: Vec } -enum CellType { +pub enum CellType { Air, Dirt, DeepSpace, } -enum SimulationOutcome { +pub enum SimulationOutcome { PlayerWon(usize), Draw, Continue, @@ -98,6 +98,17 @@ impl GameBoard { } pub fn simulate(&mut self, moves: [Command; 2]) -> SimulationOutcome { + for player in &mut self.players { + player.active_worm = (player.active_worm + 1) % player.worms.len(); + } SimulationOutcome::Continue } } + +impl Player { + pub fn find_worm(&self, id: i32) -> Option<&Worm> { + self.worms + .iter() + .find(|w| w.id == id) + } +} diff --git a/src/json.rs b/src/json.rs index 5a8267f..979252e 100644 --- a/src/json.rs +++ b/src/json.rs @@ -1,11 +1,12 @@ use std::error::Error; use std::fs::File; use std::io::prelude::*; +use std::path::Path; use serde::{Deserialize, Serialize}; use serde_json; -pub fn read_state_from_json_file(filename: &str) -> Result> { +pub fn read_state_from_json_file(filename: &Path) -> Result> { let mut file = File::open(filename)?; let mut content = String::new(); file.read_to_string(&mut content)?; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c0a6cd6 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +pub mod command; +pub mod json; +pub mod geometry; +pub mod game; +pub mod strategy; diff --git a/src/main.rs b/src/main.rs index 34d0061..d6d9a4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,11 @@ use std::io::prelude::*; use std::io::stdin; +use std::path::Path; -mod command; -mod json; -mod geometry; -mod game; -mod strategy; - -use command::Command; -use strategy::choose_move; +use steam_powered_wyrm::command::Command; +use steam_powered_wyrm::strategy::choose_move; +use steam_powered_wyrm::json; +use steam_powered_wyrm::game; fn main() { let mut game_board = None; @@ -16,7 +13,7 @@ fn main() { let round_number = line.expect("Failed to read line from stdin: {}"); let command = - match json::read_state_from_json_file(&format!("./rounds/{}/state.json", round_number)) { + match json::read_state_from_json_file(&Path::new(&format!("./rounds/{}/state.json", round_number))) { Ok(json_state) => { match &mut game_board { None => { -- cgit v1.2.3