1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use constants::*;
use moves::Submove;
use player::Player;
pub type Position = usize;
pub type MaybePoint = Option<Point>;
pub type InternalBoard = [MaybePoint; BOARD_SIZE];
#[derive(Clone, Copy)]
pub struct Point {
pub owner: Player,
pub count: u8,
}
impl Point {
pub fn new(owner: Player, count: u8) -> Point {
Point {
owner,
count,
}
}
}
pub const INITIAL_BOARD: [MaybePoint; BOARD_SIZE] = [
Some(Point { owner: Player::Black, count: 2 }),
None,
None,
None,
None,
Some(Point { owner: Player::White, count: 5 }),
None,
Some(Point { owner: Player::White, count: 3 }),
None,
None,
None,
Some(Point { owner: Player::Black, count: 5 }),
Some(Point { owner: Player::White, count: 5 }),
None,
None,
None,
Some(Point { owner: Player::Black, count: 3 }),
None,
Some(Point { owner: Player::Black, count: 5 }),
None,
None,
None,
None,
Some(Point { owner: Player::White, count: 2 }),
];
#[derive(Clone)]
pub struct Board {
pub board: InternalBoard,
pub bar_black: u8,
pub bar_white: u8,
}
impl Board {
pub fn init() -> Board {
Board {
board: INITIAL_BOARD,
bar_black: 0,
bar_white: 0,
}
}
pub fn bar(&self, p: Player) -> u8 {
match p {
Player::Black => self.bar_black,
_ => self.bar_white,
}
}
pub fn board(&self, p: Player) -> InternalBoard {
match p {
Player::White => {
let mut reversed_board = self.board;
reversed_board.reverse();
reversed_board
},
_ => self.board.clone(),
}
}
pub fn apply_submove(&mut self, _s: Submove) -> () {
}
pub fn validate_submove(&self, s: &Submove, p: Player) -> Result<bool, &str> {
let chequer_exists = |x: Position| match self.board(p)[x] {
Some(_) => Ok(true),
None => return Err("Chequer does not exist."),
};
let owns_chequer = |x: Position| self.board(p)[x].unwrap().owner == p;
let check_bar = self.bar(p) > 0;
let check_moving_to_point = |t: Position|
self.board(p)[t].is_some() &&
self.board(p)[t].unwrap().owner != p &&
self.board(p)[t].unwrap().count >= 2;
match s {
Submove::Enter { to } => {
Ok(check_moving_to_point(*to))
},
Submove::Move { from, to } => {
Ok(vec![
chequer_exists(*from)?,
owns_chequer(*from),
check_bar,
check_moving_to_point(*to),
].iter().all(|x| *x))
},
Submove::BearOff { from } => {
Ok(vec![
chequer_exists(*from)?,
owns_chequer(*from),
check_bar,
].iter().all(|x| *x))
},
}
}
pub fn pips(&mut self, p: Player) -> u16 {
let mut count: u16 = 0;
for (i, x) in self.board(p).iter().enumerate() {
let i = BOARD_SIZE - i;
let or_point = Point::new(p.switch(), 0).clone();
let x = x.unwrap_or(or_point);
if x.owner == p { count += (x.count as u16) * i as u16 }
}
count
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
#[test]
fn check_invalid_submoves() {
let p = Player::Black;
let b = Board::init();
assert_eq!(b.validate_submove(&Submove::from_str("4/3").unwrap(), p), Err("Chequer does not exist."));
assert_eq!(b.validate_submove(&Submove::from_str("1/3").unwrap(), p), Ok(false));
}
#[test]
fn check_pips() {
assert_eq!(Board::init().pips(Player::Black), 167);
assert_eq!(Board::init().pips(Player::White), 167);
}
}