From 1a7996b7a5b477786421469ddcb2dc3f4e0de0bb Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 29 Jul 2026 19:07:25 +0200 Subject: hell --- src/board.cpp | 2 +- src/bot.cpp | 13 ------ src/moves.cpp | 144 ++++++++++++++++++++++++++++++++-------------------------- 3 files changed, 80 insertions(+), 79 deletions(-) (limited to 'src') diff --git a/src/board.cpp b/src/board.cpp index eb42769..0f33f17 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -188,7 +188,7 @@ UndoMove MakeMove(Move move, Game *g) { if (g->halfMoveClock >= 50) { g->state = DRAW; - } + } return undo; }; diff --git a/src/bot.cpp b/src/bot.cpp index b3e75d6..5d5e1ad 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -277,18 +277,5 @@ float EvaluateBoardForWhite(Game *g, int depth) { else score -= value; } - - Position WhiteKingPosition = FindKing(g, g->turn); - bool IsWhiteIncheck = IsSquareAttacked(g, WhiteKingPosition, !g->turn); - - Position BlackKingPosition = FindKing(g, g->turn); - bool IsBlackInCheck = IsSquareAttacked(g, BlackKingPosition, !g->turn); - - if (IsWhiteIncheck) { - score -= CHECK_BIAS; // white king attacked - } - if (IsBlackInCheck) { - score += CHECK_BIAS; // black king attacked - } return score; } 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 bishopMoves{-9, 9, -7, 7}; - constexpr std::array 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 knightOffsets{-17, -15, -10, -6, 6, 10, 15, 17}; - if (p.type == KNIGHT && p.color == white) { - std::vector 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 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 kingOffsets{-9, -8, -7, -1, 1, 7, 8, 9}; - if (p.type == ROOK && p.color == white) { - std::vector 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 rookDirs{-8, 8, -1, 1}; + + constexpr std::array bishopDirs{-9, 9, -7, 7}; + + // Rooks + queens + for (int dir : rookDirs) { + int pos = target; - if (p.type == QUEEN && p.color == white) { - std::vector 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 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 -- cgit v1.2.3