Players get live game status updates
This commit is contained in:
@@ -151,6 +151,11 @@ impl BoatPosition {
|
||||
pub struct BoatsLayout(Vec<BoatPosition>);
|
||||
|
||||
impl BoatsLayout {
|
||||
/// Generate a new invalid (empty) boats layout
|
||||
pub fn new_invalid() -> Self {
|
||||
Self(vec![])
|
||||
}
|
||||
|
||||
/// Generate random boats layout for given game rules
|
||||
pub fn gen_random_for_rules(rules: &GameRules) -> std::io::Result<Self> {
|
||||
let mut boats = Self(Vec::with_capacity(rules.boats_list().len()));
|
||||
|
||||
@@ -1,17 +1,27 @@
|
||||
use crate::data::{Coordinates, GameRules};
|
||||
use rand::RngCore;
|
||||
|
||||
use crate::data::{BoatPosition, BoatsLayout, Coordinates, GameRules};
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct CurrentGameMapStatus {
|
||||
pub boats: BoatsLayout,
|
||||
pub successful_strikes: Vec<Coordinates>,
|
||||
pub failed_strikes: Vec<Coordinates>,
|
||||
pub sunk_boats: Vec<BoatPosition>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct CurrentGameStatus {
|
||||
pub rules: GameRules,
|
||||
//TODO
|
||||
pub your_map: CurrentGameMapStatus,
|
||||
pub opponent_map: CurrentGameMapStatus,
|
||||
}
|
||||
|
||||
impl CurrentGameStatus {
|
||||
/// Check if user can fire at a given location
|
||||
pub fn can_fire_at_location(&self, _location: Coordinates) -> bool {
|
||||
//TODO
|
||||
true
|
||||
/// Check if opponent can fire at a given location
|
||||
pub fn can_fire_at_location(&self, location: Coordinates) -> bool {
|
||||
!self.opponent_map.successful_strikes.contains(&location)
|
||||
&& !self.opponent_map.failed_strikes.contains(&location)
|
||||
}
|
||||
|
||||
/// Find valid random fire location. Loop until one is found
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::data::boats_layout::{BoatsLayout, Coordinates};
|
||||
use crate::data::{BoatPosition, EndGameMap, GameRules};
|
||||
use crate::data::{BoatPosition, CurrentGameMapStatus, EndGameMap, GameRules};
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
pub enum FireResult {
|
||||
@@ -133,6 +133,18 @@ impl GameMap {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_map_status(&self, for_opponent: bool) -> CurrentGameMapStatus {
|
||||
CurrentGameMapStatus {
|
||||
boats: match for_opponent {
|
||||
true => BoatsLayout::new_invalid(),
|
||||
false => self.boats_config.clone(),
|
||||
},
|
||||
successful_strikes: self.successful_strikes.clone(),
|
||||
failed_strikes: self.failed_strikes.clone(),
|
||||
sunk_boats: self.sunk_boats.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn final_map(&self) -> EndGameMap {
|
||||
EndGameMap {
|
||||
boats: self.boats_config.clone(),
|
||||
|
||||
Reference in New Issue
Block a user