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
use crate::pieces::util::*;
use crate::pieces::stones::*;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Error;
#[derive(Clone, Eq, Getters, Setters, Debug)]
pub struct Goban {
#[get = "pub"]
#[set]
tab: Vec<u8>,
#[get = "pub"]
#[set]
size: usize,
}
impl Goban {
pub fn new(size: usize) -> Goban {
Goban {
tab: vec![Color::None as u8; size * size],
size,
}
}
pub fn clear(&mut self) {
self.tab = vec![Color::None as u8; self.size * self.size];
}
pub fn push(&mut self, coord: &Coord, color: Color) -> Result<&mut Goban, String> {
if self.coord_valid(coord) {
self.tab[CoordUtil::new(self.size, self.size).to(coord)] = color as u8;
Ok(self)
} else {
Err(format!("the coord :({},{}) are outside the goban", coord.0, coord.1))
}
}
pub fn push_many<'a>(&'a mut self, coords: impl Iterator<Item=&'a Coord>, value: Color) {
coords.for_each(|c| {
self.push(c, value).expect("Add one of the stones to the goban.");
})
}
pub fn push_stone(&mut self, stone: &Stone) -> Result<&mut Goban, String> {
self.push(&stone.coord, stone.color)
}
pub fn get(&self, coord: &Coord) -> Color {
if !self.coord_valid(coord) {
panic!("Coord out of bounds")
}
let c = CoordUtil::new(self.size, self.size);
self.tab[c.to(coord)].into()
}
pub fn pop(&mut self) -> &mut Self {
self.tab.pop();
self
}
pub fn get_neighbors(&self, coord: &Coord) -> impl Iterator<Item=Stone> + '_ {
neighbors_coords(coord)
.into_iter()
.filter(move |x| self.coord_valid(x))
.map(move |x| Stone { coord: x.clone(), color: self.get(&x) })
}
pub fn get_neighbors_stones(&self, coord: &Coord) -> impl Iterator<Item=Stone> + '_ {
self.get_neighbors(coord)
.filter(|s| s.color != Color::None)
}
pub fn get_stones(&self) -> impl Iterator<Item=Stone> + '_ {
let coord_util = CoordUtil::new(self.size, self.size);
self.tab.iter()
.enumerate()
.filter(|(_index, t)| Color::from(**t) != Color::None)
.map(move |(index, t)|
Stone { coord: coord_util.from(index), color: (*t).into() })
}
pub fn get_stones_by_color(&self, color: Color) -> impl Iterator<Item=Stone> + '_ {
let coord_util = CoordUtil::new(self.size, self.size);
self.tab
.iter()
.enumerate()
.filter(move |(_index, t)| Color::from(**t) == color)
.map(move |(index, t)|
Stone { coord: coord_util.from(index), color: (*t).into() })
}
pub fn get_liberties(&self, point: &Stone) -> impl Iterator<Item=Stone> + '_ {
self.get_neighbors(&point.coord)
.filter(|s| s.color == Color::None)
}
pub fn get_nb_liberties(&self, point: &Stone) -> u8 {
self.get_liberties(point).count() as u8
}
pub fn has_liberties(&self, point: &Stone) -> bool {
self.get_liberties(point).any(|s| Color::None == s.color)
}
pub fn pretty_string(&self) -> String {
let mut buff = String::new();
for i in 0..self.size {
for j in 0..self.size {
buff.push(
match self.get(&(i, j)) {
Color::White => WHITE_STONE,
Color::Black => BLACK_STONE,
Color::None => EMPTY_STONE,
}
);
}
buff.push('\n');
}
buff
}
fn coord_valid(&self, coord: &Coord) -> bool {
if coord.0 < self.size && coord.1 < self.size {
true
} else {
false
}
}
}
impl Display for Goban {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{}", self.pretty_string())
}
}
impl PartialEq for Goban {
fn eq(&self, other: &Goban) -> bool {
other.tab == self.tab
}
}