aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-01 12:15:50 +0200
committerAdam <adammegarules1@gmail.com>2026-08-01 12:15:50 +0200
commit980ac2bc1ccbe1b8bde626a964464bb018baa466 (patch)
tree99b195a4eac9d346194bb5abe80f48e1ef2eee1f
parent25af8ae92ffd1a4c2dd0007b155ddb3cbd0d6e89 (diff)
adding castling
-rw-r--r--src/moves.cpp55
-rw-r--r--src/moves.hpp1
2 files changed, 50 insertions, 6 deletions
diff --git a/src/moves.cpp b/src/moves.cpp
index b07c91b..163bb8c 100644
--- a/src/moves.cpp
+++ b/src/moves.cpp
@@ -2,6 +2,7 @@
#include <cassert>
#include <cstdint>
#include <cstdlib>
+#include <iostream>
#include <sys/types.h>
#include <vector>
@@ -159,12 +160,12 @@ std::vector<Move> GetPseudoLegalMoves(Game *b, bool GenerateQuietMoves) {
continue;
}
+ if (piece.type == PAWN) {
+ GeneratePawnMoves(b, i, moves, GenerateQuietMoves);
+ }
if (piece.type == KNIGHT) {
GenerateKnightMoves(b, i, moves, GenerateQuietMoves);
};
- if (piece.type == KING) {
- GenerateKingMoves(b, i, moves, GenerateQuietMoves);
- };
if (piece.type == BISHOP) {
GenerateSlidingMoves(b, i, bishop_Moves, moves, GenerateQuietMoves);
}
@@ -175,9 +176,10 @@ std::vector<Move> GetPseudoLegalMoves(Game *b, bool GenerateQuietMoves) {
GenerateSlidingMoves(b, i, rook_Moves, moves, GenerateQuietMoves);
GenerateSlidingMoves(b, i, bishop_Moves, moves, GenerateQuietMoves);
}
- if (piece.type == PAWN) {
- GeneratePawnMoves(b, i, moves, GenerateQuietMoves);
- }
+ if (piece.type == KING) {
+ GenerateKingMoves(b, i, moves, GenerateQuietMoves);
+ GenerateCastlingMoves(i, b, moves);
+ };
};
return moves;
@@ -397,3 +399,44 @@ void GenerateSlidingMoves(Game *b, int from,
}
};
};
+void GenerateCastlingMoves(int from, Game *g, std::vector<Move> &moves) {
+ Piece piece = g->pieces[from];
+ if (piece.type != KING) {
+ assert(false && "Calling generate castling moves on non king piece");
+ std::cout << "Internal error\n";
+ exit(1);
+ }
+ if (from != 4 && !g->turn) {
+ return;
+ }
+ if (from != 60 && g->turn) {
+ return;
+ }
+ bool oneToRight = g->PieceBitboard & (1ULL << (from + 1));
+ bool twoToRight = g->PieceBitboard & (1ULL << (from + 2));
+ bool oneToLeft = g->PieceBitboard & (1ULL << (from - 1));
+ bool twoToLeft = g->PieceBitboard & (1ULL << (from - 2));
+ bool threeToLeft = g->PieceBitboard & (1ULL << (from - 3));
+ if (g->turn) {
+ // white
+ if (!oneToRight && !twoToRight && g->whiteCastleKing) {
+ moves.push_back(
+ {.From = IndexToPosition(from), .To = IndexToPosition(from + 2)});
+ }
+ if (!oneToLeft && !twoToLeft && !threeToLeft && g->whiteCastleQueen) {
+ moves.push_back(
+ {.From = IndexToPosition(from), .To = IndexToPosition(from - 2)});
+ }
+ }
+ if (!g->turn) {
+ // black
+ if (!oneToRight && !twoToRight && g->blackCastleKing) {
+ moves.push_back(
+ {.From = IndexToPosition(from), .To = IndexToPosition(from + 2)});
+ }
+ if (!oneToLeft && !twoToLeft && !threeToLeft && g->blackCastleQueen) {
+ moves.push_back(
+ {.From = IndexToPosition(from), .To = IndexToPosition(from - 2)});
+ }
+ }
+}
diff --git a/src/moves.hpp b/src/moves.hpp
index 282cff8..e48d7f8 100644
--- a/src/moves.hpp
+++ b/src/moves.hpp
@@ -16,6 +16,7 @@ void GenerateKnightMoves(Game *b, int from, std::vector<Move> &moves,
bool GenerateQuietMoves);
void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves,
bool GenerateQuietMoves);
+void GenerateCastlingMoves(int from, Game *g, std::vector<Move> &moves);
bool IsSquareAttacked(Game *g, Position square, bool byWhite);
GameState GetNewGameState(Game *g);