diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2017-05-13 19:19:06 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2017-05-13 19:19:06 +0200 |
commit | 36b72bfef7b7b8dea94546d11704ec529091bce1 (patch) | |
tree | 4a8cdae81db3ab44aa946046bbfbb87f96c360c5 /src/lib.rs | |
parent | 27682d0ab246af8d0375853fdea44c38de2c2db4 (diff) |
Split into smaller portions
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5ea9ecc --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,55 @@ +extern crate json; +extern crate rand; + +mod actions; +mod math; +mod files; +mod ships; + +use actions::*; +use math::*; +use files::*; +use ships::*; + +use std::path::PathBuf; + +pub fn write_move(working_dir: PathBuf) -> Result<(), String> { + let state = read_file(&working_dir)?; + + let is_place_phase = state["Phase"] == 1; + let map_dimension = state["MapDimension"] + .as_u16() + .ok_or("Did not find the map dimension in the state json file")?; + + let action = if is_place_phase { + place_ships() + } + else { + shoot_randomly(map_dimension) + }; + + write_action(&working_dir, is_place_phase, action) + .map_err(|e| format!("Failed to write action to file. Error: {}", e))?; + + Ok(()) +} + + +fn place_ships() -> Action { + let ships = vec!( + (Ship::Battleship, Point::new(1, 0), Orientation::North), + (Ship::Carrier, Point::new(3, 1), Orientation::East), + (Ship::Cruiser, Point::new(4, 2), Orientation::North), + (Ship::Destroyer, Point::new(7, 3), Orientation::North), + (Ship::Submarine, Point::new(1, 8), Orientation::East) + ); + + Action::PlaceShips(ships) +} + +fn shoot_randomly(map_dimension: u16) -> Action { + Action::Shoot(random_point(map_dimension)) +} + + + |