mirror of
https://github.com/sockspls/badfish
synced 2025-04-29 16:23:09 +00:00
Use pointer-to-members to remove a bunch of duplicated code
Remove all generate_XXX_moves() functions, use an array of pointer to members instead. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
94f1b31484
commit
aa7121297d
4 changed files with 35 additions and 76 deletions
|
@ -52,6 +52,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
// Initialization
|
// Initialization
|
||||||
|
|
||||||
|
init_piece_attacks_fn();
|
||||||
init_mersenne();
|
init_mersenne();
|
||||||
init_direction_table();
|
init_direction_table();
|
||||||
init_bitboards();
|
init_bitboards();
|
||||||
|
|
|
@ -36,10 +36,7 @@ namespace {
|
||||||
int generate_black_pawn_captures(const Position&, MoveStack*);
|
int generate_black_pawn_captures(const Position&, MoveStack*);
|
||||||
int generate_white_pawn_noncaptures(const Position&, MoveStack*);
|
int generate_white_pawn_noncaptures(const Position&, MoveStack*);
|
||||||
int generate_black_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_piece_moves(PieceType, 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_king_moves(const Position&, MoveStack*, Square from, Bitboard t);
|
int generate_king_moves(const Position&, MoveStack*, Square from, Bitboard t);
|
||||||
int generate_castle_moves(const Position&, MoveStack*, Color us);
|
int generate_castle_moves(const Position&, MoveStack*, Color us);
|
||||||
|
|
||||||
|
@ -68,10 +65,9 @@ int generate_captures(const Position& pos, MoveStack* mlist) {
|
||||||
else
|
else
|
||||||
n = generate_black_pawn_captures(pos, mlist);
|
n = generate_black_pawn_captures(pos, mlist);
|
||||||
|
|
||||||
n += generate_knight_moves(pos, mlist+n, us, target);
|
for (PieceType pce = KNIGHT; pce < KING; pce++)
|
||||||
n += generate_bishop_moves(pos, mlist+n, us, target);
|
n += generate_piece_moves(pce, pos, mlist+n, us, target);
|
||||||
n += generate_rook_moves(pos, mlist+n, us, target);
|
|
||||||
n += generate_queen_moves(pos, mlist+n, us, target);
|
|
||||||
n += generate_king_moves(pos, mlist+n, pos.king_square(us), target);
|
n += generate_king_moves(pos, mlist+n, pos.king_square(us), target);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
@ -94,10 +90,9 @@ int generate_noncaptures(const Position& pos, MoveStack *mlist) {
|
||||||
else
|
else
|
||||||
n = generate_black_pawn_noncaptures(pos, mlist);
|
n = generate_black_pawn_noncaptures(pos, mlist);
|
||||||
|
|
||||||
n += generate_knight_moves(pos, mlist+n, us, target);
|
for (PieceType pce = KNIGHT; pce < KING; pce++)
|
||||||
n += generate_bishop_moves(pos, mlist+n, us, target);
|
n += generate_piece_moves(pce, pos, mlist+n, us, target);
|
||||||
n += generate_rook_moves(pos, mlist+n, us, target);
|
|
||||||
n += generate_queen_moves(pos, mlist+n, us, target);
|
|
||||||
n += generate_king_moves(pos, mlist+n, pos.king_square(us), target);
|
n += generate_king_moves(pos, mlist+n, pos.king_square(us), target);
|
||||||
n += generate_castle_moves(pos, mlist+n, us);
|
n += generate_castle_moves(pos, mlist+n, us);
|
||||||
return n;
|
return n;
|
||||||
|
@ -1007,74 +1002,22 @@ namespace {
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist,
|
||||||
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,
|
|
||||||
Color side, Bitboard target) {
|
Color side, Bitboard target) {
|
||||||
Square from, to;
|
Square from, to;
|
||||||
Bitboard b;
|
Bitboard b;
|
||||||
int i, n = 0;
|
Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
for(i = 0; i < pos.queen_count(side); i++) {
|
for (int i = 0; i < pos.piece_count(side, piece); i++)
|
||||||
from = pos.queen_list(side, i);
|
{
|
||||||
b = pos.queen_attacks(from) & target;
|
from = pos.piece_list(side, piece, i);
|
||||||
while(b) {
|
b = (pos.*mem_fn)(from) & target;
|
||||||
to = pop_1st_bit(&b);
|
while (b)
|
||||||
mlist[n++].move = make_move(from, to);
|
{
|
||||||
}
|
to = pop_1st_bit(&b);
|
||||||
|
mlist[n++].move = make_move(from, to);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,11 +48,21 @@ Key Position::zobSideToMove;
|
||||||
Value Position::MgPieceSquareTable[16][64];
|
Value Position::MgPieceSquareTable[16][64];
|
||||||
Value Position::EgPieceSquareTable[16][64];
|
Value Position::EgPieceSquareTable[16][64];
|
||||||
|
|
||||||
|
Piece_attacks_fn piece_attacks_fn[7];
|
||||||
|
|
||||||
////
|
////
|
||||||
//// Functions
|
//// Functions
|
||||||
////
|
////
|
||||||
|
|
||||||
|
void init_piece_attacks_fn() {
|
||||||
|
|
||||||
|
piece_attacks_fn[KNIGHT] = &Position::knight_attacks;
|
||||||
|
piece_attacks_fn[BISHOP] = &Position::bishop_attacks;
|
||||||
|
piece_attacks_fn[ROOK] = &Position::rook_attacks;
|
||||||
|
piece_attacks_fn[QUEEN] = &Position::queen_attacks;
|
||||||
|
piece_attacks_fn[KING] = &Position::king_attacks;
|
||||||
|
}
|
||||||
|
|
||||||
/// Constructors
|
/// Constructors
|
||||||
|
|
||||||
Position::Position(const Position &pos) {
|
Position::Position(const Position &pos) {
|
||||||
|
|
|
@ -370,6 +370,11 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// An array of member functions to dispatch attacks_square
|
||||||
|
typedef Bitboard (Position::* Piece_attacks_fn)(Square s) const;
|
||||||
|
extern Piece_attacks_fn piece_attacks_fn[7];
|
||||||
|
extern void init_piece_attacks_fn();
|
||||||
|
|
||||||
////
|
////
|
||||||
//// Inline functions
|
//// Inline functions
|
||||||
////
|
////
|
||||||
|
|
Loading…
Add table
Reference in a new issue