Show neighbor cells if boats can't touch

This commit is contained in:
Pierre HUBERT 2022-10-08 10:35:14 +02:00
parent 6e423f41ef
commit c7bfdb8d74
3 changed files with 21 additions and 4 deletions

View File

@ -19,7 +19,9 @@ use sea_battle_backend::data::GameRules;
async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn Error>> { async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn Error>> {
// Temporary code // Temporary code
// let res = configure_game_rules::configure_play_rules(GameRules::default(), terminal)?; // select_bot_type::select_bot_type(terminal)?; // 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( Err(io::Error::new(
ErrorKind::Other, ErrorKind::Other,
format!("result: {:?}", res), format!("result: {:?}", res),

View File

@ -146,7 +146,7 @@ fn ui<B: Backend>(
} }
let other_boats = ColoredCells { let other_boats = ColoredCells {
color: Color::DarkGray, color: Color::Gray,
cells: other_boats_cells, cells: other_boats_cells,
}; };
@ -160,7 +160,7 @@ fn ui<B: Backend>(
legend.push_str("Enter confirm layout"); 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(' ') .set_default_empty_char(' ')
.add_colored_cells(current_boat) .add_colored_cells(current_boat)
.add_colored_cells(other_boats) .add_colored_cells(other_boats)
@ -174,6 +174,21 @@ fn ui<B: Backend>(
}) })
.set_legend(legend); .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 (w, h) = game_map_widget.estimated_size();
let area = centered_rect_size(w, h, &f.size()); let area = centered_rect_size(w, h, &f.size());
f.render_widget(game_map_widget, area); f.render_widget(game_map_widget, area);

View File

@ -318,7 +318,7 @@ impl BoatsLayout {
.iter() .iter()
.any(|c| boat_j_coords.contains(c)) .any(|c| boat_j_coords.contains(c))
{ {
errors.push("A collision between two boats has been detected!"); errors.push("Two boats are touching!");
} }
} }
} }