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
use std::collections::HashSet;
use crate::pieces::stones::Stone;
use crate::pieces::goban::Goban;
use crate::pieces::stones::Color;
impl Goban {
pub fn are_dead(&self, stones: &HashSet<Stone>) -> bool {
!stones
.iter()
.any(|s| self.has_liberties(s))
}
pub fn get_connected_stones(&self) -> Vec<HashSet<Stone>> {
let atari_stones = self
.get_stones()
.filter(|point| !self.has_liberties(point));
self.get_strongly_connected_stones(atari_stones)
}
pub fn get_connected_stones_color(&self, color: Color) ->
Vec<HashSet<Stone>> {
let atari_stones = self
.get_stones_by_color(color)
.filter(|point| !self.has_liberties(point));
self.get_strongly_connected_stones(atari_stones)
}
pub fn bfs(&self, point: &Stone) -> HashSet<Stone> {
let mut explored: HashSet<Stone> = HashSet::new();
explored.insert(point.clone());
let mut to_explore: Vec<Stone> = self.get_neighbors(&point.coord)
.filter(|p| p.color == point.color)
.collect();
while let Some(stone_to_explore) = to_explore.pop() {
explored.insert(stone_to_explore.clone());
self.get_neighbors(&stone_to_explore.coord)
.filter(|p| p.color == point.color && !explored.contains(p))
.for_each(|s| to_explore.push(s));
}
explored
}
pub fn get_strongly_connected_stones(&self, stones: impl Iterator<Item=Stone>) ->
Vec<HashSet<Stone>> {
let mut strongly_connected_stones: Vec<HashSet<Stone>> = Vec::new();
for atari_stone in stones {
let is_handled = strongly_connected_stones
.iter()
.any(|set| set.contains(&atari_stone));
if !is_handled {
strongly_connected_stones.push(self.bfs(&atari_stone))
}
}
strongly_connected_stones
}
}