Implement the basic rules of Go #40
|
@ -10,7 +10,6 @@ pub enum Error {
|
|||
#[derive(Clone, Debug)]
|
||||
pub struct Board {
|
||||
pub size: Size,
|
||||
pub grid: Grid<Option<Color>>,
|
||||
pub groups: Vec<Group>,
|
||||
}
|
||||
|
||||
|
@ -45,7 +44,6 @@ impl Board {
|
|||
width: 19,
|
||||
height: 19,
|
||||
},
|
||||
grid: Grid::new(19, 19),
|
||||
groups: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
@ -90,11 +88,6 @@ impl Board {
|
|||
};
|
||||
self.groups.push(friendly_group.clone());
|
||||
|
||||
match self.grid.get_mut(coordinate.row, coordinate.column) {
|
||||
None => return Err(Error::InvalidPosition),
|
||||
Some(space) => *space = Some(color),
|
||||
}
|
||||
|
||||
let adjacent_groups = self.adjacent_groups(&friendly_group);
|
||||
for group in adjacent_groups {
|
||||
if self.liberties(&group) == 0 {
|
||||
|
@ -106,10 +99,10 @@ impl Board {
|
|||
}
|
||||
|
||||
pub fn stone(&self, coordinate: &Coordinate) -> Option<Color> {
|
||||
match self.grid.get(coordinate.row, coordinate.column) {
|
||||
None => None,
|
||||
Some(val) => *val,
|
||||
}
|
||||
self.groups
|
||||
.iter()
|
||||
.find(|g| g.contains(coordinate))
|
||||
.map(|g| g.color)
|
||||
}
|
||||
|
||||
pub fn group(&self, coordinate: &Coordinate) -> Option<&Group> {
|
||||
|
@ -120,12 +113,6 @@ impl Board {
|
|||
|
||||
pub fn remove_group(&mut self, group: &Group) {
|
||||
self.groups.retain(|g| g != group);
|
||||
for coord in group.coordinates.iter() {
|
||||
match self.grid.get_mut(coord.row, coord.column) {
|
||||
None => (),
|
||||
Some(v) => *v = None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn adjacent_groups(&self, group: &Group) -> Vec<Group> {
|
||||
|
@ -263,6 +250,12 @@ pub struct Group {
|
|||
coordinates: HashSet<Coordinate>,
|
||||
}
|
||||
|
||||
impl Group {
|
||||
fn contains(&self, coordinate: &Coordinate) -> bool {
|
||||
self.coordinates.contains(coordinate)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
impl Group {
|
||||
pub fn adjacencies(&self, max_column: usize, max_row: usize) -> HashSet<Coordinate> {
|
||||
|
|
Loading…
Reference in New Issue