You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
3.0 KiB
134 lines
3.0 KiB
use std::default::Default;
|
|
use std::fmt;
|
|
use std::fmt::Display;
|
|
|
|
#[derive(Debug,Clone,Copy,Eq,PartialEq)]
|
|
pub enum Side {
|
|
Black = 0,
|
|
White = 1,
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy)]
|
|
pub enum Phase {
|
|
PickSides,
|
|
Turn(Side),
|
|
}
|
|
|
|
#[derive(Debug,Clone,Default)]
|
|
pub struct Player {
|
|
pub born_off: u32,
|
|
pub on_bar: u32,
|
|
}
|
|
|
|
#[derive(Debug,Clone,Copy)]
|
|
pub enum Bin {
|
|
Empty,
|
|
Black(u32),
|
|
White(u32),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Game {
|
|
pub phase : Phase,
|
|
pub multiplier : u32,
|
|
pub roll : [u8; 2],
|
|
pub players : [Player; 2],
|
|
pub bins : [Bin; 24],
|
|
}
|
|
|
|
impl Default for Game {
|
|
fn default() -> Self {
|
|
Game {
|
|
phase: Phase::PickSides,
|
|
multiplier: 1,
|
|
roll: [0; 2],
|
|
players: [
|
|
Player::default(),
|
|
Player::default(),
|
|
],
|
|
bins: [Bin::Empty; 24],
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Display for Game {
|
|
fn fmt(&self, f : &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
for n in (13..=24) {
|
|
write!(f, "{} ", ('D' as u8 + (n - 13) as u8) as char)?;
|
|
}
|
|
write!(f, "\n")?;
|
|
|
|
for (i, b) in (&self.bins[12..=23]).iter().enumerate() {
|
|
match *b {
|
|
Bin::Empty => {
|
|
if i%2==0 {
|
|
write!(f, "░░")?
|
|
} else {
|
|
write!(f, "▒▒")?
|
|
}
|
|
},
|
|
Bin::Black(n) => write!(f, "{:X}#", n)?,
|
|
Bin::White(n) => write!(f, "{:X}:", n)?
|
|
}
|
|
}
|
|
write!(f, "|:{}\n", self.players[Side::Black as usize].born_off)?;
|
|
|
|
for (i, b) in (&self.bins[0..=11]).iter().rev().enumerate() {
|
|
match *b {
|
|
Bin::Empty => {
|
|
if i%2==0 {
|
|
write!(f, "░░")?
|
|
} else {
|
|
write!(f, "▒▒")?
|
|
}
|
|
},
|
|
Bin::Black(n) => write!(f, "{:X}#", n)?, //█
|
|
Bin::White(n) => write!(f, "{:X}:", n)? //░
|
|
}
|
|
}
|
|
|
|
write!(f, "|#{}\n", self.players[Side::Black as usize].born_off)?;
|
|
for n in (1..=12).rev() {
|
|
write!(f, "{:X} ", n)?;
|
|
}
|
|
write!(f, "\n")?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Game {
|
|
fn clear(&mut self) {
|
|
for i in 0..24 {
|
|
self.bins[i] = Bin::Empty;
|
|
}
|
|
}
|
|
|
|
fn set(&mut self, n : u32, val : Bin) {
|
|
assert!(n > 0 && n <= 24);
|
|
let i = n - 1;
|
|
self.bins[i as usize] = val;
|
|
}
|
|
|
|
fn starting_position(&mut self) {
|
|
self.clear();
|
|
self.set(1, Bin::White(2));
|
|
self.set(6, Bin::Black(5));
|
|
self.set(8, Bin::Black(3));
|
|
self.set(12, Bin::White(5));
|
|
self.set(13, Bin::Black(5));
|
|
self.set(17, Bin::White(3));
|
|
self.set(19, Bin::White(5));
|
|
self.set(24, Bin::Black(2));
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let mut board = Game::default();
|
|
|
|
board.starting_position();
|
|
|
|
println!("{}", board);
|
|
|
|
|
|
}
|
|
|