From effefd1aba983dda2a85781c1f47256154793a13 Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Fri, 29 Mar 2024 14:31:32 +0100 Subject: [PATCH] introduced move types --- source/move.h | 30 ++++++++++++--- source/movegen.cpp | 91 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 101 insertions(+), 20 deletions(-) diff --git a/source/move.h b/source/move.h index 1f061b0..dc58509 100644 --- a/source/move.h +++ b/source/move.h @@ -1,15 +1,35 @@ #pragma once #include "bitboards.h" +enum MOVE_FLAGS { + quiet, doublePawnPush, + kingCastle, QueenCastle, + capture, epCapture, + knightPromotion, bishopPromotion, rookPromotion, queenPromotion, + knightPromoCapture, bishopPromoCapture, rookPromoCapture, queenPromoCapture +}; + +constexpr std::array moveTypes { + "quiet", "doublePawnPush", + "kingCastle", "QueenCastle", + "capture", "epCapture", + "knightPromotion", "bishopPromotion", "rookPromotion", "queenPromotion", + "knightPromoCapture", "bishopPromoCapture", "rookPromoCapture", "queenPromoCapture" +}; + struct Move { public: - SQUARE moveFrom; - SQUARE moveTo; + int moveFrom; + int moveTo; + uint16_t moveFlags; Move(){} - Move(const SQUARE from, const SQUARE to) - : moveFrom(from), moveTo(to) {} + Move(const int from, const int to, const uint16_t flags) + : moveFrom(from), moveTo(to), moveFlags(flags) {} - void PrintMove(); + void PrintMove() { + std::cout << squareCoords[moveFrom] << squareCoords[moveTo]; + std::cout << '(' << moveTypes[moveFlags] << ")\n"; + } }; \ No newline at end of file diff --git a/source/movegen.cpp b/source/movegen.cpp index c61f469..578a6ca 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -16,10 +16,6 @@ static Bitboard notHfile = ~files[H]; static Bitboard notABfile = notAfile & ~files[B]; static Bitboard notHGfile = notHfile & ~files[G]; -void Move::PrintMove() { - std::cout << squareCoords[moveFrom] << squareCoords[moveTo] << '\n'; -} - Bitboard maskPawnAttacks(int side, int square) { Bitboard attacks; Bitboard attacker = Bitboard::GetSquare(square); @@ -283,20 +279,35 @@ void GenPawnMoves(Board &board, bool color) { Bitboard attacks = pawnAttacks[color][square] & board.colors[!color]; Bitboard firstAhead = Bitboard::GetSquare(square + direction) & ~board.occupied; + Bitboard secondAhead = Bitboard::GetSquare(square + 2 * direction) & ~board.occupied; - if (firstAhead.GetBoard()) { - attacks |= firstAhead; - attacks |= Bitboard::GetSquare(square + 2 * direction) & ~board.occupied; - } - while (attacks.GetBoard()) { int targetSquare = attacks.getLS1BIndex(); - board.AddMove(Move(SQUARE(square), SQUARE(targetSquare))); + board.AddMove(Move(square, targetSquare, capture)); attacks.PopBit(targetSquare); } + if (firstAhead.GetBoard()) { + board.AddMove(Move(square, firstAhead.getLS1BIndex(), quiet)); + + if (color == White) { + if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_2).GetBoard()) { + if ((secondAhead).GetBoard()) { + board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush)); + } + } + } else { + if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_7).GetBoard()) { + if ((secondAhead).GetBoard()) { + board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush)); + } + } + } + } + + pawns.PopBit(square); } } @@ -308,15 +319,25 @@ void GenKnightMoves(Board &board, bool color) { int square = knights.getLS1BIndex(); Bitboard attacks = knightAttacks[square] & ~board.colors[color]; + Bitboard captures = attacks & board.colors[!color]; + attacks &= ~captures; while (attacks.GetBoard()) { int targetSquare = attacks.getLS1BIndex(); - board.AddMove(Move(SQUARE(square), SQUARE(targetSquare))); + board.AddMove(Move(square, targetSquare, quiet)); attacks.PopBit(targetSquare); } + while (captures.GetBoard()) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + knights.PopBit(square); } } @@ -328,15 +349,25 @@ void GenRookMoves(Board &board, bool color) { int square = rooks.getLS1BIndex(); Bitboard attacks = getRookAttack(square, board.occupied.GetBoard()) & ~board.colors[color]; + Bitboard captures = attacks & board.colors[!color]; + attacks &= ~captures; while (attacks.GetBoard()) { int targetSquare = attacks.getLS1BIndex(); - board.AddMove(Move(SQUARE(square), SQUARE(targetSquare))); + board.AddMove(Move(square, targetSquare, quiet)); attacks.PopBit(targetSquare); } + while (captures.GetBoard()) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + rooks.PopBit(square); } } @@ -348,15 +379,25 @@ void GenBishopMoves(Board &board, bool color) { int square = bishops.getLS1BIndex(); Bitboard attacks = getBishopAttack(square, board.occupied.GetBoard()) & ~board.colors[color]; + Bitboard captures = attacks & board.colors[!color]; + attacks &= ~captures; while (attacks.GetBoard()) { int targetSquare = attacks.getLS1BIndex(); - board.AddMove(Move(SQUARE(square), SQUARE(targetSquare))); + board.AddMove(Move(square, targetSquare, quiet)); attacks.PopBit(targetSquare); } + while (captures.GetBoard()) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + bishops.PopBit(square); } } @@ -368,15 +409,25 @@ void GenQueenMoves(Board &board, bool color) { int square = queens.getLS1BIndex(); Bitboard attacks = getQueenAttack(square, board.occupied.GetBoard()) & ~board.colors[color]; + Bitboard captures = attacks & board.colors[!color]; + attacks &= ~captures; while (attacks.GetBoard()) { int targetSquare = attacks.getLS1BIndex(); - board.AddMove(Move(SQUARE(square), SQUARE(targetSquare))); + board.AddMove(Move(square, targetSquare, quiet)); attacks.PopBit(targetSquare); } + while (captures.GetBoard()) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + queens.PopBit(square); } } @@ -387,14 +438,24 @@ void GenKingMoves(Board &board, bool color) { int square = king.getLS1BIndex(); Bitboard attacks = kingAttacks[square] & ~board.colors[color]; + Bitboard captures = attacks & board.colors[!color]; + attacks &= ~captures; while (attacks.GetBoard()) { int targetSquare = attacks.getLS1BIndex(); - board.AddMove(Move(SQUARE(square), SQUARE(targetSquare))); + board.AddMove(Move(square, targetSquare, quiet)); attacks.PopBit(targetSquare); } + + while (captures.GetBoard()) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } } void GenerateMoves(Board &board) {