Struct lib_battleship::PreGame
[−]
[src]
pub struct PreGame { /* fields omitted */ }
Builder type for a game of battleship.
Methods
impl PreGame
[src]
Builder style struct for battleship.
fn new(width: Dimension, height: Dimension) -> Result<PreGame, GameError>
Creates a new instance.
Parameters
width
The number of columnsheight
The number of lines
Errors
IllegalDimensions
will be returned ifwidth
orheight
are less than 2.
fn start(self) -> Result<Game, (Self, GameStartError)>
Consume this PreGame
and provide a Game
from it.
Requires that both players have placed all their ships.
Errors
NoShipsPlaced
if no player has placed any ships yetNotAllShipsPlaced
if not all ships have been placed yet
fn add_ship_type(
&mut self,
name: &'static str,
length: Dimension
) -> Result<Rc<ShipType>, ShipTypeError>
&mut self,
name: &'static str,
length: Dimension
) -> Result<Rc<ShipType>, ShipTypeError>
Add a ship type to the game. Returns a unique id for the new ship type.
Parameters
name
The name of the ship type.length
The length of the ship type.
Errors
IllegalShipLength
If the ship type's length is smaller than 1.ShipTooLongForBattlefield
If the ship length is longer than the width or height of the battlefield.
fn place_ship(
&mut self,
player: Player,
ship_type: &Rc<ShipType>,
x: Dimension,
y: Dimension,
orientation: Orientation
) -> Result<(), PlaceError>
&mut self,
player: Player,
ship_type: &Rc<ShipType>,
x: Dimension,
y: Dimension,
orientation: Orientation
) -> Result<(), PlaceError>
Place a ship of a previously added ship type on the battlefield.
Parameters
player
The player who owns the shipship_type
Ref to the ship type of the ship to be placed.x
The x coordinate of the shipy
The y coordinate of the shiporientation
The orientation of the ship
Errors
AlreadyPlaced
In case the player has already placed a ship of that ship type.OutOfBounds
If the ship would exceed any boundary of the battlefield.UnknownShipType
If the ship type id is invalid.CellOccupied
If the ship would occupy an already occupied coordinate.
Examples
Player 1 places a corvette of length 2 on (0, 0) and (1, 0)
let mut pregame = PreGame::new(3, 3).unwrap(); let corvette = pregame.add_ship_type("Corvette", 2).unwrap(); let result = pregame.place_ship(P1, &corvette, 0, 0, Horizontal); // check result here
fn get_cell(&self, player: Player, x: Dimension, y: Dimension) -> CellStatus
Gets the status of the cell (x
, y
) owned by player
.
Parameters
player
determines which battlefield to consider, i.e. the owner of the battlefield.x
the x coordinatey
the y coordinate
Panics
Panics if the x and/or y coordinate is out of bounds.
Trait Implementations
impl PartialEq for PreGame
[src]
fn eq(&self, __arg_0: &PreGame) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &PreGame) -> bool
This method tests for !=
.
impl Debug for PreGame
[src]
impl Dimensional for PreGame
[src]
fn width(&self) -> Dimension
Returns the width of this object.
fn height(&self) -> Dimension
Returns the height of this object.
impl ShipTypeContainer for PreGame
[src]
fn ship_types(&self) -> Vec<Rc<ShipType>>
Returns a copy of the list of ship types.