Implement the basic rules of Go #40

Merged
savanni merged 13 commits from feature/go-rules into main 2023-05-04 02:34:40 +00:00
1 changed files with 10 additions and 17 deletions
Showing only changes of commit 27d87fcd02 - Show all commits

View File

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