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
impl Board
Sourcepub fn empty(&mut self) -> Result<(), Error>
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.
Sourcepub fn get(&self) -> BoardDisplay
pub fn get(&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 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.
- The bar for both players
- 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());
Sourcepub fn set(
&mut self,
player: Player,
field: usize,
amount: i8,
) -> Result<(), Error>
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.
Sourcepub fn is_blocked(&self, player: Player, field: usize) -> Result<bool, Error>
pub fn is_blocked(&self, player: Player, field: usize) -> Result<bool, Error>
Check if a field is blocked for a player.
Sourcepub fn is_available(&self, player: Player, field: usize) -> Result<bool, Error>
pub fn is_available(&self, player: Player, field: usize) -> Result<bool, Error>
Check if a field is available for a 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 in a backgammon game.