Struct battleship_bot::Battleship
source · pub struct Battleship { /* private fields */ }
Expand description
Handles the games
Implementations§
source§impl Battleship
impl Battleship
sourcepub fn new(
player1_place_fn: fn() -> BoatMap,
player2_place_fn: fn() -> BoatMap,
player1_shoot_fn: fn(_: Pos, _: ShotMap) -> (Pos, bool),
player2_shoot_fn: fn(_: Pos, _: ShotMap) -> (Pos, bool)
) -> Self
pub fn new( player1_place_fn: fn() -> BoatMap, player2_place_fn: fn() -> BoatMap, player1_shoot_fn: fn(_: Pos, _: ShotMap) -> (Pos, bool), player2_shoot_fn: fn(_: Pos, _: ShotMap) -> (Pos, bool) ) -> Self
sourcepub fn play_and_record_game(&mut self) -> Recording
pub fn play_and_record_game(&mut self) -> Recording
This function allows you to record a game and get data from it.
It will return a Recording
struct, which has a lot of handy features.
Example
use battleship_bot::Battleship;
use battleship_bot::{place, shoot};
let mut battleship = Battleship::new(
place::random,
place::random,
shoot::random,
shoot::grid_and_destroy,
);
let recording = battleship.play_and_record_game();
// Small chance this assertion will fail, but it's really small
assert_ne!(recording.player1_boats, recording.player2_boats);
println!("Player {} won!", recording.winner);
sourcepub fn play_games(&mut self, num_games: usize) -> (usize, usize)
pub fn play_games(&mut self, num_games: usize) -> (usize, usize)
This function allows you to have the bots play many games. The only parameter is a usize, and that is the number of games the bots will play agains each other. It will return a tuple of player 1 wins and player 2 wins, respectively.
Example
use battleship_bot::Battleship;
use battleship_bot::{place, shoot};
let mut battleship = Battleship::new(
place::random,
place::random,
shoot::random,
shoot::grid_and_destroy,
);
// When playing multiple games, it will return a tuple of player 1 wins and player 2 wins
let (p1_wins, p2_wins) = battleship.play_games(1000);
assert!(p2_wins > p1_wins);
sourcepub fn save_games(
place_fns: Vec<(&'static str, fn() -> BoatMap)>,
shoot_fns: Vec<(&'static str, fn(_: Pos, _: ShotMap) -> (Pos, bool))>,
games_per_comb: usize,
filename: &str
)
pub fn save_games( place_fns: Vec<(&'static str, fn() -> BoatMap)>, shoot_fns: Vec<(&'static str, fn(_: Pos, _: ShotMap) -> (Pos, bool))>, games_per_comb: usize, filename: &str )
Saves games from the inputs
Saves games against all possible combinations of place_fns and shoot_fns. It runs games_per_comb for each combination of place_fns and shoot_fns. After all the games it stores the data in filename, which is a csv file.
Trait Implementations§
source§impl Clone for Battleship
impl Clone for Battleship
source§fn clone(&self) -> Battleship
fn clone(&self) -> Battleship
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl RefUnwindSafe for Battleship
impl Send for Battleship
impl Sync for Battleship
impl Unpin for Battleship
impl UnwindSafe for Battleship
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more