From c7bfdb8d741fcdff20e13982d1d8a1631be74aa5 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 8 Oct 2022 10:35:14 +0200 Subject: [PATCH] Show neighbor cells if boats can't touch --- rust/cli_player/src/main.rs | 4 +++- .../src/ui_screens/set_boats_layout.rs | 19 +++++++++++++++++-- .../src/data/boats_layout.rs | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/rust/cli_player/src/main.rs b/rust/cli_player/src/main.rs index 3b48772..de20e65 100644 --- a/rust/cli_player/src/main.rs +++ b/rust/cli_player/src/main.rs @@ -19,7 +19,9 @@ use sea_battle_backend::data::GameRules; async fn run_app(terminal: &mut Terminal) -> Result<(), Box> { // Temporary code // let res = configure_game_rules::configure_play_rules(GameRules::default(), terminal)?; // select_bot_type::select_bot_type(terminal)?; - let res = set_boats_layout::set_boat_layout(&GameRules::default(), terminal)?; // select_bot_type::select_bot_type(terminal)?; + let mut rules = GameRules::default(); + rules.boats_can_touch = true; + let res = set_boats_layout::set_boat_layout(&rules, terminal)?; // select_bot_type::select_bot_type(terminal)?; Err(io::Error::new( ErrorKind::Other, format!("result: {:?}", res), diff --git a/rust/cli_player/src/ui_screens/set_boats_layout.rs b/rust/cli_player/src/ui_screens/set_boats_layout.rs index 64433a5..6693694 100644 --- a/rust/cli_player/src/ui_screens/set_boats_layout.rs +++ b/rust/cli_player/src/ui_screens/set_boats_layout.rs @@ -146,7 +146,7 @@ fn ui( } let other_boats = ColoredCells { - color: Color::DarkGray, + color: Color::Gray, cells: other_boats_cells, }; @@ -160,7 +160,7 @@ fn ui( legend.push_str("Enter confirm layout"); } - let game_map_widget = GameMapWidget::new(rules) + let mut game_map_widget = GameMapWidget::new(rules) .set_default_empty_char(' ') .add_colored_cells(current_boat) .add_colored_cells(other_boats) @@ -174,6 +174,21 @@ fn ui( }) .set_legend(legend); + // Colorate neighbors if boats can not touch + if !rules.boats_can_touch { + let mut boats_neighbors_cells = vec![]; + for boat in &model.layout.0 { + for pos in boat.neighbor_coordinates(rules) { + boats_neighbors_cells.push(pos); + } + } + + game_map_widget = game_map_widget.add_colored_cells(ColoredCells { + color: Color::Rgb(30, 30, 30), + cells: boats_neighbors_cells, + }); + } + let (w, h) = game_map_widget.estimated_size(); let area = centered_rect_size(w, h, &f.size()); f.render_widget(game_map_widget, area); diff --git a/rust/sea_battle_backend/src/data/boats_layout.rs b/rust/sea_battle_backend/src/data/boats_layout.rs index b70d52f..7d79fe2 100644 --- a/rust/sea_battle_backend/src/data/boats_layout.rs +++ b/rust/sea_battle_backend/src/data/boats_layout.rs @@ -318,7 +318,7 @@ impl BoatsLayout { .iter() .any(|c| boat_j_coords.contains(c)) { - errors.push("A collision between two boats has been detected!"); + errors.push("Two boats are touching!"); } } }