diff --git a/rust/cli_player/src/ui_screens/game_screen.rs b/rust/cli_player/src/ui_screens/game_screen.rs index 527ef21..fd76d50 100644 --- a/rust/cli_player/src/ui_screens/game_screen.rs +++ b/rust/cli_player/src/ui_screens/game_screen.rs @@ -220,7 +220,7 @@ impl GameScreen { } fn player_map(&self, map: &CurrentGameMapStatus, opponent_map: bool) -> GameMapWidget { - let mut map_widget = GameMapWidget::new(&self.game.rules); + let mut map_widget = GameMapWidget::new(&self.game.rules).set_default_empty_char(' '); // Current shoot position if opponent_map { @@ -241,8 +241,14 @@ impl GameScreen { } // Sunk boats + for b in &map.sunk_boats { + for c in b.all_coordinates() { + map_widget = + map_widget.set_char(c, b.len.to_string().chars().next().unwrap_or('9')); + } + } let sunk_boats = ColoredCells { - color: Color::Gray, + color: Color::LightRed, cells: map .sunk_boats .iter() @@ -251,18 +257,29 @@ impl GameScreen { }; // Touched boats + for b in &map.successful_strikes { + map_widget = map_widget.set_char_no_overwrite(*b, 'T'); + } let touched_areas = ColoredCells { color: Color::Red, cells: map.successful_strikes.clone(), }; // Failed strikes + for b in &map.failed_strikes { + map_widget = map_widget.set_char_no_overwrite(*b, '.'); + } let failed_strikes = ColoredCells { - color: Color::DarkGray, + color: Color::Black, cells: map.failed_strikes.clone(), }; // Boats + for b in &map.boats.0 { + for c in b.all_coordinates() { + map_widget = map_widget.set_char_no_overwrite(c, 'B'); + } + } let boats = ColoredCells { color: Color::Blue, cells: map diff --git a/rust/cli_player/src/ui_widgets/game_map_widget.rs b/rust/cli_player/src/ui_widgets/game_map_widget.rs index d10dab0..32b0ac9 100644 --- a/rust/cli_player/src/ui_widgets/game_map_widget.rs +++ b/rust/cli_player/src/ui_widgets/game_map_widget.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::fmt::Display; use tui::buffer::Buffer; @@ -21,6 +22,7 @@ pub struct GameMapWidget<'a> { title: Option, legend: Option, yield_coordinates: Option>, + chars: HashMap, } impl<'a> GameMapWidget<'a> { @@ -32,6 +34,7 @@ impl<'a> GameMapWidget<'a> { title: None, legend: None, yield_coordinates: None, + chars: Default::default(), } } @@ -63,6 +66,16 @@ impl<'a> GameMapWidget<'a> { self } + pub fn set_char(mut self, coordinates: Coordinates, c: char) -> Self { + self.chars.insert(coordinates, c); + self + } + + pub fn set_char_no_overwrite(mut self, coordinates: Coordinates, c: char) -> Self { + self.chars.entry(coordinates).or_insert(c); + self + } + pub fn grid_size(&self) -> (u16, u16) { let w = self.rules.map_width as u16 * 2 + 1; let h = self.rules.map_height as u16 * 2 + 1; @@ -157,9 +170,12 @@ impl<'a> Widget for GameMapWidget<'a> { } if x < self.rules.map_width && y < self.rules.map_height { - let cell = buf - .get_mut(o_x + 1, o_y + 1) - .set_char(self.default_empty_character); + let cell = buf.get_mut(o_x + 1, o_y + 1).set_char( + *self + .chars + .get(&coordinates) + .unwrap_or(&self.default_empty_character), + ); if let Some(c) = color { cell.set_bg(c.color);