2023-10-28 15:12:04 +02:00
|
|
|
use crate::player::{Player, Color};
|
2023-10-07 20:46:24 +02:00
|
|
|
use crate::Error;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
/// Represents the Backgammon board
|
|
|
|
|
///
|
2023-10-28 15:12:04 +02:00
|
|
|
/// A Tric-Trac board consists of 24 fields, each of which can hold 0 or more checkers.
|
2023-10-07 20:46:24 +02:00
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # fn foo() {}
|
|
|
|
|
/// // +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 |
|
2023-10-28 15:12:04 +02:00
|
|
|
/// // | | | |
|
2023-10-07 20:46:24 +02:00
|
|
|
/// // | 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-+
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, PartialEq, Deserialize, Default)]
|
|
|
|
|
pub struct Board {
|
|
|
|
|
raw_board: (PlayerBoard, PlayerBoard),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Represents the Backgammon board for both players (to be used for graphical representation).
|
|
|
|
|
#[derive(Debug, Serialize, PartialEq, Deserialize)]
|
|
|
|
|
pub struct BoardDisplay {
|
|
|
|
|
/// The board represented as an array of 24 fields, each of which can hold 0 or more checkers.
|
|
|
|
|
/// Positive amounts represent checkers of player 0, negative amounts represent checkers of
|
|
|
|
|
/// player 1.
|
|
|
|
|
pub board: [i8; 24],
|
|
|
|
|
/// The off for both players
|
|
|
|
|
pub off: (u8, u8),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Board {
|
|
|
|
|
/// Create a new board
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Board::default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the board for both players. Use for graphical representation of the board.
|
|
|
|
|
///
|
|
|
|
|
/// This method outputs a tuple with three values:
|
|
|
|
|
///
|
|
|
|
|
/// 1. the board represented as an array of 24 fields, each of which can hold 0 or more
|
|
|
|
|
/// checkers. Positive amounts represent checkers of player 0, negative amounts represent
|
|
|
|
|
/// checkers of player 1.
|
|
|
|
|
/// 3. the off for both players
|
|
|
|
|
pub fn get(&self) -> BoardDisplay {
|
|
|
|
|
let mut board: [i8; 24] = [0; 24];
|
|
|
|
|
|
|
|
|
|
for (i, val) in board.iter_mut().enumerate() {
|
|
|
|
|
*val = self.raw_board.0.board[i] as i8 - self.raw_board.1.board[23 - i] as i8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BoardDisplay {
|
|
|
|
|
board,
|
|
|
|
|
off: self.get_off(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the off for both players
|
|
|
|
|
fn get_off(&self) -> (u8, u8) {
|
|
|
|
|
(self.raw_board.0.off, self.raw_board.1.off)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set checkers for a player on a field
|
|
|
|
|
///
|
|
|
|
|
/// This method adds the amount of checkers for a player on a field. The field is numbered from
|
|
|
|
|
/// 0 to 23, starting from the last field of each player in the home board, the most far away
|
|
|
|
|
/// 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.
|
|
|
|
|
pub fn set(&mut self, player: Player, field: usize, amount: i8) -> Result<(), Error> {
|
|
|
|
|
if field > 23 {
|
|
|
|
|
return Err(Error::FieldInvalid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.blocked(player, field)? {
|
|
|
|
|
return Err(Error::FieldBlocked);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 15:12:04 +02:00
|
|
|
match player.color {
|
|
|
|
|
Color::White => {
|
2023-10-07 20:46:24 +02:00
|
|
|
let new = self.raw_board.0.board[field] as i8 + amount;
|
|
|
|
|
if new < 0 {
|
|
|
|
|
return Err(Error::MoveInvalid);
|
|
|
|
|
}
|
|
|
|
|
self.raw_board.0.board[field] = new as u8;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-10-28 15:12:04 +02:00
|
|
|
Color::Black => {
|
2023-10-07 20:46:24 +02:00
|
|
|
let new = self.raw_board.1.board[field] as i8 + amount;
|
|
|
|
|
if new < 0 {
|
|
|
|
|
return Err(Error::MoveInvalid);
|
|
|
|
|
}
|
|
|
|
|
self.raw_board.1.board[field] = new as u8;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check if a field is blocked for a player
|
|
|
|
|
pub fn blocked(&self, player: Player, field: usize) -> Result<bool, Error> {
|
|
|
|
|
if field > 23 {
|
|
|
|
|
return Err(Error::FieldInvalid);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 15:12:04 +02:00
|
|
|
match player.color {
|
|
|
|
|
Color::White => {
|
2023-10-07 20:46:24 +02:00
|
|
|
if self.raw_board.1.board[23 - field] > 1 {
|
|
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-28 15:12:04 +02:00
|
|
|
Color::Black => {
|
2023-10-07 20:46:24 +02:00
|
|
|
if self.raw_board.0.board[23 - field] > 1 {
|
|
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set checkers for a player off the board. This method adds amount to the already existing
|
|
|
|
|
/// checkers there.
|
|
|
|
|
pub fn set_off(&mut self, player: Player, amount: u8) -> Result<(), Error> {
|
2023-10-28 15:12:04 +02:00
|
|
|
match player.color {
|
|
|
|
|
Color::White => {
|
2023-10-07 20:46:24 +02:00
|
|
|
let new = self.raw_board.0.off + amount;
|
|
|
|
|
self.raw_board.0.off = new;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-10-28 15:12:04 +02:00
|
|
|
Color::Black => {
|
2023-10-07 20:46:24 +02:00
|
|
|
let new = self.raw_board.1.off + amount;
|
|
|
|
|
self.raw_board.1.off = new;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Represents the Backgammon board for one player
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
struct PlayerBoard {
|
|
|
|
|
board: [u8; 24],
|
|
|
|
|
off: u8,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for PlayerBoard {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
PlayerBoard {
|
|
|
|
|
board: [
|
|
|
|
|
0, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
|
|
|
|
|
],
|
|
|
|
|
off: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Trait to move checkers
|
|
|
|
|
pub trait Move {
|
|
|
|
|
/// Move a checker
|
|
|
|
|
fn move_checker(&mut self, player: Player, dice: u8, from: usize) -> Result<&mut Self, Error>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
|
|
|
|
|
|
|
|
|
/// Move permitted
|
|
|
|
|
fn move_permitted(&mut self, player: Player, dice: u8) -> Result<&mut Self, Error>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unit Tests
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn default_board() {
|
|
|
|
|
assert_eq!(Board::new(), Board::default());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn default_player_board() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
PlayerBoard::default(),
|
|
|
|
|
PlayerBoard {
|
|
|
|
|
board: [0, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,],
|
|
|
|
|
off: 0
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn get_board() {
|
|
|
|
|
let board = Board::new();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
board.get(),
|
|
|
|
|
BoardDisplay {
|
|
|
|
|
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,
|
|
|
|
|
],
|
|
|
|
|
off: (0, 0)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn get_off() {
|
|
|
|
|
let board = Board::new();
|
|
|
|
|
assert_eq!(board.get_off(), (0, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_player0() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
let player = Player {name: "".into(), color: Color::White};
|
|
|
|
|
board.set(player, 1, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().board[1], 1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_player1() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
let player = Player {name: "".into(), color: Color::Black};
|
|
|
|
|
board.set(player, 2, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().board[21], -1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_player0_off() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
let player = Player {name: "".into(), color: Color::White};
|
|
|
|
|
board.set_off(player, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().off.0, 1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_player1_off() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
let player = Player {name: "".into(), color: Color::Black};
|
|
|
|
|
board.set_off(player, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().off.1, 1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_player1_off1() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
let player = Player {name: "".into(), color: Color::Black};
|
|
|
|
|
board.set_off(player, 1)?;
|
|
|
|
|
board.set_off(player, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().off.1, 2);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn blocked_player0() -> Result<(), Error> {
|
|
|
|
|
let board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
assert!(board.blocked(Player { name:"".into(), color: Color::White }, 0)?);
|
2023-10-07 20:46:24 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn blocked_player1() -> Result<(), Error> {
|
|
|
|
|
let board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
assert!(board.blocked(Player { name:"".into(), color: Color::Black }, 0)?);
|
2023-10-07 20:46:24 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn blocked_player0_a() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
board.set(Player { name:"".into(), color: Color::Black }, 1, 2)?;
|
|
|
|
|
assert!(board.blocked(Player { name:"".into(), color: Color::White }, 22)?);
|
2023-10-07 20:46:24 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn blocked_player1_a() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
board.set(Player { name:"".into(), color: Color::White }, 1, 2)?;
|
|
|
|
|
assert!(board.blocked(Player { name:"".into(), color: Color::Black }, 22)?);
|
2023-10-07 20:46:24 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn blocked_invalid_field() {
|
|
|
|
|
let board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
assert!(board.blocked(Player { name:"".into(), color: Color::White }, 24).is_err());
|
2023-10-07 20:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_field_with_1_checker_player0_a() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
board.set(Player { name:"".into(), color: Color::White }, 1, 1)?;
|
|
|
|
|
board.set(Player { name:"".into(), color: Color::Black }, 22, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().board[1], -1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_field_with_1_checker_player0_b() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
board.set(Player { name:"".into(), color: Color::White }, 1, 1)?;
|
|
|
|
|
board.set(Player { name:"".into(), color: Color::Black }, 22, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().board[1], -1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_field_with_1_checker_player1_a() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
board.set(Player { name:"".into(), color: Color::Black }, 1, 1)?;
|
|
|
|
|
board.set(Player { name:"".into(), color: Color::White }, 22, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().board[22], 1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_field_with_1_checker_player1_b() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
board.set(Player { name:"".into(), color: Color::Black }, 1, 1)?;
|
|
|
|
|
board.set(Player { name:"".into(), color: Color::White }, 22, 1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().board[22], 1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_field_with_2_checkers_player0_a() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
board.set(Player { name:"".into(), color: Color::White }, 23, 2)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().board[23], 4);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_field_with_2_checkers_player0_b() -> Result<(), Error> {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
board.set(Player { name:"".into(), color: Color::White }, 23, -1)?;
|
2023-10-07 20:46:24 +02:00
|
|
|
assert_eq!(board.get().board[23], 1);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_field_blocked() {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
assert!(board.set(Player { name:"".into(), color: Color::White }, 0, 2).is_err());
|
2023-10-07 20:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_wrong_field1() {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
assert!(board.set(Player { name:"".into(), color: Color::White }, 50, 2).is_err());
|
2023-10-07 20:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_wrong_amount0() {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
assert!(board.set(Player { name:"".into(), color: Color::White }, 23, -3).is_err());
|
2023-10-07 20:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn set_wrong_amount1() {
|
|
|
|
|
let mut board = Board::new();
|
2023-10-28 15:12:04 +02:00
|
|
|
assert!(board.set(Player { name:"".into(), color: Color::Black }, 23, -3).is_err());
|
2023-10-07 20:46:24 +02:00
|
|
|
}
|
|
|
|
|
}
|