fix diagonals FULLY
This commit is contained in:
90
src/board.rs
90
src/board.rs
@@ -1,4 +1,7 @@
|
||||
use crate::{misc::split_from, piece::Piece};
|
||||
use crate::{
|
||||
misc::{diag, split_from},
|
||||
piece::Piece,
|
||||
};
|
||||
use std::{cmp::Ordering, fmt};
|
||||
|
||||
pub const BOARD_SIZE: usize = 8;
|
||||
@@ -138,25 +141,8 @@ impl Board {
|
||||
.map(|range| range.into_iter().map(|j| (i, j)).collect()),
|
||||
);
|
||||
|
||||
// TODO! diagonals completely broken :(
|
||||
|
||||
// handle diagonals
|
||||
chains.extend(
|
||||
i_chain
|
||||
.clone()
|
||||
.into_iter()
|
||||
.zip(j_chain.clone())
|
||||
.map(|(i_vec, j_vec)| i_vec.into_iter().zip(j_vec).collect()),
|
||||
);
|
||||
|
||||
// handle top right diagonals too
|
||||
chains.extend(
|
||||
i_chain
|
||||
.into_iter()
|
||||
.rev()
|
||||
.zip(j_chain)
|
||||
.map(|(i_vec, j_vec)| i_vec.into_iter().zip(j_vec).collect()),
|
||||
);
|
||||
chains.extend(diag(i, j, 0, 0, BOARD_SIZE - 1, BOARD_SIZE - 1));
|
||||
|
||||
let mut captured: usize = 0;
|
||||
|
||||
@@ -326,16 +312,15 @@ mod test {
|
||||
fn corner_capture_top_left() {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Black pieces at (0, 0) and (1, 0), and (0, 1) to create capture opportunity
|
||||
// Black pieces at (0, 0) and (2, 2)
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked(1, 0, Piece::White);
|
||||
board.place_unchecked(0, 1, Piece::White);
|
||||
board.place_unchecked(1, 1, Piece::White); // to be captured
|
||||
board.place_unchecked(2, 2, Piece::Black);
|
||||
|
||||
board.propegate_from(0, 0);
|
||||
|
||||
// Capture white piece at (0, 1) and (1, 0)
|
||||
assert_eq!(board.get(0, 1), &Some(Piece::Black));
|
||||
assert_eq!(board.get(1, 0), &Some(Piece::Black));
|
||||
// Capture white piece at (1,1)
|
||||
assert_eq!(board.get(1, 1), &Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test corner capture from top-right corner
|
||||
@@ -343,16 +328,15 @@ mod test {
|
||||
fn corner_capture_top_right() {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Black pieces at (0, 7) and (1, 7), and (0, 6) to create capture opportunity
|
||||
// Black pieces at (0, 7) and (2, 5)
|
||||
board.place_unchecked(0, 7, Piece::Black);
|
||||
board.place_unchecked(1, 7, Piece::White);
|
||||
board.place_unchecked(0, 6, Piece::White);
|
||||
board.place_unchecked(1, 6, Piece::White); // to be captured
|
||||
board.place_unchecked(2, 5, Piece::Black);
|
||||
|
||||
board.propegate_from(0, 7);
|
||||
board.propegate_from(2, 5);
|
||||
|
||||
// Capture white piece at (0, 6) and (1, 7)
|
||||
assert_eq!(board.get(0, 6), &Some(Piece::Black));
|
||||
assert_eq!(board.get(1, 7), &Some(Piece::Black));
|
||||
// Capture white piece at (1, 6)
|
||||
assert_eq!(board.get(1, 6), &Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test corner capture from bottom-left corner
|
||||
@@ -360,16 +344,15 @@ mod test {
|
||||
fn corner_capture_bottom_left() {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Black pieces at (7, 0) and (6, 0), and (7, 1) to create capture opportunity
|
||||
// Black pieces at (7, 0) and (5, 2)
|
||||
board.place_unchecked(7, 0, Piece::Black);
|
||||
board.place_unchecked(6, 0, Piece::White);
|
||||
board.place_unchecked(7, 1, Piece::White);
|
||||
board.place_unchecked(6, 1, Piece::White); // to be captured
|
||||
board.place_unchecked(5, 2, Piece::Black);
|
||||
|
||||
board.propegate_from(7, 0);
|
||||
board.propegate_from(5, 2);
|
||||
|
||||
// Capture white piece at (7, 1) and (6, 0)
|
||||
assert_eq!(board.get(7, 1), &Some(Piece::Black));
|
||||
assert_eq!(board.get(6, 0), &Some(Piece::Black));
|
||||
// Capture white piece at (6, 1)
|
||||
assert_eq!(board.get(6, 1), &Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test corner capture from bottom-right corner
|
||||
@@ -377,16 +360,15 @@ mod test {
|
||||
fn corner_capture_bottom_right() {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Black pieces at (7, 7) and (6, 7), and (7, 6) to create capture opportunity
|
||||
// Black pieces at (7, 7) and (5, 5)
|
||||
board.place_unchecked(7, 7, Piece::Black);
|
||||
board.place_unchecked(6, 7, Piece::White);
|
||||
board.place_unchecked(7, 6, Piece::White);
|
||||
board.place_unchecked(6, 6, Piece::White); // to be captured
|
||||
board.place_unchecked(5, 5, Piece::Black);
|
||||
|
||||
board.propegate_from(7, 7);
|
||||
board.propegate_from(5, 5);
|
||||
|
||||
// Capture white piece at (7, 6) and (6, 7)
|
||||
assert_eq!(board.get(7, 6), &Some(Piece::Black));
|
||||
assert_eq!(board.get(6, 7), &Some(Piece::Black));
|
||||
// Capture white piece at (6, 6)
|
||||
assert_eq!(board.get(6, 6), &Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test capture from top-left corner (horizontal)
|
||||
@@ -396,12 +378,12 @@ mod test {
|
||||
|
||||
// Create a scenario where a capture should happen horizontally from (0, 0)
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked(0, 1, Piece::White);
|
||||
board.place_unchecked(0, 1, Piece::White); // to be captured
|
||||
board.place_unchecked(0, 2, Piece::Black);
|
||||
|
||||
board.propegate_from(0, 2);
|
||||
|
||||
assert_eq!(board.get(0, 1), &Some(Piece::Black));
|
||||
assert_eq!(board.get(0, 1), &Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test capture from top-right corner (horizontal)
|
||||
@@ -411,12 +393,12 @@ mod test {
|
||||
|
||||
// Create a scenario where a capture should happen horizontally from (0, 7)
|
||||
board.place_unchecked(0, 7, Piece::Black);
|
||||
board.place_unchecked(0, 6, Piece::White);
|
||||
board.place_unchecked(0, 6, Piece::White); // to be captured
|
||||
board.place_unchecked(0, 5, Piece::Black);
|
||||
|
||||
board.propegate_from(0, 5);
|
||||
|
||||
assert_eq!(board.get(0, 6), &Some(Piece::Black));
|
||||
assert_eq!(board.get(0, 6), &Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test capture from top-left corner (vertical)
|
||||
@@ -426,12 +408,12 @@ mod test {
|
||||
|
||||
// Create a scenario where a capture should happen vertically from (0, 0)
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked(1, 0, Piece::White);
|
||||
board.place_unchecked(1, 0, Piece::White); // to be captured
|
||||
board.place_unchecked(2, 0, Piece::Black);
|
||||
|
||||
board.propegate_from(2, 0);
|
||||
|
||||
assert_eq!(board.get(1, 0), &Some(Piece::Black));
|
||||
assert_eq!(board.get(1, 0), &Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test capture from bottom-left corner (vertical)
|
||||
@@ -441,11 +423,11 @@ mod test {
|
||||
|
||||
// Create a scenario where a capture should happen vertically from (7, 0)
|
||||
board.place_unchecked(7, 0, Piece::Black);
|
||||
board.place_unchecked(6, 0, Piece::White);
|
||||
board.place_unchecked(6, 0, Piece::White); // to be captured
|
||||
board.place_unchecked(5, 0, Piece::Black);
|
||||
|
||||
board.propegate_from(5, 0);
|
||||
|
||||
assert_eq!(board.get(6, 0), &Some(Piece::Black));
|
||||
assert_eq!(board.get(6, 0), &Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user