board logic changes
This commit is contained in:
parent
0fea3da31c
commit
3096c4d85b
57
src/board.rs
57
src/board.rs
@ -202,6 +202,11 @@ impl Board {
|
|||||||
self.board_mut(!piece).set(i, j, false);
|
self.board_mut(!piece).set(i, j, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn delete(&mut self, i: usize, j: usize) {
|
||||||
|
self.board_mut(Piece::White).set(i, j, false);
|
||||||
|
self.board_mut(Piece::Black).set(i, j, false);
|
||||||
|
}
|
||||||
|
|
||||||
/// Return a modified [`Board`] with the piece placed at a position
|
/// Return a modified [`Board`] with the piece placed at a position
|
||||||
/// Returns None if the move was invalid
|
/// Returns None if the move was invalid
|
||||||
pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Option<Self> {
|
pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Option<Self> {
|
||||||
@ -225,12 +230,17 @@ impl Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), &'static str> {
|
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), &'static str> {
|
||||||
let what_if_result = self
|
if self.get(i, j).is_some() {
|
||||||
.what_if(i, j, piece)
|
return Err("position is occupied");
|
||||||
.ok_or("move would not propegate")?;
|
}
|
||||||
|
self.place_unchecked(i, j, piece);
|
||||||
*self = what_if_result;
|
let captured = self.propegate_from(i, j);
|
||||||
|
if captured > 0 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
self.delete(i, j);
|
||||||
|
return Err("move would not propegate");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Propegate the board and captures starting from a specific position
|
/// Propegate the board and captures starting from a specific position
|
||||||
@ -378,8 +388,7 @@ mod test {
|
|||||||
board.place_unchecked(i, 0, Piece::White);
|
board.place_unchecked(i, 0, Piece::White);
|
||||||
}
|
}
|
||||||
|
|
||||||
board.place_unchecked(7, 0, Piece::Black);
|
assert_eq!(board.place(7, 0, Piece::Black), Ok(()));
|
||||||
board.propegate_from(7, 0);
|
|
||||||
|
|
||||||
for i in 2..=6 {
|
for i in 2..=6 {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -397,12 +406,10 @@ mod test {
|
|||||||
fn corner_capture_top_left() {
|
fn corner_capture_top_left() {
|
||||||
let mut board = Board::new();
|
let mut board = Board::new();
|
||||||
|
|
||||||
// Black pieces at (0, 0) and (2, 2)
|
// Black pieces at (2, 2) and (0, 0)
|
||||||
board.place_unchecked(0, 0, Piece::Black);
|
|
||||||
board.place_unchecked(1, 1, Piece::White); // to be captured
|
board.place_unchecked(1, 1, Piece::White); // to be captured
|
||||||
board.place_unchecked(2, 2, Piece::Black);
|
board.place_unchecked(2, 2, Piece::Black);
|
||||||
|
assert_eq!(board.place(0, 0, Piece::Black), Ok(()));
|
||||||
board.propegate_from(0, 0);
|
|
||||||
|
|
||||||
// Capture white piece at (1,1)
|
// Capture white piece at (1,1)
|
||||||
assert_eq!(board.get(1, 1), Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(1, 1), Some(Piece::Black), "\n{}", board);
|
||||||
@ -416,9 +423,7 @@ mod test {
|
|||||||
// Black pieces at (0, 7) and (2, 5)
|
// Black pieces at (0, 7) and (2, 5)
|
||||||
board.place_unchecked(0, 7, Piece::Black);
|
board.place_unchecked(0, 7, Piece::Black);
|
||||||
board.place_unchecked(1, 6, Piece::White); // to be captured
|
board.place_unchecked(1, 6, Piece::White); // to be captured
|
||||||
board.place_unchecked(2, 5, Piece::Black);
|
assert_eq!(board.place(2, 5, Piece::Black), Ok(()));
|
||||||
|
|
||||||
board.propegate_from(2, 5);
|
|
||||||
|
|
||||||
// Capture white piece at (1, 6)
|
// Capture white piece at (1, 6)
|
||||||
assert_eq!(board.get(1, 6), Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(1, 6), Some(Piece::Black), "\n{}", board);
|
||||||
@ -432,9 +437,7 @@ mod test {
|
|||||||
// Black pieces at (7, 0) and (5, 2)
|
// Black pieces at (7, 0) and (5, 2)
|
||||||
board.place_unchecked(7, 0, Piece::Black);
|
board.place_unchecked(7, 0, Piece::Black);
|
||||||
board.place_unchecked(6, 1, Piece::White); // to be captured
|
board.place_unchecked(6, 1, Piece::White); // to be captured
|
||||||
board.place_unchecked(5, 2, Piece::Black);
|
assert_eq!(board.place(5, 2, Piece::Black), Ok(()));
|
||||||
|
|
||||||
board.propegate_from(5, 2);
|
|
||||||
|
|
||||||
// Capture white piece at (6, 1)
|
// Capture white piece at (6, 1)
|
||||||
assert_eq!(board.get(6, 1), Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(6, 1), Some(Piece::Black), "\n{}", board);
|
||||||
@ -448,9 +451,7 @@ mod test {
|
|||||||
// Black pieces at (7, 7) and (5, 5)
|
// Black pieces at (7, 7) and (5, 5)
|
||||||
board.place_unchecked(7, 7, Piece::Black);
|
board.place_unchecked(7, 7, Piece::Black);
|
||||||
board.place_unchecked(6, 6, Piece::White); // to be captured
|
board.place_unchecked(6, 6, Piece::White); // to be captured
|
||||||
board.place_unchecked(5, 5, Piece::Black);
|
assert_eq!(board.place(5, 5, Piece::Black), Ok(()));
|
||||||
|
|
||||||
board.propegate_from(5, 5);
|
|
||||||
|
|
||||||
// Capture white piece at (6, 6)
|
// Capture white piece at (6, 6)
|
||||||
assert_eq!(board.get(6, 6), Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(6, 6), Some(Piece::Black), "\n{}", board);
|
||||||
@ -464,9 +465,7 @@ mod test {
|
|||||||
// Create a scenario where a capture should happen horizontally from (0, 0)
|
// Create a scenario where a capture should happen horizontally from (0, 0)
|
||||||
board.place_unchecked(0, 0, Piece::Black);
|
board.place_unchecked(0, 0, Piece::Black);
|
||||||
board.place_unchecked(0, 1, Piece::White); // to be captured
|
board.place_unchecked(0, 1, Piece::White); // to be captured
|
||||||
board.place_unchecked(0, 2, Piece::Black);
|
assert_eq!(board.place(0, 2, Piece::Black), Ok(()));
|
||||||
|
|
||||||
board.propegate_from(0, 2);
|
|
||||||
|
|
||||||
assert_eq!(board.get(0, 1), Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(0, 1), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
@ -479,9 +478,7 @@ mod test {
|
|||||||
// Create a scenario where a capture should happen horizontally from (0, 7)
|
// Create a scenario where a capture should happen horizontally from (0, 7)
|
||||||
board.place_unchecked(0, 7, Piece::Black);
|
board.place_unchecked(0, 7, Piece::Black);
|
||||||
board.place_unchecked(0, 6, Piece::White); // to be captured
|
board.place_unchecked(0, 6, Piece::White); // to be captured
|
||||||
board.place_unchecked(0, 5, Piece::Black);
|
assert_eq!(board.place(0, 5, Piece::Black), Ok(()));
|
||||||
|
|
||||||
board.propegate_from(0, 5);
|
|
||||||
|
|
||||||
assert_eq!(board.get(0, 6), Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(0, 6), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
@ -494,9 +491,7 @@ mod test {
|
|||||||
// Create a scenario where a capture should happen vertically from (0, 0)
|
// Create a scenario where a capture should happen vertically from (0, 0)
|
||||||
board.place_unchecked(0, 0, Piece::Black);
|
board.place_unchecked(0, 0, Piece::Black);
|
||||||
board.place_unchecked(1, 0, Piece::White); // to be captured
|
board.place_unchecked(1, 0, Piece::White); // to be captured
|
||||||
board.place_unchecked(2, 0, Piece::Black);
|
assert_eq!(board.place(2, 0, Piece::Black), Ok(()));
|
||||||
|
|
||||||
board.propegate_from(2, 0);
|
|
||||||
|
|
||||||
assert_eq!(board.get(1, 0), Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(1, 0), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
@ -509,9 +504,7 @@ mod test {
|
|||||||
// Create a scenario where a capture should happen vertically from (7, 0)
|
// Create a scenario where a capture should happen vertically from (7, 0)
|
||||||
board.place_unchecked(7, 0, Piece::Black);
|
board.place_unchecked(7, 0, Piece::Black);
|
||||||
board.place_unchecked(6, 0, Piece::White); // to be captured
|
board.place_unchecked(6, 0, Piece::White); // to be captured
|
||||||
board.place_unchecked(5, 0, Piece::Black);
|
assert_eq!(board.place(5, 0, Piece::Black), Ok(()));
|
||||||
|
|
||||||
board.propegate_from(5, 0);
|
|
||||||
|
|
||||||
assert_eq!(board.get(6, 0), Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(6, 0), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user