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
impl Board
Sourcepub fn new() -> Self
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.
Sourcepub fn get_bar(&self, player: Player) -> Result<u8, Error>
pub fn get_bar(&self, player: Player) -> Result<u8, Error>
Get the bar counts for both players.
Sourcepub fn get_off(&self, player: Player) -> Result<u8, Error>
pub fn get_off(&self, player: Player) -> Result<u8, Error>
Get the off counts for both players.
Sourcepub fn is_blocked(
&self,
player: Player,
position: Position,
) -> Result<bool, Error>
pub fn is_blocked( &self, player: Player, position: Position, ) -> Result<bool, Error>
Checks if a position is blocked for a player.
Sourcepub fn is_available(
&self,
player: Player,
position: Position,
) -> Result<bool, Error>
pub fn is_available( &self, player: Player, position: Position, ) -> Result<bool, Error>
Checks if a position is available for a player.
Sourcepub fn is_bar_empty(&self, player: Player) -> Result<bool, Error>
pub fn is_bar_empty(&self, player: Player) -> Result<bool, Error>
Checks if the bar is empty for a given player.
Sourcepub fn ready_for_off_play(&self, player: Player) -> Result<bool, Error>
pub fn ready_for_off_play(&self, player: Player) -> Result<bool, Error>
Checks if a player is ready to start bearing off their checkers.
Sourcepub fn backgammon_loss_position(&self, player: Player) -> Result<bool, Error>
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.
Trait Implementations§
Source§impl Move for Board
impl Move for Board
Source§fn get_board(&self) -> BoardDisplay
fn get_board(&self) -> BoardDisplay
Retrieve the board for both players to generate its graphical representation.
This method outputs a tuple with three values:
- 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.
- The bar for both players
- 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>
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>
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.