Skip to main content

Board

Struct Board 

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

§Backgammon Board

A Backgammon board consists of 24 fields, each holding any number of checkers of one colour. Each player has 15 checkers available for play. There is also a bar for hit checkers and an off-area for checkers removed from play. Further, two dice are required to play Backgammon.

This struct represents the Backgammon board, including dice and checkers. It provides methods to manipulate it.

Caveat: Do not operate directly on the Board struct unless you know what you are doing! To play Backgammon, you should play on the Match struct; otherwise, game and match rules are ignored.

//        +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-+

§Example: Playing on the Board

This example demonstrates how two players can make moves on a backgammon board. See the caveat above.

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

fn main() {
   let mut board = Board::default();

   // Player 0 rolls dice and gets 3, moves from field 24
   board.set_dice(Player::Nobody,(3, 1)).unwrap();

   // With these dice, Player 0 can select one of the following move options.
   let m = board.available_moves(Player::Player0);
   assert_eq!(
       m,
       Ok(vec![
           vec![
               (Position::Board(6), Position::Board(3)),
               (Position::Board(3), Position::Board(2))
           ],
           vec![
               (Position::Board(6), Position::Board(3)),
               (Position::Board(6), Position::Board(5))
           ],
           vec![
               (Position::Board(6), Position::Board(3)),
               (Position::Board(8), Position::Board(7))
           ],
           vec![
               (Position::Board(6), Position::Board(3)),
               (Position::Board(24), Position::Board(23))
           ],
           vec![
               (Position::Board(8), Position::Board(5)),
               (Position::Board(5), Position::Board(4))
           ],
           vec![
               (Position::Board(8), Position::Board(5)),
               (Position::Board(6), Position::Board(5))
           ],
           vec![
               (Position::Board(8), Position::Board(5)),
               (Position::Board(8), Position::Board(7))
           ],
           vec![
               (Position::Board(8), Position::Board(5)),
               (Position::Board(24), Position::Board(23))
           ],
           vec![
               (Position::Board(13), Position::Board(10)),
               (Position::Board(6), Position::Board(5))
           ],
           vec![
               (Position::Board(13), Position::Board(10)),
               (Position::Board(8), Position::Board(7))
           ],
           vec![
               (Position::Board(13), Position::Board(10)),
               (Position::Board(10), Position::Board(9))
           ],
           vec![
               (Position::Board(13), Position::Board(10)),
               (Position::Board(24), Position::Board(23))
           ],
           vec![
               (Position::Board(24), Position::Board(21)),
               (Position::Board(6), Position::Board(5))
           ],
           vec![
               (Position::Board(24), Position::Board(21)),
               (Position::Board(8), Position::Board(7))
           ],
           vec![
               (Position::Board(24), Position::Board(21)),
               (Position::Board(21), Position::Board(20))
           ],
           vec![
               (Position::Board(24), Position::Board(21)),
               (Position::Board(24), Position::Board(23))
           ],
           vec![
               (Position::Board(6), Position::Board(5)),
               (Position::Board(5), Position::Board(2))
           ],
           vec![
               (Position::Board(6), Position::Board(5)),
               (Position::Board(6), Position::Board(3))
           ],
           vec![
               (Position::Board(6), Position::Board(5)),
               (Position::Board(8), Position::Board(5))
           ],
           vec![
               (Position::Board(6), Position::Board(5)),
               (Position::Board(13), Position::Board(10))
           ],
           vec![
               (Position::Board(6), Position::Board(5)),
               (Position::Board(24), Position::Board(21))
           ],
           vec![
               (Position::Board(8), Position::Board(7)),
               (Position::Board(6), Position::Board(3))
           ],
           vec![
               (Position::Board(8), Position::Board(7)),
               (Position::Board(7), Position::Board(4))
           ],
           vec![
               (Position::Board(8), Position::Board(7)),
               (Position::Board(8), Position::Board(5))
           ],
           vec![
               (Position::Board(8), Position::Board(7)),
               (Position::Board(13), Position::Board(10))
           ],
           vec![
               (Position::Board(8), Position::Board(7)),
               (Position::Board(24), Position::Board(21))
           ],
           vec![
               (Position::Board(24), Position::Board(23)),
               (Position::Board(6), Position::Board(3))
           ],
           vec![
               (Position::Board(24), Position::Board(23)),
               (Position::Board(8), Position::Board(5))
           ],
           vec![
               (Position::Board(24), Position::Board(23)),
               (Position::Board(13), Position::Board(10))
           ],
           vec![
               (Position::Board(24), Position::Board(23)),
               (Position::Board(23), Position::Board(20))
           ],
           vec![
               (Position::Board(24), Position::Board(23)),
               (Position::Board(24), Position::Board(21))
           ],
       ])
   );
   board
       .move_checkers(
           Player::Player0,
           vec![
               (Position::Board(24), Position::Board(23)),
               (Position::Board(8), Position::Board(5)),
           ],
       )
       .unwrap();

   // Player 1 rolls the dice and hits one checker of Player 0
   board.set_dice(Player::Player1,(4, 1)).unwrap();
   board
       .move_checkers(
           Player::Player1,
           vec![
               (Position::Board(24), Position::Board(20)),
               (Position::Board(24), Position::Board(23)),
           ],
       )
       .unwrap();

   // Check the current board state
   assert_eq!(
       "Board: [0, -1, 0, 0, -1, 5, 0, 2, 0, 0, 0, -5, 5, 0, 0, 0, -3, 0, -5, 0, 0, 0, 1, 1], Bar: (1, 0), Off: (0, 0), Dice: (4, 1)",
       board.get_board().to_string()
   );
}

Implementations§

Source§

impl Board

Source

pub fn new() -> Self

Creates a new board with the standard Backgammon starting position.

This function initialises a board with all checkers placed in their default positions according to the standard Backgammon rules. Each player starts with 15 checkers arranged as follows:

  • 2 checkers on the 24-point
  • 5 checkers on the 13-point
  • 3 checkers on the 8-point
  • 5 checkers on the 6-point

The bar and off positions are empty at the start of the game.

Source

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

Get the bar counts for both players.

Source

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

Get the off counts for both players.

Source

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

Checks if a position is blocked for a player.

Source

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

Checks if a position is available for a player.

Source

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

Checks if the bar is empty for a given player.

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.

Source

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

Checks if a player has checkers in positions that would make him lose worth a Backgammon.

Source

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

Checks if the player has no checkers left on the board.

Source

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

Checks if a position contains a checker for a player.

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 get_board(&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 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,Move};

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), Dice: (0, 0)", b.get_board().to_string());
Source§

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

Moves a sequence of checkers according to the provided moves.

The moves are specified as a vector of tuples of positions (from, to). The ‘from’ position can be either a board field or the bar. The ‘to’ position can be either a board field or off.

Caveat: Do not use for playing! This method allows illegal moves if used on the Board struct. To play correctly, use Match or Game struct.

Source§

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

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>

Removes all checkers on the board, resets the bar, and off positions.

Source§

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

Return all available checker moves for a given player and a given set of available (not already used) dice.
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 Roll for Board

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 Board

Source§

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

Returns the Pip for a player. Read more
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 UnsafeUnpin 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.