aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-07-29 19:07:25 +0200
committerAdam <adammegarules1@gmail.com>2026-07-29 19:07:25 +0200
commit1a7996b7a5b477786421469ddcb2dc3f4e0de0bb (patch)
tree2e171faac5b7730b96ad94593dc5e01e23d81fd7 /src
parent40767a0c4ff84af71dc00c11d27186c7cd1ab973 (diff)
hell
Diffstat (limited to 'src')
-rw-r--r--src/board.cpp2
-rw-r--r--src/bot.cpp13
-rw-r--r--src/moves.cpp144
3 files changed, 80 insertions, 79 deletions
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<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