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