Skip to content

Commit

Permalink
Fixed zobrist hash in move ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Dannyj1 committed Jan 11, 2023
1 parent aeaa86f commit d8ae86a
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 36 deletions.
129 changes: 110 additions & 19 deletions bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ namespace Zagreus {

castlingRights &= ~(CastlingRights::WHITE_KINGSIDE | CastlingRights::WHITE_QUEENSIDE);
} else if (pieceType == PieceType::BLACK_KING) {
if (castlingRights & CastlingRights::WHITE_KINGSIDE) {
if (castlingRights & CastlingRights::BLACK_KINGSIDE) {
zobristHash ^= zobristConstants[768 + 3];
}

Expand Down Expand Up @@ -375,7 +375,6 @@ namespace Zagreus {
}
}


if (pieceType == PieceType::WHITE_ROOK) {
if (fromSquare == Square::A1) {
if (castlingRights & CastlingRights::WHITE_QUEENSIDE) {
Expand Down Expand Up @@ -891,39 +890,131 @@ namespace Zagreus {
Bitboard::blackTimeMsec = blackTimeMsec;
}

/*uint64_t Bitboard::getZobristForMove(Move &move) {
uint64_t Bitboard::getZobristForMove(int fromSquare, int toSquare, PieceType pieceType, PieceType promotionPiece) {
uint64_t hash = getZobristHash();

if (enPassantSquare[PieceColor::WHITE] != -1) {
zobristHash ^= zobristConstants[(enPassantSquare[PieceColor::WHITE] % 8) + 768 + 5];
hash ^= zobristConstants[(enPassantSquare[PieceColor::WHITE] % 8) + 768 + 5];
}

if (enPassantSquare[PieceColor::BLACK] != -1) {
zobristHash ^= zobristConstants[(enPassantSquare[PieceColor::BLACK] % 8) + 768 + 13];
hash ^= zobristConstants[(enPassantSquare[PieceColor::BLACK] % 8) + 768 + 13];
}

if (move.pieceType == PieceType::WHITE_PAWN || move.pieceType == PieceType::BLACK_PAWN) {
halfMoveClock = 0;
if (move.fromSquare - move.toSquare == 16) {
zobristHash ^= zobristConstants[(enPassantSquare[PieceColor::BLACK] % 8) + 768 + 13];
} else if (move.fromSquare - move.toSquare == -16) {
zobristHash ^= zobristConstants[(enPassantSquare[PieceColor::WHITE] % 8) + 768 + 5];
if (pieceType == PieceType::WHITE_PAWN || pieceType == PieceType::BLACK_PAWN) {
if (fromSquare - toSquare == 16) {
hash ^= zobristConstants[((toSquare + 8) % 8) + 768 + 13];
} else if (fromSquare - toSquare == -16) {
hash ^= zobristConstants[((toSquare - 8) % 8) + 768 + 5];
} else {
if (std::abs(fromSquare - toSquare) == 7 || std::abs(fromSquare - toSquare) == 9) {
if (enPassantSquare[PieceColor::WHITE] == toSquare) {
hash ^= zobristConstants[(toSquare + 8) * 12 + PieceType::WHITE_PAWN];
} else if (enPassantSquare[PieceColor::BLACK] == toSquare) {
hash ^= zobristConstants[(toSquare - 8) * 12 + PieceType::BLACK_PAWN];
}
}
}
}

if (pieceType == PieceType::WHITE_KING) {
castlingRights &= ~(CastlingRights::WHITE_KINGSIDE | CastlingRights::WHITE_QUEENSIDE);
zobristHash ^= zobristConstants[768 + 1];
zobristHash ^= zobristConstants[768 + 2];
if (castlingRights & CastlingRights::WHITE_KINGSIDE) {
hash ^= zobristConstants[768 + 1];
}

if (castlingRights & CastlingRights::WHITE_QUEENSIDE) {
hash ^= zobristConstants[768 + 2];
}
} else if (pieceType == PieceType::BLACK_KING) {
castlingRights &= ~(CastlingRights::BLACK_KINGSIDE | CastlingRights::BLACK_QUEENSIDE);
zobristHash ^= zobristConstants[768 + 3];
zobristHash ^= zobristConstants[768 + 4];
if (castlingRights & CastlingRights::BLACK_KINGSIDE) {
hash ^= zobristConstants[768 + 3];
}

if (castlingRights & CastlingRights::BLACK_QUEENSIDE) {
hash ^= zobristConstants[768 + 4];
}
}

if (pieceType == PieceType::WHITE_ROOK && toSquare == Square::E1) {
if (fromSquare == Square::A1 && (castlingRights & CastlingRights::WHITE_QUEENSIDE)) {
if (castlingRights & CastlingRights::WHITE_KINGSIDE) {
hash ^= zobristConstants[768 + 1];
}

if (castlingRights & CastlingRights::WHITE_QUEENSIDE) {
hash ^= zobristConstants[768 + 2];
}
} else if (fromSquare == Square::H1 && (castlingRights & CastlingRights::WHITE_KINGSIDE)) {
if (castlingRights & CastlingRights::WHITE_KINGSIDE) {
hash ^= zobristConstants[768 + 1];
}

if (castlingRights & CastlingRights::WHITE_QUEENSIDE) {
hash ^= zobristConstants[768 + 2];
}
}
}

if (pieceType == PieceType::BLACK_ROOK && toSquare == Square::E8) {
if (fromSquare == Square::A8 && (castlingRights & CastlingRights::BLACK_QUEENSIDE)) {
if (castlingRights & CastlingRights::BLACK_KINGSIDE) {
hash ^= zobristConstants[768 + 3];
}

if (castlingRights & CastlingRights::BLACK_QUEENSIDE) {
hash ^= zobristConstants[768 + 4];
}
} else if (fromSquare == Square::H8 && (castlingRights & CastlingRights::BLACK_KINGSIDE)) {
if (castlingRights & CastlingRights::BLACK_KINGSIDE) {
hash ^= zobristConstants[768 + 3];
}

if (castlingRights & CastlingRights::BLACK_QUEENSIDE) {
hash ^= zobristConstants[768 + 4];
}
}
}

if (pieceType == PieceType::WHITE_ROOK) {
if (fromSquare == Square::A1) {
if (castlingRights & CastlingRights::WHITE_QUEENSIDE) {
hash ^= zobristConstants[768 + 2];
}
} else if (fromSquare == Square::H1) {
if (castlingRights & CastlingRights::WHITE_KINGSIDE) {
hash ^= zobristConstants[768 + 1];
}
}
}

if (pieceType == PieceType::BLACK_ROOK) {
if (fromSquare == Square::A8) {
if (castlingRights & CastlingRights::BLACK_QUEENSIDE) {
zobristHash ^= zobristConstants[768 + 4];
}
} else if (fromSquare == Square::H8) {
if (castlingRights & CastlingRights::BLACK_KINGSIDE) {
zobristHash ^= zobristConstants[768 + 3];
}
}
}

PieceType capturedPiece = getPieceOnSquare(toSquare);
if (capturedPiece != PieceType::EMPTY) {
hash ^= zobristConstants[toSquare * 12 + capturedPiece];
}

hash ^= zobristConstants[fromSquare * 12 + pieceType];

if (promotionPiece != PieceType::EMPTY) {
hash ^= zobristConstants[toSquare * 12 + promotionPiece];
} else {
hash ^= zobristConstants[toSquare * 12 + pieceType];
}

hash ^= zobristConstants[768];
return hash;
}*/
}

bool Bitboard::isInsufficientMaterial() {
int pieceCount = popcnt(getOccupiedBoard());
Expand Down
2 changes: 2 additions & 0 deletions bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ namespace Zagreus {
bool isHasWhiteCastled() const;

bool isHasBlackCastled() const;

uint64_t getZobristForMove(int fromSquare, int toSquare, PieceType pieceType, PieceType promotionPiece);
};

uint64_t soutOne(uint64_t b);
Expand Down
2 changes: 1 addition & 1 deletion engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace Zagreus {
}

std::string ZagreusEngine::getEngineVersion() {
return "v1.0.0";
return "v1.0.1";
}

std::string ZagreusEngine::getAuthorName() {
Expand Down
3 changes: 1 addition & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ int main() {
Zagreus::ZagreusEngine engine;
senjo::UCIAdapter adapter(engine);

/*
Zagreus::Bitboard bb;
bb.setFromFEN("r1bq1rk1/pp2p1b1/2pp1nnp/3P2p1/2P2p1P/2N1PNB1/PP2BPP1/R2QK2R w KQ - 0 13");
bb.makeMove(0, 1, Zagreus::PieceType::WHITE_ROOK, Zagreus::PieceType::EMPTY);
bb.setWhiteTimeMsec(999999999);
Zagreus::searchManager.getBestMove(engine, bb, Zagreus::PieceColor::WHITE);
*/

std::string line;
line.reserve(16384);
Expand Down
28 changes: 14 additions & 14 deletions move_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,20 @@ namespace Zagreus {
}

if (genIndex >= 56 || genIndex <= 7) {
moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, static_cast<PieceType>(PieceType::WHITE_QUEEN + color)), bitboard.getPly(),
captureScore,
static_cast<PieceType>(PieceType::WHITE_QUEEN + color)});
moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, static_cast<PieceType>(PieceType::WHITE_ROOK + color)), bitboard.getPly(),
captureScore,
static_cast<PieceType>(PieceType::WHITE_ROOK + color)});
moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, static_cast<PieceType>(PieceType::WHITE_BISHOP + color)), bitboard.getPly(),
captureScore,
static_cast<PieceType>(PieceType::WHITE_BISHOP + color)});
moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, static_cast<PieceType>(PieceType::WHITE_KNIGHT + color)), bitboard.getPly(),
captureScore,
static_cast<PieceType>(PieceType::WHITE_KNIGHT + color)});
} else {
moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, PieceType::EMPTY), bitboard.getPly(),
captureScore});
}

Expand Down Expand Up @@ -248,7 +248,7 @@ namespace Zagreus {
bitboard.getPieceWeight(pieceType);
}

moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, PieceType::EMPTY), bitboard.getPly(),
captureScore});

genBB &= ~(1ULL << genIndex);
Expand Down Expand Up @@ -288,7 +288,7 @@ namespace Zagreus {
bitboard.getPieceWeight(pieceType);
}

moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, PieceType::EMPTY), bitboard.getPly(),
captureScore});

genBB &= ~(1ULL << genIndex);
Expand Down Expand Up @@ -319,7 +319,7 @@ namespace Zagreus {
if (!bitboard.isSquareAttackedByColor(Square::F1, Bitboard::getOppositeColor(color))
&& !bitboard.isSquareAttackedByColor(Square::G1, Bitboard::getOppositeColor(color))) {
moves.push_back({Square::H1, Square::E1, PieceType::WHITE_ROOK,
bitboard.getZobristHash(), 0, 0});
bitboard.getZobristForMove(Square::H1, Square::E1, pieceType, PieceType::EMPTY), 0, 0});
}
}
}
Expand All @@ -331,7 +331,7 @@ namespace Zagreus {
if (!bitboard.isSquareAttackedByColor(Square::C1, Bitboard::getOppositeColor(color))
&& !bitboard.isSquareAttackedByColor(Square::D1, Bitboard::getOppositeColor(color))) {
moves.push_back({Square::A1, Square::E1, PieceType::WHITE_ROOK,
bitboard.getZobristHash(), 0, 0});
bitboard.getZobristForMove(Square::A1, Square::E1, pieceType, PieceType::EMPTY), 0, 0});
}
}
}
Expand All @@ -343,7 +343,7 @@ namespace Zagreus {
if (!bitboard.isSquareAttackedByColor(Square::F8, Bitboard::getOppositeColor(color))
&& !bitboard.isSquareAttackedByColor(Square::G8, Bitboard::getOppositeColor(color))) {
moves.push_back({Square::H8, Square::E8, PieceType::BLACK_ROOK,
bitboard.getZobristHash(), 0, 0});
bitboard.getZobristForMove(Square::H8, Square::E8, pieceType, PieceType::EMPTY), 0, 0});
}
}
}
Expand All @@ -356,7 +356,7 @@ namespace Zagreus {
if (!bitboard.isSquareAttackedByColor(Square::C8, Bitboard::getOppositeColor(color))
&& !bitboard.isSquareAttackedByColor(Square::D8, Bitboard::getOppositeColor(color))) {
moves.push_back({Square::A8, Square::E8, PieceType::BLACK_ROOK,
bitboard.getZobristHash(), 0, 0});
bitboard.getZobristForMove(Square::A8, Square::E8, pieceType, PieceType::EMPTY), 0, 0});
}
}
}
Expand Down Expand Up @@ -386,7 +386,7 @@ namespace Zagreus {
bitboard.getPieceWeight(pieceType);
}

moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, PieceType::EMPTY), bitboard.getPly(),
captureScore});

genBB &= ~(1ULL << genIndex);
Expand Down Expand Up @@ -426,7 +426,7 @@ namespace Zagreus {
bitboard.getPieceWeight(pieceType);
}

moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, PieceType::EMPTY), bitboard.getPly(),
captureScore});

genBB &= ~(1ULL << genIndex);
Expand Down Expand Up @@ -466,7 +466,7 @@ namespace Zagreus {
bitboard.getPieceWeight(pieceType);
}

moves.push_back({index, genIndex, pieceType, bitboard.getZobristHash(), bitboard.getPly(),
moves.push_back({index, genIndex, pieceType, bitboard.getZobristForMove(index, genIndex, pieceType, PieceType::EMPTY), bitboard.getPly(),
captureScore});

genBB &= ~(1ULL << genIndex);
Expand Down

0 comments on commit d8ae86a

Please sign in to comment.