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
195
196
197
198
199
200
use std::fmt::{Display, Error, Formatter};
use std::ops::Not;
use std::str::FromStr;
use crate::pieces::Nat;
use crate::pieces::stones::Color;
use crate::pieces::util::coord::Point;
pub mod game;
pub mod game_builder;
mod sgf_bridge;
#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
pub enum Player {
White,
Black,
}
impl Not for Player {
type Output = Player;
fn not(self) -> Self::Output {
match self {
Player::Black => Player::White,
Player::White => Player::Black,
}
}
}
impl Display for Player {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match self {
Player::White => write!(f, "White"),
Player::Black => write!(f, "Black"),
}
}
}
impl Player {
#[inline]
pub fn stone_color(self) -> Color {
match self {
Player::Black => Color::Black,
Player::White => Color::White,
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum GobanSizes {
Nineteen,
Nine,
Thirteen,
Custom(Nat, Nat),
}
impl Into<(Nat, Nat)> for GobanSizes {
fn into(self) -> (Nat, Nat) {
match self {
GobanSizes::Nine => (9, 9),
GobanSizes::Thirteen => (13, 13),
GobanSizes::Nineteen => (19, 19),
GobanSizes::Custom(height, width) => (height, width),
}
}
}
impl From<usize> for GobanSizes {
fn from(x: usize) -> Self {
match x {
9 => GobanSizes::Nine,
13 => GobanSizes::Thirteen,
19 => GobanSizes::Nineteen,
_ => panic!("Not implemented for others size than 9,13,19"),
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Move {
Pass,
Resign(Player),
Play(Nat, Nat),
}
impl From<Point> for Move {
fn from((x0, x1): Point) -> Self {
Move::Play(x0, x1)
}
}
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum EndGame {
WinnerByScore(Player, f32),
WinnerByResign(Player),
WinnerByTime(Player),
WinnerByForfeit(Player),
Draw,
}
impl EndGame {
#[inline]
pub fn get_winner(self) -> Option<Player> {
match self {
EndGame::WinnerByScore(p, _)
| EndGame::WinnerByResign(p)
| EndGame::WinnerByTime(p)
| EndGame::WinnerByForfeit(p) => Some(p),
EndGame::Draw => None,
}
}
}
#[derive(Clone, Eq, PartialEq, Debug, Copy)]
pub enum PlayError {
Ko,
Suicide,
GamePaused,
FillEye,
}
type FlagUInt = u32;
bitflags! {
pub struct IllegalRules: FlagUInt{
const KO = 1;
const SUPERKO = 1 << 1;
const SUICIDE = 1 << 2;
const FILLEYE = 1 << 3;
}
}
bitflags! {
pub struct ScoreRules : FlagUInt {
const STONES = 1;
const KOMI = 1 << 1;
const PRISONNERS = 1 << 2;
}
}
#[derive(Clone, Eq, PartialEq, Debug, Copy)]
pub enum Rule {
Japanese,
Chinese,
}
impl Rule {
#[inline(always)]
pub fn komi(self) -> f32 {
match self {
Self::Japanese => 6.5,
Self::Chinese => 7.5,
}
}
#[inline(always)]
pub fn illegal_flag(self) -> IllegalRules {
match self {
Self::Japanese => IllegalRules::KO | IllegalRules::SUICIDE,
Self::Chinese => IllegalRules::SUPERKO | IllegalRules::KO | IllegalRules::SUICIDE,
}
}
#[inline(always)]
pub fn score_flag(self) -> ScoreRules {
match self {
Self::Japanese => ScoreRules::KOMI | ScoreRules::PRISONNERS,
Self::Chinese => ScoreRules::KOMI | ScoreRules::STONES,
}
}
}
impl FromStr for Rule {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"JAP" => Ok(Rule::Japanese),
"CHI" => Ok(Rule::Chinese),
_ => Err(format!("The rule {} is not implemented yet.", s)),
}
}
}