Add support for linear bots

This commit is contained in:
2022-09-17 12:02:13 +02:00
parent 6efaa81ecb
commit d1e8efc764
18 changed files with 344 additions and 72 deletions

View File

@@ -284,7 +284,7 @@ impl BoatsLayout {
mod test {
use crate::data::boats_layout::{BoatDirection, BoatPosition, BoatsLayout, Coordinates};
use crate::data::game_map::GameMap;
use crate::data::{BotType, GameRules, PlayConfiguration};
use crate::data::{BotType, GameRules, PlayConfiguration, PrintableMap};
#[test]
fn get_boat_coordinates() {

View File

@@ -1,8 +1,10 @@
use rand::RngCore;
use crate::data::{BoatPosition, BoatsLayout, Coordinates, GameRules};
use crate::data::{
BoatPosition, BoatsLayout, Coordinates, GameRules, MapCellContent, PrintableMap,
};
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
pub struct CurrentGameMapStatus {
pub boats: BoatsLayout,
pub successful_strikes: Vec<Coordinates>,
@@ -10,7 +12,46 @@ pub struct CurrentGameMapStatus {
pub sunk_boats: Vec<BoatPosition>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
impl CurrentGameMapStatus {
pub fn did_fire_at_location(&self, c: Coordinates) -> bool {
self.successful_strikes.contains(&c) || self.failed_strikes.contains(&c)
}
}
struct PrintableCurrentGameMapStatus(GameRules, CurrentGameMapStatus);
impl PrintableMap for PrintableCurrentGameMapStatus {
fn map_cell_content(&self, c: Coordinates) -> MapCellContent {
if !c.is_valid(&self.0) {
return MapCellContent::Invalid;
}
if self.1.failed_strikes.contains(&c) {
return MapCellContent::FailedStrike;
}
if self
.1
.sunk_boats
.iter()
.any(|b| b.all_coordinates().contains(&c))
{
return MapCellContent::SunkBoat;
}
if self.1.successful_strikes.contains(&c) {
return MapCellContent::TouchedBoat;
}
if self.1.boats.find_boat_at_position(c).is_some() {
return MapCellContent::Boat;
}
MapCellContent::Nothing
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
pub struct CurrentGameStatus {
pub rules: GameRules,
pub your_map: CurrentGameMapStatus,
@@ -37,4 +78,26 @@ impl CurrentGameStatus {
}
}
}
/// Find valid linear fire location. Loop until one is found
pub fn find_first_valid_fire_location(&self) -> Coordinates {
for y in 0..self.rules.map_height {
for x in 0..self.rules.map_width {
let coordinates = Coordinates::new(x as i32, y as i32);
if self.can_fire_at_location(coordinates) {
return coordinates;
}
}
}
panic!("Could not find fire location!")
}
pub fn print_your_map(&self) {
PrintableCurrentGameMapStatus(self.rules.clone(), self.your_map.clone()).print_map()
}
pub fn print_opponent_map(&self) {
PrintableCurrentGameMapStatus(self.rules.clone(), self.opponent_map.clone()).print_map()
}
}

View File

@@ -1,5 +1,7 @@
use crate::data::boats_layout::{BoatsLayout, Coordinates};
use crate::data::{BoatPosition, CurrentGameMapStatus, EndGameMap, GameRules};
use crate::data::{
BoatPosition, CurrentGameMapStatus, EndGameMap, GameRules, MapCellContent, PrintableMap,
};
#[derive(Debug, Copy, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum FireResult {
@@ -10,29 +12,6 @@ pub enum FireResult {
AlreadyTargetedPosition,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum MapCellContent {
Invalid,
Nothing,
TouchedBoat,
SunkBoat,
Boat,
FailedStrike,
}
impl MapCellContent {
pub fn letter(&self) -> &'static str {
match self {
MapCellContent::Invalid => "!",
MapCellContent::Nothing => ".",
MapCellContent::TouchedBoat => "T",
MapCellContent::SunkBoat => "S",
MapCellContent::Boat => "B",
MapCellContent::FailedStrike => "x",
}
}
}
pub struct GameMap {
rules: GameRules,
boats_config: BoatsLayout,
@@ -120,19 +99,6 @@ impl GameMap {
self.sunk_boats.len() == self.boats_config.number_of_boats()
}
pub fn print_map(&self) {
for y in 0..self.rules.map_height {
for x in 0..self.rules.map_width {
print!(
"{} ",
self.get_cell_content(Coordinates::new(x as i32, y as i32))
.letter()
);
}
println!();
}
}
pub fn current_map_status(&self, for_opponent: bool) -> CurrentGameMapStatus {
CurrentGameMapStatus {
boats: match for_opponent {
@@ -158,3 +124,9 @@ impl GameMap {
}
}
}
impl PrintableMap for GameMap {
fn map_cell_content(&self, c: Coordinates) -> MapCellContent {
self.get_cell_content(c)
}
}

View File

@@ -27,6 +27,11 @@ impl GameRules {
}
}
pub fn with_bot_type(mut self, t: BotType) -> Self {
self.bot_type = t;
self
}
/// Set the list of boats for this configuration
pub fn set_boats_list(&mut self, boats: &[usize]) {
self.boats_str = boats

View File

@@ -4,6 +4,7 @@ pub use end_game_map::*;
pub use game_map::*;
pub use game_rules::*;
pub use play_config::*;
pub use printable_map::*;
mod boats_layout;
mod current_game_status;
@@ -11,3 +12,4 @@ mod end_game_map;
mod game_map;
mod game_rules;
mod play_config;
mod printable_map;

View File

@@ -4,7 +4,7 @@ use crate::consts::*;
#[derive(serde::Serialize, serde::Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
pub enum BotType {
Random,
// TODO : LinearShooting
Linear,
// TODO : GridBot
// TODO : SmartBot
}
@@ -40,10 +40,16 @@ impl Default for PlayConfiguration {
max_map_height: MAX_MAP_HEIGHT,
min_boats_number: MIN_BOATS_NUMBER,
max_boats_number: MAX_BOATS_NUMBER,
bot_types: vec![BotDescription {
r#type: BotType::Random,
description: "Random strike. All the time.".to_string(),
}],
bot_types: vec![
BotDescription {
r#type: BotType::Linear,
description: "Linear strike. Shoot A1, A2, A3, ..., B1, B2, ...".to_string(),
},
BotDescription {
r#type: BotType::Random,
description: "Random strike. All the time.".to_string(),
},
],
ordinate_alphabet: ALPHABET,
}
}

View File

@@ -0,0 +1,55 @@
use crate::data::Coordinates;
#[derive(Debug, Copy, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum MapCellContent {
Invalid,
Nothing,
TouchedBoat,
SunkBoat,
Boat,
FailedStrike,
}
impl MapCellContent {
pub fn letter(&self) -> &'static str {
match self {
MapCellContent::Invalid => "!",
MapCellContent::Nothing => ".",
MapCellContent::TouchedBoat => "T",
MapCellContent::SunkBoat => "S",
MapCellContent::Boat => "B",
MapCellContent::FailedStrike => "x",
}
}
}
pub trait PrintableMap {
fn map_cell_content(&self, c: Coordinates) -> MapCellContent;
fn print_map(&self) {
let mut y = 0;
let mut x;
loop {
x = 0;
loop {
let content = self.map_cell_content(Coordinates::new(x, y));
if content == MapCellContent::Invalid {
break;
}
print!("{} ", content.letter());
x += 1;
}
println!();
// x == 0 <=> we reached the end of the map
if x == 0 {
break;
}
y += 1;
}
}
}