diff options
Diffstat (limited to 'src/moves.cpp')
| -rw-r--r-- | src/moves.cpp | 144 |
1 files changed, 79 insertions, 65 deletions
diff --git a/src/moves.cpp b/src/moves.cpp index f741b7f..132e79f 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -188,113 +188,127 @@ GameState GetNewGameState(Game *g) { } return TURN; } - -bool IsSquareAttacked(Game *board, Position square, bool white) { - Game temp = *board; - temp.turn = white; - - // Moves - constexpr std::array<int, 4> bishopMoves{-9, 9, -7, 7}; - constexpr std::array<int, 4> rookMoves{-8, 8, -1, 1}; - +bool IsSquareAttacked(Game *g, Position square, bool white) { int target = PositionToIndex(square); - // Pawns (custom because pawn moves != pawn attacks) - int pawnDirection = white ? -1 : 1; + // Pawn attacks + int pawnDir = white ? -1 : 1; - int pawnRank = square.rank - pawnDirection; + int pawnRank = square.rank - pawnDir; if (pawnRank >= 0 && pawnRank < 8) { for (int fileOffset : {-1, 1}) { - int pawnFile = square.file + fileOffset; + int file = square.file + fileOffset; - if (pawnFile < 0 || pawnFile >= 8) + if (file < 0 || file >= 8) continue; - Piece p = temp.pieces[pawnRank * 8 + pawnFile]; + Piece p = g->pieces[pawnRank * 8 + file]; if (p.type == PAWN && p.color == white) return true; } } - // knights - for (int i = 0; i < 64; i++) { - Piece p = temp.pieces[i]; + // Knight attacks + constexpr std::array<int, 8> knightOffsets{-17, -15, -10, -6, 6, 10, 15, 17}; - if (p.type == KNIGHT && p.color == white) { - std::vector<Move> moves; - GenerateKnightMoves(&temp, i, moves); + for (int offset : knightOffsets) { + int from = target + offset; - for (Move m : moves) { - if (PositionToIndex(m.To) == target) - return true; - } - } - } - for (int i = 0; i < 64; i++) { - Piece p = temp.pieces[i]; + if (from < 0 || from >= 64) + continue; - if (p.type == BISHOP && p.color == white) { - std::vector<Move> moves; - GenerateSlidingMoves(&temp, i, bishopMoves, moves); + int fileDiff = abs((from % 8) - (target % 8)); - for (Move m : moves) { - if (PositionToIndex(m.To) == target) - return true; - } - } + if (fileDiff != 1 && fileDiff != 2) + continue; + + Piece p = g->pieces[from]; + + if (p.type == KNIGHT && p.color == white) + return true; } - // rook - for (int i = 0; i < 64; i++) { - Piece p = temp.pieces[i]; + // King attacks + constexpr std::array<int, 8> kingOffsets{-9, -8, -7, -1, 1, 7, 8, 9}; - if (p.type == ROOK && p.color == white) { - std::vector<Move> moves; - GenerateSlidingMoves(&temp, i, rookMoves, moves); + for (int offset : kingOffsets) { + int from = target + offset; - for (Move m : moves) { - if (PositionToIndex(m.To) == target) - return true; - } - } + if (from < 0 || from >= 64) + continue; + + if (abs((from % 8) - (target % 8)) > 1) + continue; + + Piece p = g->pieces[from]; + + if (p.type == KING && p.color == white) + return true; } - // queen - for (int i = 0; i < 64; i++) { - Piece p = temp.pieces[i]; + // Sliding pieces + constexpr std::array<int, 4> rookDirs{-8, 8, -1, 1}; + + constexpr std::array<int, 4> bishopDirs{-9, 9, -7, 7}; + + // Rooks + queens + for (int dir : rookDirs) { + int pos = target; - if (p.type == QUEEN && p.color == white) { - std::vector<Move> moves; + while (true) { + int next = pos + dir; - GenerateSlidingMoves(&temp, i, bishopMoves, moves); - GenerateSlidingMoves(&temp, i, rookMoves, moves); + if (next < 0 || next >= 64) + break; - for (Move m : moves) { - if (PositionToIndex(m.To) == target) + // horizontal wrap + if ((dir == 1 || dir == -1) && next / 8 != pos / 8) + break; + + Piece p = g->pieces[next]; + + if (p.type != NONEPIECE) { + if (p.color == white && (p.type == ROOK || p.type == QUEEN)) return true; + + break; } + + pos = next; } } - // --- Kings --- - for (int i = 0; i < 64; i++) { - Piece p = temp.pieces[i]; + // Bishops + queens + for (int dir : bishopDirs) { + int pos = target; - if (p.type == KING && p.color == white) { - std::vector<Move> moves; - GenerateKingMoves(&temp, i, moves); + while (true) { + int next = pos + dir; + + if (next < 0 || next >= 64) + break; + + if (abs((next % 8) - (pos % 8)) != 1) + break; - for (Move m : moves) { - if (PositionToIndex(m.To) == target) + Piece p = g->pieces[next]; + + if (p.type != NONEPIECE) { + if (p.color == white && (p.type == BISHOP || p.type == QUEEN)) return true; + + break; } + + pos = next; } } return false; } + Position IndexToPosition(int i) { uint8_t rank = i / 8; // 0-7 uint8_t file = i % 8; // 0-7 |
