Board

Struct Board 

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

Represents the Backgammon board.

A Backgammon board has 24 fields, each holding any number of checkers. There is also a bar for hit checkers and an off-area for checkers removed from play.

//        +11-10--9--8--7--6-------5--4--3--2--1--0-+
//        | 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 | +-------+
//        +12-13-14-15-16-17------18-19-20-21-22-23-+

§Example: Playing on the Board

This example demonstrates how two players can make moves in a backgammon game.

Caveat: Do not not operate directly on the Board struct unless you know what you are doing! To play Backgammon, you must play on the Game struct, otherwise game rules are ignored.

use backgammon::rules::prelude::*;

let mut board = Board::default();

// Player 0 rolls dice and gets 3, moves from field 23
board.move_checker(Player::Player0, 3, MoveFrom::Board(23)).unwrap();

// Player 1 rolls dice and gets 4, moves from field 23
board.move_checker(Player::Player1, 4, MoveFrom::Board(23)).unwrap();

// Player 0 rolls dice and gets 2, moves from field 12
board.move_checker(Player::Player0, 2, MoveFrom::Board(12)).unwrap();

// Player 1 rolls dice and gets 5, moves from field 12
board.move_checker(Player::Player1, 5, MoveFrom::Board(12)).unwrap();

// If a checker gets hit and goes to the bar, it must be moved from the bar first
// Player 0 has a checker on the bar and rolls a 3
// board.move_checker(Player::Player0, 3, MoveFrom::Bar).unwrap();

// When all checkers are in the home board, players can bear off
// Player 0 bears off a checker from field 2 with dice roll 3
// board.move_checker(Player::Player0, 3, MoveFrom::Board(2)).unwrap();

// Check the current board state
let display = board.get();
println!("{}", display);
assert_eq!("Board: [-1, 0, 0, 0, -1, 5, 0, 3, 0, 0, 1, -4, 4, 0, 0, 0, -4, 0, -5, 0, 1, 0, 0, 1], Bar: (0, 0), Off: (0, 0)",display.to_string());

Implementations§

Source§

impl Board

Source

pub fn new() -> Self

Create a new board.

Source

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

Completely empty out a Backgammon board so that the set() method can be used to fill it arbitrarily. This can, for example, be used to replay any game situation.

Source

pub fn get(&self) -> BoardDisplay

Retrieve the board for both players to generate its graphical representation.

This method outputs a tuple with three values:

  1. The board consists of an array of 24 fields. Each field holds zero or more checkers. Positive values indicate checkers for player 0, and negative values indicate checkers for player 1.
  2. The bar for both players
  3. The third element is the off-counts for both players.
use backgammon::rules::Board;

let b = Board::default();
assert_eq!("Board: [-2, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, -5, 5, 0, 0, 0, -3, 0, -5, 0, 0, 0, 0, 2], Bar: (0, 0), Off: (0, 0)", b.get().to_string());
Source

pub fn set( &mut self, player: Player, field: usize, amount: i8, ) -> Result<(), Error>

Set checkers for a player on a field.

This method adds the number of checkers for a player on a field. The field is numbered from 0 to 23, starting from the last field of each player on the home board. The farthest field for each player (where there are 2 checkers to start with) is number 23.

If the field is blocked for the player, an error is returned. If the field is not blocked, but there is already one checker from the other player on the field, that checker is hit and moved to the bar.

If the amount to be set is zero, nothing happens.

Source

pub fn is_blocked(&self, player: Player, field: usize) -> Result<bool, Error>

Check if a field is blocked for a player.

Source

pub fn is_available(&self, player: Player, field: usize) -> Result<bool, Error>

Check if a field is available for a player.

Source

pub fn is_bar_empty(&self, player: Player) -> Result<bool, Error>

Need to clean bar first.

Source

pub fn ready_for_off_play(&self, player: Player) -> Result<bool, Error>

Checks if a player is ready to start bearing off their checkers in a backgammon game.

Source

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

Set checkers for a player off the board. This method increments the number of checkers already present there.

Source

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

Set checkers for a player off the board. This method adds amount to the already existing checkers there.

Trait Implementations§

Source§

impl Clone for Board

Source§

fn clone(&self) -> Board

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 Board

Source§

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

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

impl Default for Board

Source§

fn default() -> Board

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

impl Move for Board

Source§

fn move_checker( &mut self, player: Player, dice: u8, from: MoveFrom, ) -> Result<(), Error>

Move a checker from a position (board field or bar) by the dice value.

Source§

fn available_moves( &self, player: Player, dice: u8, ) -> Result<Vec<MoveFrom>, Error>

Return available moves for a player
Source§

impl PartialEq for Board

Source§

fn eq(&self, other: &Board) -> 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 Stats for Board

Source§

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

Calculate the Pip for each player.
Source§

impl StructuralPartialEq for Board

Auto Trait Implementations§

§

impl Freeze for Board

§

impl RefUnwindSafe for Board

§

impl Send for Board

§

impl Sync for Board

§

impl Unpin for Board

§

impl UnwindSafe for Board

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, 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.
Source§

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

Source§

fn vzip(self) -> V