int generate_black_pawn_captures(const Position&, MoveStack*);
int generate_white_pawn_noncaptures(const Position&, MoveStack*);
int generate_black_pawn_noncaptures(const Position&, MoveStack*);
- int generate_knight_moves(const Position&, MoveStack*, Color side, Bitboard t);
- int generate_bishop_moves(const Position&, MoveStack*, Color side, Bitboard t);
- int generate_rook_moves(const Position&, MoveStack*, Color side, Bitboard t);
- int generate_queen_moves(const Position&, MoveStack*, Color side, Bitboard t);
+ int generate_piece_moves(PieceType, const Position&, MoveStack*, Color side, Bitboard t);
int generate_king_moves(const Position&, MoveStack*, Square from, Bitboard t);
int generate_castle_moves(const Position&, MoveStack*, Color us);
else
n = generate_black_pawn_captures(pos, mlist);
- n += generate_knight_moves(pos, mlist+n, us, target);
- n += generate_bishop_moves(pos, mlist+n, us, target);
- n += generate_rook_moves(pos, mlist+n, us, target);
- n += generate_queen_moves(pos, mlist+n, us, target);
+ for (PieceType pce = KNIGHT; pce < KING; pce++)
+ n += generate_piece_moves(pce, pos, mlist+n, us, target);
+
n += generate_king_moves(pos, mlist+n, pos.king_square(us), target);
return n;
}
else
n = generate_black_pawn_noncaptures(pos, mlist);
- n += generate_knight_moves(pos, mlist+n, us, target);
- n += generate_bishop_moves(pos, mlist+n, us, target);
- n += generate_rook_moves(pos, mlist+n, us, target);
- n += generate_queen_moves(pos, mlist+n, us, target);
+ for (PieceType pce = KNIGHT; pce < KING; pce++)
+ n += generate_piece_moves(pce, pos, mlist+n, us, target);
+
n += generate_king_moves(pos, mlist+n, pos.king_square(us), target);
n += generate_castle_moves(pos, mlist+n, us);
return n;
return MOVE_NONE;
// Proceed according to the type of the moving piece.
- switch(type_of_piece(pc)) {
+ if (type_of_piece(pc) == PAWN)
+ {
+ // If the destination square is on the 8/1th rank, the move must
+ // be a promotion.
+ if ( ( (square_rank(to) == RANK_8 && us == WHITE)
+ ||(square_rank(to) == RANK_1 && us != WHITE))
+ && !move_promotion(m))
+ return MOVE_NONE;
- case PAWN:
- // Pawn moves, as usual, are somewhat messy.
- if (us == WHITE)
- {
- // If the destination square is on the 8th rank, the move must
- // be a promotion.
- if (square_rank(to) == RANK_8 && !move_promotion(m))
- return MOVE_NONE;
-
- // Proceed according to the square delta between the source and
- // destionation squares.
- switch (to - from)
- {
- case DELTA_NW:
- case DELTA_NE:
- // Capture. The destination square must be occupied by an enemy
- // piece (en passant captures was handled earlier).
- if (pos.color_of_piece_on(to) != them)
- return MOVE_NONE;
- break;
-
- case DELTA_N:
- // Pawn push. The destination square must be empty.
- if (!pos.square_is_empty(to))
- return MOVE_NONE;
- break;
-
- case DELTA_NN:
- // Double pawn push. The destination square must be on the fourth
- // rank, and both the destination square and the square between the
- // source and destination squares must be empty.
- if ( square_rank(to) != RANK_4
- || !pos.square_is_empty(to)
- || !pos.square_is_empty(from + DELTA_N))
- return MOVE_NONE;
- break;
-
- default:
- return MOVE_NONE;
- }
- }
- else // (us == BLACK)
+ // Proceed according to the square delta between the source and
+ // destionation squares.
+ switch (to - from)
{
- // If the destination square is on the 1th rank, the move must
- // be a promotion.
- if (square_rank(to) == RANK_1 && !move_promotion(m))
- return MOVE_NONE;
-
- // Proceed according to the square delta between the source and
- // destionation squares.
- switch (to - from)
- {
- case DELTA_SW:
- case DELTA_SE:
- // Capture. The destination square must be occupied by an enemy
- // piece (en passant captures was handled earlier).
- if (pos.color_of_piece_on(to) != them)
- return MOVE_NONE;
- break;
-
- case DELTA_S:
- // Pawn push. The destination square must be empty.
- if (!pos.square_is_empty(to))
- return MOVE_NONE;
- break;
-
- case DELTA_SS:
- // Double pawn push. The destination square must be on the fifth
- // rank, and both the destination square and the square between the
- // source and destination squares must be empty.
- if ( square_rank(to) != RANK_5
- || !pos.square_is_empty(to)
- || !pos.square_is_empty(from + DELTA_S))
- return MOVE_NONE;
- break;
-
- default:
+ case DELTA_NW:
+ case DELTA_NE:
+ case DELTA_SW:
+ case DELTA_SE:
+ // Capture. The destination square must be occupied by an enemy
+ // piece (en passant captures was handled earlier).
+ if (pos.color_of_piece_on(to) != them)
return MOVE_NONE;
- }
- }
- // The move is pseudo-legal. Return it if it is legal.
- return (pos.move_is_legal(m) ? m : MOVE_NONE);
- break;
-
- case KNIGHT:
- return ( pos.knight_attacks_square(from, to)
- && pos.move_is_legal(m)
- && !move_promotion(m) ? m : MOVE_NONE);
- break;
-
- case BISHOP:
- return ( pos.bishop_attacks_square(from, to)
- && pos.move_is_legal(m)
- && !move_promotion(m) ? m : MOVE_NONE);
break;
-
- case ROOK:
- return ( pos.rook_attacks_square(from, to)
- && pos.move_is_legal(m)
- && !move_promotion(m) ? m : MOVE_NONE);
+
+ case DELTA_N:
+ case DELTA_S:
+ // Pawn push. The destination square must be empty.
+ if (!pos.square_is_empty(to))
+ return MOVE_NONE;
break;
-
- case QUEEN:
- return ( pos.queen_attacks_square(from, to)
- && pos.move_is_legal(m)
- && !move_promotion(m) ? m : MOVE_NONE);
+
+ case DELTA_NN:
+ // Double white pawn push. The destination square must be on the fourth
+ // rank, and both the destination square and the square between the
+ // source and destination squares must be empty.
+ if ( square_rank(to) != RANK_4
+ || !pos.square_is_empty(to)
+ || !pos.square_is_empty(from + DELTA_N))
+ return MOVE_NONE;
break;
- case KING:
- return ( pos.king_attacks_square(from, to)
- && pos.move_is_legal(m)
- && !move_promotion(m) ? m : MOVE_NONE);
+ case DELTA_SS:
+ // Double black pawn push. The destination square must be on the fifth
+ // rank, and both the destination square and the square between the
+ // source and destination squares must be empty.
+ if ( square_rank(to) != RANK_5
+ || !pos.square_is_empty(to)
+ || !pos.square_is_empty(from + DELTA_S))
+ return MOVE_NONE;
break;
default:
- assert(false);
- }
- assert(false);
- return MOVE_NONE;
+ return MOVE_NONE;
+ }
+ // The move is pseudo-legal. Return it if it is legal.
+ return (pos.move_is_legal(m) ? m : MOVE_NONE);
+ }
+
+ // Luckly we can handle all the other pieces in one go
+ return ( pos.piece_attacks_square(from, to)
+ && pos.move_is_legal(m)
+ && !move_promotion(m) ? m : MOVE_NONE);
}
return n;
}
-
- int generate_knight_moves(const Position &pos, MoveStack *mlist,
- Color side, Bitboard target) {
- Square from, to;
- Bitboard b;
- int i, n = 0;
-
- for(i = 0; i < pos.knight_count(side); i++) {
- from = pos.knight_list(side, i);
- b = pos.knight_attacks(from) & target;
- while(b) {
- to = pop_1st_bit(&b);
- mlist[n++].move = make_move(from, to);
- }
- }
- return n;
- }
-
-
- int generate_bishop_moves(const Position &pos, MoveStack *mlist,
- Color side, Bitboard target) {
- Square from, to;
- Bitboard b;
- int i, n = 0;
-
- for(i = 0; i < pos.bishop_count(side); i++) {
- from = pos.bishop_list(side, i);
- b = pos.bishop_attacks(from) & target;
- while(b) {
- to = pop_1st_bit(&b);
- mlist[n++].move = make_move(from, to);
- }
- }
- return n;
- }
-
-
- int generate_rook_moves(const Position &pos, MoveStack *mlist,
- Color side, Bitboard target) {
- Square from, to;
- Bitboard b;
- int i, n = 0;
-
- for(i = 0; i < pos.rook_count(side); i++) {
- from = pos.rook_list(side, i);
- b = pos.rook_attacks(from) & target;
- while(b) {
- to = pop_1st_bit(&b);
- mlist[n++].move = make_move(from, to);
- }
- }
- return n;
- }
-
-
- int generate_queen_moves(const Position &pos, MoveStack *mlist,
+ int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist,
Color side, Bitboard target) {
+
+ const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
Square from, to;
Bitboard b;
- int i, n = 0;
+ int n = 0;
- for(i = 0; i < pos.queen_count(side); i++) {
- from = pos.queen_list(side, i);
- b = pos.queen_attacks(from) & target;
- while(b) {
- to = pop_1st_bit(&b);
- mlist[n++].move = make_move(from, to);
- }
+ for (int i = 0; i < pos.piece_count(side, piece); i++)
+ {
+ from = pos.piece_list(side, piece, i);
+ b = (pos.*mem_fn)(from) & target;
+ while (b)
+ {
+ to = pop_1st_bit(&b);
+ mlist[n++].move = make_move(from, to);
+ }
}
return n;
}
return n;
}
-
}