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.

Creates a new instance.

Parameters

  • width The number of columns
  • height The number of lines

Errors

  • IllegalDimensions will be returned if width or height are less than 2.

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 yet
  • NotAllShipsPlaced if not all ships have been placed yet

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.

Place a ship of a previously added ship type on the battlefield.

Parameters

  • player The player who owns the ship
  • ship_type Ref to the ship type of the ship to be placed.
  • x The x coordinate of the ship
  • y The y coordinate of the ship
  • orientation 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

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 coordinate
  • y the y coordinate

Panics

Panics if the x and/or y coordinate is out of bounds.

Trait Implementations

impl PartialEq for PreGame
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Debug for PreGame
[src]

Formats the value using the given formatter.

impl Dimensional for PreGame
[src]

Returns the width of this object.

Returns the height of this object.

impl ShipTypeContainer for PreGame
[src]

Returns a copy of the list of ship types.