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
//! Module with tools for getting the connected stones and liberties.

use std::collections::HashSet;
use crate::pieces::stones::Stone;
use crate::pieces::goban::Goban;
use crate::pieces::stones::Color;

impl Goban {
    ///
    /// Test if a group of stones is dead.
    ///
    /// "a group of stones is dead if it doesn't have liberties"
    ///
    pub fn are_dead(&self, stones: &HashSet<Stone>) -> bool {
        !stones // If there is one stone connected who has liberties, they are not captured.
            .iter()
            .any(|s| self.has_liberties(s))
    }

    ///
    /// Get all the groups of connected stones, who has at least one stones
    /// without liberties.
    ///
    pub fn get_connected_stones(&self) -> Vec<HashSet<Stone>> {
        let atari_stones = self
            // get all stones without liberties
            .get_stones()
            .filter(|point| !self.has_liberties(point));

        self.get_strongly_connected_stones(atari_stones)
    }

    ///
    /// Get all the groups of connected stones of a color, who has at leart one stone without
    /// liberties.
    ///
    /// if the color is empty it's an undefined behaviour
    ///
    pub fn get_connected_stones_color(&self, color: Color) ->
    Vec<HashSet<Stone>> {
        let atari_stones = self
            .get_stones_by_color(color)
            // get all stones without liberties
            .filter(|point| !self.has_liberties(point));
        self.get_strongly_connected_stones(atari_stones)
    }

    ///
    /// Can get a group of stones and his neighbors with a Breadth First Search,
    /// works for Empty stones too.
    ///
    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(); // Acquiring all the neighbors

        while let Some(stone_to_explore) = to_explore.pop() { // exploring the graph
            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
    }

    ///
    /// Use a breadth first search to deduce the groups of connected stones.
    /// Get stones connected. [[x,y,z],[a,e,r]] example of return.
    ///
    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 {
            // if the stone is already in a group of 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
    }
}