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 93 additions and 0 deletions
Showing only changes of commit 6d51ae8479 - Show all commits

View File

@ -112,11 +112,28 @@ impl GameState {
} }
} }
#[derive(Clone, Debug)]
pub struct Board { pub struct Board {
pub size: Size, pub size: Size,
pub spaces: Vec<Option<Color>>, pub spaces: Vec<Option<Color>>,
} }
impl std::fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
for row in 0..self.size.height {
for column in 0..self.size.width {
match self.stone(column, row) {
None => write!(f, " . ")?,
Some(Color::Black) => write!(f, " X ")?,
Some(Color::White) => write!(f, " O ")?,
}
}
writeln!(f, "")?;
}
Ok(())
}
}
impl Board { impl Board {
fn new() -> Self { fn new() -> Self {
let mut spaces = Vec::new(); let mut spaces = Vec::new();
@ -146,3 +163,79 @@ impl Board {
((row as usize) * (self.size.width as usize) + (column as usize)) as usize ((row as usize) * (self.size.width as usize) + (column as usize)) as usize
} }
} }
#[cfg(test)]
mod test {
use super::*;
/* Two players (Black and White) take turns and Black plays first
* Stones are placed on the line intersections and not moved.
* A stone with no liberties is removed from the board.
* A group of stones of the same color share liberties.
* A stone at the edge of the board has only three liberties.
* A stone at the corner of the board has only two liberties.
* A stone may not be placed in a suicidal position.
* A stone placed in a suicidal position is legal if it captures other stones first.
*/
#[test]
fn it_shows_stones() {
let mut board = Board::new();
board.place_stone(3, 2, Color::White);
board.place_stone(2, 3, Color::White);
board.place_stone(4, 3, Color::White);
board.place_stone(3, 3, Color::Black);
println!("{}", board);
assert!(false);
}
#[test]
fn it_counts_individual_liberties() {
let mut board = Board::new();
board.place_stone(3, 3, Color::White);
board.place_stone(0, 3, Color::White);
board.place_stone(0, 0, Color::White);
println!("{}", board);
assert!(false);
}
#[test]
fn stones_share_liberties() {
let mut board = Board::new();
board.place_stone(3, 3, Color::White);
board.place_stone(3, 4, Color::White);
board.place_stone(3, 5, Color::White);
board.place_stone(4, 4, Color::White);
println!("{}", board);
assert!(false);
}
#[test]
fn opposing_stones_reduce_liberties() {
let mut board = Board::new();
board.place_stone(3, 3, Color::White);
board.place_stone(3, 4, Color::Black);
println!("{}", board);
assert!(false);
}
#[test]
fn surrounding_a_stone_remove_it() {
assert!(false);
}
#[test]
fn sorrounding_a_group_removes_it() {
assert!(false);
}
#[test]
fn suicide_is_forbidden() {
assert!(false);
}
#[test]
fn captures_preceed_self_capture() {
assert!(false);
}
}