Struct lib_battleship::Game
[−]
[src]
pub struct Game { /* fields omitted */ }
Struct representing a running game of battleship.
Methods
impl Game
[src]
fn new(ship_types: Vec<Rc<ShipType>>, battlefields: Vec<Battlefield>) -> Self
Creates a new instance. Use of this function is discouraged. You should rather go through
the PreGame
struct.
Parameters
ship_types
A non-emptpy vector ofShipType
s.battlefields
A Vector of exactly twoBattlefield
s where player 1 owns the battlefield at index 0 and player 2 owns the one at index 1.
fn current_player(&self) -> Player
This function determines who's turn it is.
fn shoot(
&mut self,
target_player: Player,
x: Dimension,
y: Dimension
) -> Result<ShootOk, ShootError>
&mut self,
target_player: Player,
x: Dimension,
y: Dimension
) -> Result<ShootOk, ShootError>
Shoot at a player's battlefield.
Parameters
target_player
The player to be shot at.x
The x coordinate.y
The y coordinate.
Errors
NotThisPlayersTurn
iftarget_player
is the same as what's returned bycurrent_player()
.OutOfBounds
if the given coordinates are outside the boundaries of the battlefield.GameOver
if the game is already finished
Examples
match game.shoot(P2, 0, 0).unwrap() { ShootOk::Hit => println!("hit!"), ShootOk::Miss => println!("miss!"), ShootOk::Destroyed => println!("ship destroyed!"), ShootOk::WinningShot => println!("you won!") } // note that you shouldn't just call `unwrap()` after `shoot()`, don't ignore errors.
fn get_cell(&self, player: Player, x: Dimension, y: Dimension) -> CellStatus
Gets the status of the cell (x
, y
) owned by player
.
Does not display missed shots, i.e. misses are considered Empty
(see get_opponent_cell
).
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.
fn get_opponent_cell(
&self,
player: Player,
x: Dimension,
y: Dimension
) -> CellStatus
&self,
player: Player,
x: Dimension,
y: Dimension
) -> CellStatus
Gets the status of the opponent cell (x
, y
) owned by player
.
Does not display unhit ship cells, i.e. unhit ship cells are considered Empty
.
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.
fn get_winner(&self) -> Option<Player>
Gets the winner of the game, if any.
Trait Implementations
impl PartialEq for Game
[src]
fn eq(&self, __arg_0: &Game) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Game) -> bool
This method tests for !=
.
impl Debug for Game
[src]
impl Dimensional for Game
[src]
fn width(&self) -> Dimension
Returns the width of this object.
fn height(&self) -> Dimension
Returns the height of this object.
impl ShipTypeContainer for Game
[src]
fn ship_types(&self) -> Vec<Rc<ShipType>>
Returns a copy of the list of ship types.