-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatarigo.rs
182 lines (154 loc) · 4.25 KB
/
atarigo.rs
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
#![allow(unused)]
use super::bitboard;
use super::bitboard::BitBoard;
use crate::display::RectangularBoard;
use crate::display::RectangularBoardDisplay;
use crate::game::Game;
use crate::game::PlayerIndex;
use serde::Serialize;
use std::fmt;
#[derive(Copy, Clone, Serialize, Debug, Default, PartialEq, Eq)]
pub enum Player {
#[default]
Black,
White,
}
impl Player {
fn next(self) -> Player {
match self {
Player::Black => Player::White,
Player::White => Player::Black,
}
}
}
impl PlayerIndex for Player {
fn to_index(&self) -> usize {
*self as usize
}
}
#[derive(Clone, Copy, Serialize, Debug, Hash, PartialEq, Eq)]
pub struct Move(u8, u64);
#[derive(Clone, Copy, Serialize, Debug, Default, PartialEq, Eq)]
pub struct State<const N: usize> {
black: BitBoard<N, N>,
white: BitBoard<N, N>,
turn: Player,
winner: bool,
}
impl<const N: usize> State<N> {
#[inline(always)]
fn occupied(&self) -> BitBoard<N, N> {
self.black | self.white
}
#[inline(always)]
fn player(&self, player: Player) -> BitBoard<N, N> {
match player {
Player::Black => self.black,
Player::White => self.white,
}
}
#[inline(always)]
fn color(&self, index: usize) -> Player {
debug_assert!(self.occupied().get(index));
if self.black.get(index) {
Player::Black
} else {
debug_assert!(self.white.get(index));
Player::White
}
}
#[inline]
fn valid(&self, index: usize) -> (bool, BitBoard<N, N>) {
bitboard::check_go_move::<N, N>(
self.player(self.turn),
self.player(self.turn.next()),
index,
)
}
#[inline]
fn apply(&mut self, action: &Move) -> Self {
debug_assert!(!self.occupied().get(action.0 as usize));
let player = self.player(self.turn) | BitBoard::from_index(action.0 as usize);
let opponent = self.player(self.turn.next());
match self.turn {
Player::Black => {
self.black = player;
self.white = opponent & !BitBoard::new(action.1);
}
Player::White => {
self.white = player;
self.black = opponent & !BitBoard::new(action.1);
}
}
if action.1 > 0 {
self.winner = true;
} else {
self.turn = self.turn.next();
}
*self
}
}
#[derive(Clone)]
pub struct AtariGo<const N: usize>;
impl<const N: usize> Game for AtariGo<N> {
type S = State<N>;
type A = Move;
type P = Player;
fn apply(mut state: State<N>, action: &Move) -> State<N> {
state.apply(action)
}
fn generate_actions(state: &State<N>, actions: &mut Vec<Move>) {
for index in !state.occupied() {
let (mut valid, will_capture) = state.valid(index);
actions.push(Move(index as u8, will_capture.get_raw()))
}
}
fn is_terminal(state: &State<N>) -> bool {
state.winner
}
fn player_to_move(state: &State<N>) -> Player {
state.turn
}
fn winner(state: &State<N>) -> Option<Player> {
if state.winner {
Some(state.turn)
} else {
None
}
}
fn notation(state: &Self::S, action: &Self::A) -> String {
const COL_NAMES: &[u8] = b"ABCDEFGH";
let (row, col) = BitBoard::<N, N>::to_coord(action.0 as usize);
format!("{}{}", COL_NAMES[col] as char, row + 1)
}
fn num_players() -> usize {
2
}
}
impl<const N: usize> RectangularBoard for State<N> {
const NUM_DISPLAY_ROWS: usize = N;
const NUM_DISPLAY_COLS: usize = N;
fn display_char_at(&self, row: usize, col: usize) -> char {
if self.black.get_at(row, col) {
'X'
} else if self.white.get_at(row, col) {
'O'
} else {
'.'
}
}
}
impl<const N: usize> fmt::Display for State<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
RectangularBoardDisplay(self).fmt(f)
}
}
#[cfg(test)]
mod tests {
use crate::util::random_play;
use super::*;
#[test]
fn test_atarigo() {
random_play::<AtariGo<7>>();
}
}