1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Syntactic sugar to loop across pieces

Also add some comments to the new operator~(Piece).

No functional change.
This commit is contained in:
Marco Costalba 2016-09-04 15:29:11 +02:00
parent ca6c9f85a5
commit e340ce221c
2 changed files with 22 additions and 23 deletions

View file

@ -109,10 +109,9 @@ void Position::init() {
PRNG rng(1070372); PRNG rng(1070372);
for (Color c = WHITE; c <= BLACK; ++c) for (Piece pc : Pieces)
for (PieceType pt = PAWN; pt <= KING; ++pt) for (Square s = SQ_A1; s <= SQ_H8; ++s)
for (Square s = SQ_A1; s <= SQ_H8; ++s) Zobrist::psq[pc][s] = rng.rand<Key>();
Zobrist::psq[make_piece(c, pt)][s] = rng.rand<Key>();
for (File f = FILE_A; f <= FILE_H; ++f) for (File f = FILE_A; f <= FILE_H; ++f)
Zobrist::enpassant[f] = rng.rand<Key>(); Zobrist::enpassant[f] = rng.rand<Key>();
@ -342,14 +341,14 @@ void Position::set_state(StateInfo* si) const {
si->pawnKey ^= Zobrist::psq[piece_on(s)][s]; si->pawnKey ^= Zobrist::psq[piece_on(s)][s];
} }
for (Color c = WHITE; c <= BLACK; ++c) for (Piece pc : Pieces)
for (PieceType pt = PAWN; pt <= KING; ++pt) {
for (int cnt = 0; cnt < pieceCount[make_piece(c, pt)]; ++cnt) if (type_of(pc) != PAWN && type_of(pc) != KING)
si->materialKey ^= Zobrist::psq[make_piece(c, pt)][cnt]; si->nonPawnMaterial[color_of(pc)] += pieceCount[pc] * PieceValue[MG][pc];
for (Color c = WHITE; c <= BLACK; ++c) for (int cnt = 0; cnt < pieceCount[pc]; ++cnt)
for (PieceType pt = KNIGHT; pt <= QUEEN; ++pt) si->materialKey ^= Zobrist::psq[pc][cnt];
si->nonPawnMaterial[c] += pieceCount[make_piece(c, pt)] * PieceValue[MG][pt]; }
} }
@ -1136,18 +1135,15 @@ bool Position::pos_is_ok(int* failedStep) const {
} }
if (step == Lists) if (step == Lists)
for (Color c = WHITE; c <= BLACK; ++c) for (Piece pc : Pieces)
for (PieceType pt = PAWN; pt <= KING; ++pt) {
{ if (pieceCount[pc] != popcount(pieces(color_of(pc), type_of(pc))))
Piece pc = make_piece(c, pt); return false;
if (pieceCount[pc] != popcount(pieces(c, pt))) for (int i = 0; i < pieceCount[pc]; ++i)
if (board[pieceList[pc][i]] != pc || index[pieceList[pc][i]] != i)
return false; return false;
}
for (int i = 0; i < pieceCount[pc]; ++i)
if (board[pieceList[pc][i]] != pc || index[pieceList[pc][i]] != i)
return false;
}
if (step == Castling) if (step == Castling)
for (Color c = WHITE; c <= BLACK; ++c) for (Color c = WHITE; c <= BLACK; ++c)

View file

@ -205,6 +205,9 @@ enum Piece {
PIECE_NB = 16 PIECE_NB = 16
}; };
const Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING };
enum Depth { enum Depth {
ONE_PLY = 1, ONE_PLY = 1,
@ -329,7 +332,7 @@ inline Score operator/(Score s, int i) {
extern Value PieceValue[PHASE_NB][PIECE_NB]; extern Value PieceValue[PHASE_NB][PIECE_NB];
inline Color operator~(Color c) { inline Color operator~(Color c) {
return Color(c ^ BLACK); return Color(c ^ BLACK); // Toggle color
} }
inline Square operator~(Square s) { inline Square operator~(Square s) {
@ -337,7 +340,7 @@ inline Square operator~(Square s) {
} }
inline Piece operator~(Piece pc) { inline Piece operator~(Piece pc) {
return Piece(pc ^ 8); return Piece(pc ^ 8); // Swap color of piece B_KNIGHT -> W_KNIGHT
} }
inline CastlingRight operator|(Color c, CastlingSide s) { inline CastlingRight operator|(Color c, CastlingSide s) {