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 68 additions and 41 deletions
Showing only changes of commit 3cd39af060 - Show all commits

View File

@ -173,9 +173,9 @@ impl std::fmt::Display for Board {
for row in 0..self.size.height {
for column in 0..self.size.width {
match self.stone(Coordinate { column, row }) {
None => write!(f, " . ")?,
Some(Color::Black) => write!(f, " X ")?,
Some(Color::White) => write!(f, " O ")?,
None => write!(f, " .")?,
Some(Color::Black) => write!(f, " X")?,
Some(Color::White) => write!(f, " O")?,
}
}
writeln!(f, "")?;
@ -310,6 +310,7 @@ mod test {
]
.into_iter(),
);
assert!(board.group(Coordinate { column: 18, row: 3 }).is_none());
assert_eq!(
board
.group(Coordinate { column: 3, row: 3 })
@ -347,15 +348,27 @@ mod test {
#[test]
fn stones_share_liberties() {
let board = Board::from_coordinates(
vec![
(Coordinate { column: 3, row: 3 }, Color::White),
(Coordinate { column: 3, row: 4 }, Color::White),
(Coordinate { column: 8, row: 3 }, Color::Black),
(Coordinate { column: 9, row: 3 }, Color::Black),
(Coordinate { column: 9, row: 4 }, Color::Black),
(Coordinate { column: 15, row: 3 }, Color::White),
(Coordinate { column: 15, row: 4 }, Color::White),
(Coordinate { column: 15, row: 5 }, Color::White),
(Coordinate { column: 14, row: 4 }, Color::White),
(Coordinate { column: 3, row: 8 }, Color::White),
(Coordinate { column: 3, row: 9 }, Color::White),
(Coordinate { column: 4, row: 9 }, Color::White),
(Coordinate { column: 3, row: 10 }, Color::Black),
]
.into_iter(),
);
let test_cases = vec![
(
Board::from_coordinates(
vec![
(Coordinate { column: 3, row: 3 }, Color::White),
(Coordinate { column: 3, row: 4 }, Color::White),
]
.into_iter(),
),
board.clone(),
Coordinate { column: 3, row: 4 },
Some(Group {
color: Color::White,
@ -369,21 +382,14 @@ mod test {
Some(6),
),
(
Board::from_coordinates(
vec![
(Coordinate { column: 3, row: 3 }, Color::White),
(Coordinate { column: 3, row: 4 }, Color::White),
(Coordinate { column: 4, row: 4 }, Color::White),
]
.into_iter(),
),
Coordinate { column: 3, row: 4 },
board.clone(),
Coordinate { column: 9, row: 3 },
Some(Group {
color: Color::White,
color: Color::Black,
coordinates: vec![
Coordinate { column: 3, row: 3 },
Coordinate { column: 3, row: 4 },
Coordinate { column: 4, row: 4 },
Coordinate { column: 8, row: 3 },
Coordinate { column: 9, row: 3 },
Coordinate { column: 9, row: 4 },
]
.into_iter()
.collect(),
@ -391,29 +397,36 @@ mod test {
Some(7),
),
(
Board::from_coordinates(
vec![
(Coordinate { column: 3, row: 3 }, Color::White),
(Coordinate { column: 3, row: 4 }, Color::White),
(Coordinate { column: 4, row: 4 }, Color::White),
(Coordinate { column: 3, row: 5 }, Color::White),
]
.into_iter(),
),
Coordinate { column: 3, row: 4 },
board.clone(),
Coordinate { column: 15, row: 4 },
Some(Group {
color: Color::White,
coordinates: vec![
Coordinate { column: 3, row: 3 },
Coordinate { column: 3, row: 4 },
Coordinate { column: 4, row: 4 },
Coordinate { column: 3, row: 5 },
Coordinate { column: 15, row: 3 },
Coordinate { column: 15, row: 4 },
Coordinate { column: 15, row: 5 },
Coordinate { column: 14, row: 4 },
]
.into_iter()
.collect(),
}),
Some(8),
),
(
board.clone(),
Coordinate { column: 3, row: 9 },
Some(Group {
color: Color::White,
coordinates: vec![
Coordinate { column: 3, row: 8 },
Coordinate { column: 3, row: 9 },
Coordinate { column: 4, row: 9 },
]
.into_iter()
.collect(),
}),
Some(6),
),
];
for (board, coordinate, group, liberties) in test_cases {
@ -427,10 +440,24 @@ mod test {
#[test]
fn opposing_stones_reduce_liberties() {
let mut board = Board::new();
board.place_stone(Coordinate { column: 3, row: 3 }, Color::White);
board.place_stone(Coordinate { column: 3, row: 4 }, Color::Black);
println!("{}", board);
let test_cases = vec![(
Board::from_coordinates(
vec![
(Coordinate { column: 3, row: 3 }, Color::White),
(Coordinate { column: 3, row: 4 }, Color::Black),
]
.into_iter(),
),
Coordinate { column: 3, row: 4 },
Some(6),
)];
println!("{}", test_cases[0].0);
for (board, coordinate, liberties) in test_cases {
assert_eq!(
board.group(coordinate).map(|g| board.liberties(&g)),
liberties
);
}
assert!(false);
}