Skip to main content

Game

Struct Game 

Source
pub struct Game { /* private fields */ }
Expand description

§The Backgammon Game

This struct holds all the fields necessary to play a single round of Backgammon. The implemented methods for this struct are the ones required to play a single round of Backgammon, e.g. enforcing the right player and other game rules.

Caveat: Even though you can theoretically play one Backgammon game correctly on the Game struct, it is not suggested to operate directly on this struct. To correctly apply all double cube rules, it is recommended to use the Match struct.

//        +12-11-10--9--8--7-------6--5--4--3--2--1-+
//        | X           O    |   | O              X | +-------+
//        | X           O    |   | O              X | | OFF O |
//        | X           O    |   | O                | +-------+
//        | X                |   | O                |
//        | X                |   | O                |
//        |                  |BAR|                  |
//        | O                |   | X                |
//        | O                |   | X                |
//        | O           X    |   | X                | +-------+
//        | O           X    |   | X              O | | OFF X |
//        | O           X    |   | X              O | +-------+
//        +13-14-15-16-17-18------19-20-21-22-23-24-+

Implementations§

Source§

impl Game

Source

pub fn new() -> Self

Creates a new default game.

Source

pub fn get_state(&self) -> &GameState

Returns the current game state.

Source

pub fn set_player(&mut self, player: Player) -> Result<(), Error>

Sets the current player.

Source

pub fn get_player(&self) -> &Player

Returns the current player. For ended games, returns Nobody.

Trait Implementations§

Source§

impl Clone for Game

Source§

fn clone(&self) -> Game

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Game

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Game

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Game

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Move for Game

Source§

fn get_board(&self) -> BoardDisplay

Returns the current board.

Source§

fn set_checkers( &mut self, player: Player, to: Position, amount: i8, ) -> Result<(), Error>

Set a number of checkers at a given position. This method can be used to prepare a board.

Updates the game state by moving a player’s checkers to a new position on the board based on the specified amount. This function handles various scenarios such as moving checkers to specific board fields, the bar, or off the board. The method is incremental, i.e. it adds or subtracts (if negative amounts are used) the existing checkers on the respective positions.

Source§

fn empty_board(&mut self) -> Result<(), Error>

Empty the board.

Source§

fn available_moves( &self, _player: Player, ) -> Result<Vec<Vec<(Position, Position)>>, Error>

Calculates and returns all available moves for a given player based on the dice roll.

Source§

fn move_checkers( &mut self, player: Player, moves: Vec<(Position, Position)>, ) -> Result<(), Error>

Move a sequence of checkers from a given position to another position. Read more
Source§

impl PartialEq for Game

Source§

fn eq(&self, other: &Game) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Roll for Game

Source§

fn roll(&mut self, player: Player) -> Result<(), Error>

Roll the two dice.
Source§

fn get_dice(&self) -> (u8, u8)

Get the current values of the dice.
Source§

fn set_dice(&mut self, player: Player, v: (u8, u8)) -> Result<(), Error>

Set the values of the dice.
Source§

fn dice_available(&self) -> &Vec<u8>

Returns the available dice values. These are the dice not yet consumed by a move.
Source§

fn dice_consumed(&self) -> bool

Return true if all dice have been consumed and no die is available for playing.
Source§

impl Stats for Game

Source§

fn pip(&self, player: Player) -> Result<u8, Error>

Returns the Pip for a player. Read more
Source§

impl StructuralPartialEq for Game

Auto Trait Implementations§

§

impl Freeze for Game

§

impl RefUnwindSafe for Game

§

impl Send for Game

§

impl Sync for Game

§

impl Unpin for Game

§

impl UnsafeUnpin for Game

§

impl UnwindSafe for Game

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.