From ff0c0c785aa159494ff2531ed03a5909cf35531c Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 15 Apr 2024 09:21:03 +0200 Subject: [PATCH] rewrote the whole GetDirection function + minor bugfix in castling updates --- source/board.cpp | 2 +- source/main.cpp | 2 +- source/movegen.cpp | 39 +++++++++++++++++++++++---------------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index b4a2f25..3245ba1 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -295,7 +295,7 @@ void Board::RemovePiece(int piece, int square, bool color) { static void UpdateCastlingRights(Board &board, int targetPiece, int targetSquare, int attackerColor) { if (targetPiece == Rook) { int queenSideRook = attackerColor ? a1 : a8; - int kingSideRook = attackerColor ? a8 : h8; + int kingSideRook = attackerColor ? h1 : h8; if (targetSquare == queenSideRook) { board.castlingRights[!attackerColor * 2 + 1] = false; diff --git a/source/main.cpp b/source/main.cpp index e7ae63e..86970db 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -9,7 +9,7 @@ int main() { initSliderAttacks(); Board board; - //board.SetByFen("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8"); + board.SetByFen("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1"); Perft(board, 5); return 0; diff --git a/source/movegen.cpp b/source/movegen.cpp index 0cb72c6..74b7f84 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -316,24 +316,31 @@ static Bitboard Checkers(Board &board, bool color) { // Returns the direction to target from square static int GetDirection(int square, int target) { - int difference = target - square; + int rankDiff = (target / 8) - (square / 8); + int fileDiff = (target % 8) - (square % 8); - if (difference % north == 0 && difference > 0) { - return north; - } else if (difference % south == 0 && difference < 0) { - return south; - } else if (difference % noWe == 0 && difference > 0) { - return noWe; - } else if (difference % noEa == 0 && difference > 0) { - return noEa; - } else if (difference % soWe == 0 && difference < 0) { - return soWe; - } else if (difference % soEa == 0 && difference < 0) { - return soEa; - } else if (ranks[square / 8] == ranks[target / 8] && target < square) { + // Main directions + if (rankDiff == 0 && fileDiff < 0) { return west; - } else if (ranks[square / 8] == ranks[target / 8] && target > square) { + } else if (rankDiff == 0 && fileDiff > 0) { return east; + } else if (rankDiff < 0 && fileDiff == 0) { + return south; + } else if (rankDiff > 0 && fileDiff == 0) { + return north; + } + + // Diagonals + if (abs(rankDiff) == abs(fileDiff)) { + if (rankDiff < 0 && fileDiff < 0) { + return soWe; + } else if (rankDiff < 0 && fileDiff > 0) { + return soEa; + } else if (rankDiff > 0 && fileDiff < 0) { + return noWe; + } else if (rankDiff > 0 && fileDiff > 0) { + return noEa; + } } return 0; @@ -853,7 +860,7 @@ static void GenCheckEvasions(Board &board, bool color) { pushMask.PopBit(kingSquare); pushMask.PopBit(checkerSquare); } - + // looping through own pieces to add captures and blocks for (int type = Pawn; type < King; type++) { Bitboard currentPiece = board.colors[color] & board.pieces[type];