aboutsummaryrefslogtreecommitdiff
path: root/src/moves.cpp
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-07-30 17:19:56 +0200
committerAdam <adammegarules1@gmail.com>2026-07-30 17:19:56 +0200
commit0528ed43bb13edd89dfa8a38b9bfca6ba7fd83d6 (patch)
tree4b7dbb7d2f2875a761629da5fe290f7680a46bea /src/moves.cpp
parent8df9e390a71eeb2835eab06ec4b474c8cad63ecd (diff)
optimazing
Diffstat (limited to 'src/moves.cpp')
-rw-r--r--src/moves.cpp150
1 files changed, 82 insertions, 68 deletions
diff --git a/src/moves.cpp b/src/moves.cpp
index 4deafe3..c4cda26 100644
--- a/src/moves.cpp
+++ b/src/moves.cpp
@@ -8,7 +8,8 @@
#include "board/board.hpp"
#include "moves.hpp"
-void GenerateKnightMoves(Game *b, int from, std::vector<Move> &moves) {
+void GenerateKnightMoves(Game *b, int from, std::vector<Move> &moves,
+ bool GenerateQuietMoves) {
Piece knight = b->pieces[from];
if (knight.type != KNIGHT) {
assert(false && "Calling generate knight moves on non knight");
@@ -32,10 +33,14 @@ void GenerateKnightMoves(Game *b, int from, std::vector<Move> &moves) {
};
}
+ if (!GenerateQuietMoves && b->pieces[next].type == NONEPIECE) {
+ continue;
+ }
moves.push_back({IndexToPosition(from), IndexToPosition(next)});
}
};
-void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves) {
+void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves,
+ bool quietMoves) {
Piece pawn = b->pieces[from];
if (pawn.type != PAWN) {
assert(false && "Calling generate pawn moves on non pawn");
@@ -52,24 +57,27 @@ void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves) {
}
// if piece it want to move to is none and it as legal move
- if (b->pieces[next].type == NONEPIECE) {
- if (position.rank + (pawn.color ? -1 : 1) == 0 ||
- position.rank + (pawn.color ? -1 : 1) == 7) {
- moves.push_back({position, IndexToPosition(next), QUEEN});
- moves.push_back({position, IndexToPosition(next), ROOK});
- moves.push_back({position, IndexToPosition(next), BISHOP});
- moves.push_back({position, IndexToPosition(next), KNIGHT});
- } else {
- moves.push_back({position, IndexToPosition(next)});
- }
+ if (quietMoves) {
+ if (b->pieces[next].type == NONEPIECE) {
+ if (position.rank + (pawn.color ? -1 : 1) == 0 ||
+ position.rank + (pawn.color ? -1 : 1) == 7) {
+ moves.push_back({position, IndexToPosition(next), QUEEN});
+ moves.push_back({position, IndexToPosition(next), ROOK});
+ moves.push_back({position, IndexToPosition(next), BISHOP});
+ moves.push_back({position, IndexToPosition(next), KNIGHT});
+ } else {
+ moves.push_back({position, IndexToPosition(next)});
+ }
- int startingRank = pawn.color ? 6 : 1;
- int twoSteps = from + step * 2;
- if (position.rank == startingRank &&
- b->pieces[twoSteps].type == NONEPIECE) {
- moves.push_back({position, IndexToPosition(twoSteps)});
+ int startingRank = pawn.color ? 6 : 1;
+ int twoSteps = from + step * 2;
+ if (position.rank == startingRank &&
+ b->pieces[twoSteps].type == NONEPIECE) {
+ moves.push_back({position, IndexToPosition(twoSteps)});
+ }
}
- }
+ };
+
for (int fileOffset : {-1, 1}) {
int targetFile = position.file + fileOffset;
int targetRank = position.rank + (pawn.color ? -1 : 1);
@@ -99,9 +107,40 @@ void GeneratePawnMoves(Game *b, int from, std::vector<Move> &moves) {
}
}
// the end
-}
+};
+void GenerateKingMoves(Game *b, int from, std::vector<Move> &moves,
+ bool GenerateQuietMoves) {
+ if (b->pieces[from].type != KING) {
+ assert(false && "calling generate king moves on non king");
+ return;
+ }
+ if (b->pieces[from].color != b->turn) {
+ assert(false && "calling generate king moves on king of opposite color");
+ return;
+ }
+ constexpr std::array<int, 8> king_moves{-1, 1, 8, -8, -9, 9, -7, 7};
+ for (int offset : king_moves) {
+ int next = from + offset;
+ if (next >= 64 || next < 0) {
+ continue;
+ }
+ if (std::abs((next % 8) - (from % 8)) > 1) {
+ continue;
+ };
+ if (b->pieces[next].type != NONEPIECE) {
+ if (b->pieces[next].color == b->turn) {
+ continue;
+ };
+ }
+ if (!GenerateQuietMoves && b->pieces[next].type == NONEPIECE) {
+ continue;
+ }
+
+ moves.push_back({IndexToPosition(from), IndexToPosition(next)});
+ }
+};
-std::vector<Move> GetPseudoLegalMoves(Game *b) {
+std::vector<Move> GetPseudoLegalMoves(Game *b, bool GenerateQuietMoves) {
std::vector<Move> moves;
moves.reserve(40);
@@ -113,36 +152,35 @@ std::vector<Move> GetPseudoLegalMoves(Game *b) {
if (piece.type == NONEPIECE) {
continue;
}
- if (piece.color != b->turn)
+ if (piece.color != b->turn) {
continue;
-
- // add support for knight and king later
-
- if (piece.type == PAWN) {
- GeneratePawnMoves(b, i, moves);
}
+
if (piece.type == KNIGHT) {
- GenerateKnightMoves(b, i, moves);
+ GenerateKnightMoves(b, i, moves, GenerateQuietMoves);
};
- if (piece.type == BISHOP) {
- GenerateSlidingMoves(b, i, bishop_Moves, moves);
- }
if (piece.type == KING) {
- GenerateKingMoves(b, i, moves);
+ GenerateKingMoves(b, i, moves, GenerateQuietMoves);
};
+ if (piece.type == BISHOP) {
+ GenerateSlidingMoves(b, i, bishop_Moves, moves, GenerateQuietMoves);
+ }
if (piece.type == ROOK) {
- GenerateSlidingMoves(b, i, rook_Moves, moves);
+ GenerateSlidingMoves(b, i, rook_Moves, moves, GenerateQuietMoves);
}
if (piece.type == QUEEN) {
- GenerateSlidingMoves(b, i, rook_Moves, moves);
- GenerateSlidingMoves(b, i, bishop_Moves, moves);
+ GenerateSlidingMoves(b, i, rook_Moves, moves, GenerateQuietMoves);
+ GenerateSlidingMoves(b, i, bishop_Moves, moves, GenerateQuietMoves);
+ }
+ if (piece.type == PAWN) {
+ GeneratePawnMoves(b, i, moves, GenerateQuietMoves);
}
};
return moves;
}
-std::vector<Move> GetLegalMoves(Game *g) {
- std::vector<Move> moves = GetPseudoLegalMoves(g);
+std::vector<Move> GetLegalMoves(Game *g, bool quietMove) {
+ std::vector<Move> moves = GetPseudoLegalMoves(g, quietMove);
std::vector<Move> legalMoves;
for (Move move : moves) {
@@ -319,7 +357,7 @@ Position IndexToPosition(int i) {
void GenerateSlidingMoves(Game *b, int from,
const std::array<int, 4> &directions,
- std::vector<Move> &moves) {
+ std::vector<Move> &moves, bool GenerateQuietMoves) {
for (uint i = 0; i < directions.size(); i++) {
int direction = directions[i];
@@ -337,13 +375,17 @@ void GenerateSlidingMoves(Game *b, int from,
if (i2 >= 64 || i2 < 0) {
break;
}
- if (b->pieces[i2].color == b->turn && b->pieces[i2].type != NONEPIECE) {
- break;
- }
+
if ((direction == 1 || direction == -1) &&
(i2 / 8 != (i2 - direction) / 8)) {
break;
}
+ if (b->pieces[i2].color == b->turn && b->pieces[i2].type != NONEPIECE) {
+ break;
+ }
+ if (!GenerateQuietMoves && b->pieces[i2].type == NONEPIECE) {
+ continue;
+ }
moves.push_back({IndexToPosition(from), IndexToPosition(i2)});
if (b->pieces[i2].color != b->turn && b->pieces[i2].type != NONEPIECE) {
break;
@@ -351,31 +393,3 @@ void GenerateSlidingMoves(Game *b, int from,
}
};
};
-
-void GenerateKingMoves(Game *b, int from, std::vector<Move> &moves) {
- if (b->pieces[from].type != KING) {
- assert(false && "calling generate king moves on non king");
- return;
- }
- if (b->pieces[from].color != b->turn) {
- assert(false && "calling generate king moves on king of opposite color");
- return;
- }
- constexpr std::array<int, 8> king_moves{-1, 1, 8, -8, -9, 9, -7, 7};
- for (int offset : king_moves) {
- int next = from + offset;
- if (next >= 64 || next < 0) {
- continue;
- }
- if (std::abs((next % 8) - (from % 8)) > 1) {
- continue;
- };
- if (b->pieces[next].type != NONEPIECE) {
- if (b->pieces[next].color == b->turn) {
- continue;
- };
- }
-
- moves.push_back({IndexToPosition(from), IndexToPosition(next)});
- }
-}