[board] Implement a PositionBuilder; refactor piece bitboards into a PieceBitBoards struct
This makes Position immutable, even if declared mut. I think this will also make it easier to build positions going forward. Moving to a PieceBitBoards struct also required a lot of places that return owned BitBoards to return refs. This is basically fine except for adding a few more & here and there. This was a huge refactoring project. All the move generators and a bunch of BitBoard stuff needed to be updated.
This commit is contained in:
parent
939fac80d7
commit
24cce95a88
17 changed files with 472 additions and 381 deletions
|
@ -66,27 +66,20 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
piece::{Color, Piece},
|
||||
position::DiagramFormatter,
|
||||
BitBoard, Position, Square,
|
||||
};
|
||||
use crate::{piece, position, position::DiagramFormatter, BitBoard, Color};
|
||||
|
||||
#[test]
|
||||
fn classical_single_queen_bitboard() {
|
||||
let mut pos = Position::empty();
|
||||
[(Piece::queen(Color::White), Square::A1)]
|
||||
.into_iter()
|
||||
.for_each(|(p, sq)| {
|
||||
pos.place_piece(p, sq)
|
||||
.expect(&format!("Unable to place {:?} on {}", &p, &sq))
|
||||
});
|
||||
let pos = position![White Queen on B2];
|
||||
|
||||
let generator = ClassicalMoveGenerator::new(&pos, Color::White);
|
||||
let bitboard = generator.bitboard();
|
||||
let expected = BitBoard::new(
|
||||
0b10000001_01000001_00100001_00010001_00001001_00000101_00000011_11111110,
|
||||
);
|
||||
let expected = bitboard![
|
||||
A2, C2, D2, E2, F2, G2, H2, // Rank
|
||||
B1, B3, B4, B5, B6, B7, B8, // File
|
||||
A1, C3, D4, E5, F6, G7, H8, // Diagonal
|
||||
C1, A3 // Anti-diagonal
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
bitboard, expected,
|
||||
|
@ -98,16 +91,10 @@ mod tests {
|
|||
/// Test that a rook can move up to, but not onto, a friendly piece.
|
||||
#[test]
|
||||
fn classical_single_queen_with_same_color_blocker_bitboard() {
|
||||
let mut pos = Position::empty();
|
||||
[
|
||||
(Piece::queen(Color::White), Square::A1),
|
||||
(Piece::knight(Color::White), Square::E1),
|
||||
]
|
||||
.into_iter()
|
||||
.for_each(|(p, sq)| {
|
||||
pos.place_piece(p, sq)
|
||||
.expect(&format!("Unable to place {} on {}", p, sq))
|
||||
});
|
||||
let pos = position![
|
||||
White Queen on A1,
|
||||
White Knight on E1,
|
||||
];
|
||||
|
||||
println!("{}", DiagramFormatter::new(&pos));
|
||||
|
||||
|
@ -127,44 +114,40 @@ mod tests {
|
|||
/// Test that a rook can move up to, and then capture, an enemy piece.
|
||||
#[test]
|
||||
fn classical_single_queen_with_opposing_color_blocker_bitboard() {
|
||||
let mut pos = Position::empty();
|
||||
[
|
||||
(Piece::queen(Color::White), Square::A1),
|
||||
(Piece::knight(Color::Black), Square::E5),
|
||||
]
|
||||
.into_iter()
|
||||
.for_each(|(p, sq)| {
|
||||
pos.place_piece(p, sq)
|
||||
.expect(&format!("Unable to place {} on {}", p, sq))
|
||||
});
|
||||
let pos = position![
|
||||
White Queen on B2,
|
||||
Black Knight on E5,
|
||||
];
|
||||
println!("{}", DiagramFormatter::new(&pos));
|
||||
|
||||
let generator = ClassicalMoveGenerator::new(&pos, Color::White);
|
||||
|
||||
assert_eq!(
|
||||
generator.bitboard(),
|
||||
BitBoard::new(
|
||||
0b00000001_00000001_00000001_00010001_00001001_00000101_00000011_11111110,
|
||||
)
|
||||
bitboard![
|
||||
A2, C2, D2, E2, F2, G2, H2, // Rank
|
||||
B1, B3, B4, B5, B6, B7, B8, // File
|
||||
A1, C3, D4, E5, // Diagonal
|
||||
C1, A3 // Anti-diagonal
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classical_single_queen_in_center() {
|
||||
let mut pos = Position::empty();
|
||||
[(Piece::queen(Color::White), Square::E4)]
|
||||
.into_iter()
|
||||
.for_each(|(p, sq)| {
|
||||
pos.place_piece(p, sq)
|
||||
.expect(&format!("Unable to place {} on {}", p, sq))
|
||||
});
|
||||
let pos = position![White Queen on D3];
|
||||
println!("{}", DiagramFormatter::new(&pos));
|
||||
|
||||
let generator = ClassicalMoveGenerator::new(&pos, Color::White);
|
||||
|
||||
assert_eq!(
|
||||
generator.bitboard(),
|
||||
BitBoard::new(
|
||||
0b00010001_10010010_01010100_00111000_11101111_00111000_01010100_10010010
|
||||
)
|
||||
bitboard![
|
||||
A3, B3, C3, E3, F3, G3, H3, // Rank
|
||||
D1, D2, D4, D5, D6, D7, D8, // File
|
||||
B1, C2, E4, F5, G6, H7, // Diagonal
|
||||
F1, E2, C4, B5, A6, // Anti-diagonal
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue