feat: pawn promotion movegen
This commit is contained in:
parent
5ea9a0d184
commit
35f0585979
@ -395,11 +395,38 @@ impl PseudoMoveGen for BoardState {
|
|||||||
for src in squares!(Pawn) {
|
for src in squares!(Pawn) {
|
||||||
let (r, c) = src.to_row_col();
|
let (r, c) = src.to_row_col();
|
||||||
|
|
||||||
|
let last_row = match self.turn {
|
||||||
|
Color::White => BOARD_HEIGHT as isize - 1,
|
||||||
|
Color::Black => 0,
|
||||||
|
};
|
||||||
|
|
||||||
let nr = (r as isize)
|
let nr = (r as isize)
|
||||||
+ match self.turn {
|
+ match self.turn {
|
||||||
Color::White => 1,
|
Color::White => 1,
|
||||||
Color::Black => -1,
|
Color::Black => -1,
|
||||||
};
|
};
|
||||||
|
let is_promotion = nr == last_row;
|
||||||
|
|
||||||
|
macro_rules! push_moves {
|
||||||
|
($src: ident, $dest: ident) => {
|
||||||
|
if is_promotion {
|
||||||
|
use PromotePiece::*;
|
||||||
|
for prom_pc in [Queen, Knight, Rook, Bishop] {
|
||||||
|
ret.push(Move {
|
||||||
|
$src,
|
||||||
|
$dest,
|
||||||
|
move_type: MoveType::Promotion(prom_pc),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ret.push(Move {
|
||||||
|
$src,
|
||||||
|
$dest,
|
||||||
|
move_type: MoveType::Normal,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// capture
|
// capture
|
||||||
for horiz in [-1, 1] {
|
for horiz in [-1, 1] {
|
||||||
@ -409,11 +436,7 @@ impl PseudoMoveGen for BoardState {
|
|||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
};
|
};
|
||||||
if self.get_piece(dest).is_some() || self.ep_square == Some(dest) {
|
if self.get_piece(dest).is_some() || self.ep_square == Some(dest) {
|
||||||
ret.push(Move {
|
push_moves!(src, dest);
|
||||||
src,
|
|
||||||
dest,
|
|
||||||
move_type: MoveType::Normal,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,11 +448,7 @@ impl PseudoMoveGen for BoardState {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if self.get_piece(dest).is_none() {
|
if self.get_piece(dest).is_none() {
|
||||||
ret.push(Move {
|
push_moves!(src, dest);
|
||||||
src,
|
|
||||||
dest,
|
|
||||||
move_type: MoveType::Normal,
|
|
||||||
});
|
|
||||||
|
|
||||||
// double push
|
// double push
|
||||||
if r == match self.turn {
|
if r == match self.turn {
|
||||||
@ -445,11 +464,7 @@ impl PseudoMoveGen for BoardState {
|
|||||||
let dest = Square::from_row_col_signed(nr, nc)
|
let dest = Square::from_row_col_signed(nr, nc)
|
||||||
.expect("Pawn double push should have valid destination");
|
.expect("Pawn double push should have valid destination");
|
||||||
if self.get_piece(dest).is_none() {
|
if self.get_piece(dest).is_none() {
|
||||||
ret.push(Move {
|
push_moves!(src, dest);
|
||||||
src,
|
|
||||||
dest,
|
|
||||||
move_type: MoveType::Normal,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -489,6 +504,42 @@ mod tests {
|
|||||||
MoveType::Normal,
|
MoveType::Normal,
|
||||||
)],
|
)],
|
||||||
),
|
),
|
||||||
|
// white pawn promotion
|
||||||
|
(
|
||||||
|
"q1q5/1P6/8/8/8/8/8/8 w - - 0 1",
|
||||||
|
vec![
|
||||||
|
("b7", vec!["b8"], MoveType::Promotion(PromotePiece::Rook)),
|
||||||
|
("b7", vec!["b8"], MoveType::Promotion(PromotePiece::Queen)),
|
||||||
|
("b7", vec!["b8"], MoveType::Promotion(PromotePiece::Bishop)),
|
||||||
|
("b7", vec!["b8"], MoveType::Promotion(PromotePiece::Knight)),
|
||||||
|
("b7", vec!["a8"], MoveType::Promotion(PromotePiece::Rook)),
|
||||||
|
("b7", vec!["a8"], MoveType::Promotion(PromotePiece::Queen)),
|
||||||
|
("b7", vec!["a8"], MoveType::Promotion(PromotePiece::Bishop)),
|
||||||
|
("b7", vec!["a8"], MoveType::Promotion(PromotePiece::Knight)),
|
||||||
|
("b7", vec!["c8"], MoveType::Promotion(PromotePiece::Rook)),
|
||||||
|
("b7", vec!["c8"], MoveType::Promotion(PromotePiece::Queen)),
|
||||||
|
("b7", vec!["c8"], MoveType::Promotion(PromotePiece::Bishop)),
|
||||||
|
("b7", vec!["c8"], MoveType::Promotion(PromotePiece::Knight)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
// black pawn promotion
|
||||||
|
(
|
||||||
|
"8/8/8/8/8/8/1p6/Q1Q5 b - - 0 1",
|
||||||
|
vec![
|
||||||
|
("b2", vec!["b1"], MoveType::Promotion(PromotePiece::Rook)),
|
||||||
|
("b2", vec!["b1"], MoveType::Promotion(PromotePiece::Queen)),
|
||||||
|
("b2", vec!["b1"], MoveType::Promotion(PromotePiece::Bishop)),
|
||||||
|
("b2", vec!["b1"], MoveType::Promotion(PromotePiece::Knight)),
|
||||||
|
("b2", vec!["a1"], MoveType::Promotion(PromotePiece::Rook)),
|
||||||
|
("b2", vec!["a1"], MoveType::Promotion(PromotePiece::Queen)),
|
||||||
|
("b2", vec!["a1"], MoveType::Promotion(PromotePiece::Bishop)),
|
||||||
|
("b2", vec!["a1"], MoveType::Promotion(PromotePiece::Knight)),
|
||||||
|
("b2", vec!["c1"], MoveType::Promotion(PromotePiece::Rook)),
|
||||||
|
("b2", vec!["c1"], MoveType::Promotion(PromotePiece::Queen)),
|
||||||
|
("b2", vec!["c1"], MoveType::Promotion(PromotePiece::Bishop)),
|
||||||
|
("b2", vec!["c1"], MoveType::Promotion(PromotePiece::Knight)),
|
||||||
|
],
|
||||||
|
),
|
||||||
// white pawn push/capture
|
// white pawn push/capture
|
||||||
(
|
(
|
||||||
"8/8/8/8/8/p1p5/1P6/8 w - - 0 1",
|
"8/8/8/8/8/p1p5/1P6/8 w - - 0 1",
|
||||||
@ -502,7 +553,10 @@ mod tests {
|
|||||||
// white pawn blocked
|
// white pawn blocked
|
||||||
("8/8/8/8/8/1p6/1P6/8 w - - 0 1", vec![]),
|
("8/8/8/8/8/1p6/1P6/8 w - - 0 1", vec![]),
|
||||||
// white pawn blocked (partially)
|
// white pawn blocked (partially)
|
||||||
("8/8/8/8/1p6/8/1P6/8 w - - 0 1", vec![("b2", vec!["b3"], MoveType::Normal)]),
|
(
|
||||||
|
"8/8/8/8/1p6/8/1P6/8 w - - 0 1",
|
||||||
|
vec![("b2", vec!["b3"], MoveType::Normal)],
|
||||||
|
),
|
||||||
// black pawn push/capture
|
// black pawn push/capture
|
||||||
(
|
(
|
||||||
"8/1p6/P1P5/8/8/8/8/8 b - - 0 1",
|
"8/1p6/P1P5/8/8/8/8/8 b - - 0 1",
|
||||||
|
Loading…
Reference in New Issue
Block a user