pub struct Battleship { /* private fields */ }
Expand description

Handles the games

Implementations§

source§

impl Battleship

source

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

This function is used to create the game. The parameters are basically what they are named.

player1_place_fn and player2_place_fn can both be any function from place.

player1_shoot_fn and player2_shoot_fn can both be any function from shoot.

source

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);
source

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);
source

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

source§

fn clone(&self) -> Battleship

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V