use crate::consts::*; #[derive(serde::Serialize)] pub struct PlayConfiguration { min_boat_len: usize, max_boat_len: usize, min_map_width: usize, max_map_width: usize, min_map_height: usize, max_map_height: usize, min_boats_number: usize, max_boats_number: usize, } impl Default for PlayConfiguration { fn default() -> Self { Self { min_boat_len: MIN_BOATS_LENGTH, max_boat_len: MAX_BOATS_LENGTH, min_map_width: MIN_MAP_WIDTH, max_map_width: MAX_MAP_WIDTH, min_map_height: MIN_MAP_HEIGHT, max_map_height: MAX_MAP_HEIGHT, min_boats_number: MIN_BOATS_NUMBER, max_boats_number: MAX_BOATS_NUMBER, } } } #[derive(serde::Serialize, serde::Deserialize)] pub struct GameRules { pub map_width: usize, pub map_height: usize, pub boats: Vec, pub boats_can_touch: bool, pub player_continue_on_hit: bool, } impl GameRules { pub fn multi_players_rules() -> Self { Self { map_width: MULTI_PLAYER_MAP_WIDTH, map_height: MULTI_PLAYER_MAP_HEIGHT, boats: MULTI_PLAYER_PLAYER_BOATS.to_vec(), boats_can_touch: MULTI_PLAYER_BOATS_CAN_TOUCH, player_continue_on_hit: MULTI_PLAYER_PLAYER_CAN_CONTINUE_AFTER_HIT, } } pub fn get_errors(&self) -> Vec<&str> { let config = PlayConfiguration::default(); let mut errors = vec![]; if self.map_width < config.min_map_width || self.map_width > config.max_map_width { errors.push("Map width is outside bounds!"); } if self.map_height < config.min_map_height || self.map_height > config.max_map_height { errors.push("Map height is outside bounds!"); } if self.boats.len() < config.min_boat_len || self.boats.len() > config.max_boat_len { errors.push("Number of boats is invalid!"); } errors } } #[cfg(test)] mod test { use crate::data::GameRules; #[test] fn multi_players_config() { assert!(GameRules::multi_players_rules().get_errors().is_empty()); } }