pub fn play(&mut self, s: Stone, x: usize, y: usize, rules: &Rules) -> bool {
        // If coordinate is off the board
        if x > self.w || y > self.h {
            return false;
        }

        let index = self.index(x, y);

        if index >= self.stones.len() {
            return false;
        }

        // If point is already filled
        if self.stones[index] != Stone::Empty {
            return false;
        }

        // Place stone
        self.stones[index] = s;
        // Find group of newly-placed stone
        let group = self.find_group(x, y).unwrap();

        self.groups.push(group.clone());
        self.update_groups();

        // Kill enemy groups
        let mut dead: Vec<usize> = Vec::new();
        for i in (0..self.groups.len()).rev() {
            let g = &self.groups[i];

            if g.liberties.is_empty() && g.color == group.color.swap() {
                dead.push(i);
            }
        }

        for g in &dead {
            self.kill_group(*g);
        }

        self.update_groups();
        let last_group = self.groups.last().unwrap();

        // Undo last move if no groups are captured
        if last_group.liberties.is_empty() && dead.is_empty() {
            if rules.suicide_allowed {
                self.kill_group(self.groups.len() - 1);
            }

            self.stones[index] = Stone::Empty;
            self.remove_stone_from_group((x, y));

            return false;
        }

        return true;
    }
/// Do move only if it is legal.
/// Returns if move is legal
Do move only if it is legal.
Returns if move is legal
Do move only if it is legal . Returns if move is legal
pub fn play(&mut self, s: Stone, x: usize, y: usize, rules: &Rules) -> bool {
        // If coordinate is off the board
        if x > self.w || y > self.h {
            return false;
        }
        let index = self.index(x, y);
        if index >= self.stones.len() {
            return false;
        }
        // If point is already filled
        if self.stones[index] != Stone::Empty {
            return false;
        }
        // Place stone
        self.stones[index] = s;
        // Find group of newly-placed stone
        let group = self.find_group(x, y).unwrap();
        self.groups.push(group.clone());
        self.update_groups();
        // Kill enemy groups
        let mut dead: Vec<usize> = Vec::new();
        for i in (0..self.groups.len()).rev() {
            let g = &self.groups[i];
            if g.liberties.is_empty() && g.color == group.color.swap() {
                dead.push(i);
            }
        }
        for g in &dead {
            self.kill_group(*g);
        }
        self.update_groups();
        let last_group = self.groups.last().unwrap();
        // Undo last move if no groups are captured
        if last_group.liberties.is_empty() && dead.is_empty() {
            if rules.suicide_allowed {
                self.kill_group(self.groups.len() - 1);
            }
            self.stones[index] = Stone::Empty;
            self.remove_stone_from_group((x, y));
            return false;
        }
        return true;
    }
pub fn play ( & mut self , s : Stone , x : usize , y : usize , rules : & Rules ) -> bool { // If coordinate is off the board if x > self . w || y > self . h { return false ; } let index = self . index ( x , y ) ; if index >= self . stones . len ( ) { return false ; } // If point is already filled if self . stones [ index ] != Stone :: Empty { return false ; } // Place stone self . stones [ index ] = s ; // Find group of newly-placed stone let group = self . find_group ( x , y ) . unwrap ( ) ; self . groups . push ( group . clone ( ) ) ; self . update_groups ( ) ; // Kill enemy groups let mut dead : Vec < usize > = Vec :: new ( ) ; for i in ( 0 .. self . groups . len ( ) ) . rev ( ) { let g = & self . groups [ i ] ; if g . liberties . is_empty ( ) && g . color == group . color . swap ( ) { dead . push ( i ) ; } } for g in & dead { self . kill_group ( * g ) ; } self . update_groups ( ) ; let last_group = self . groups . last ( ) . unwrap ( ) ; // Undo last move if no groups are captured if last_group . liberties . is_empty ( ) && dead . is_empty ( ) { if rules . suicide_allowed { self . kill_group ( self . groups . len ( ) - 1 ) ; } self . stones [ index ] = Stone :: Empty ; self . remove_stone_from_group ( ( x , y ) ) ; return false ; } return true ; }
Do move only if it is legal.
Do move only if it is legal .
&mut self
s
Stone
x
usize
y
usize
rules
&Rules
&mut self
s
Stone
x
usize
y
usize
rules
&Rules