aboutsummaryrefslogtreecommitdiff
path: root/src/moves.cpp
diff options
context:
space:
mode:
authorPeter B. <peter.bezdek@gmail.com>2026-07-25 22:10:18 +0200
committerPeter B. <peter.bezdek@gmail.com>2026-07-25 22:10:18 +0200
commit63039394e4f8ef056289805bcee6ad1e7519de0a (patch)
tree33e88a5bec7037042f9a38ab5940f066fda9c414 /src/moves.cpp
parente9c8925045ff30c9af08a74000a634559878e96e (diff)
implement pawn movement logic and add unit tests for pawn moves
Diffstat (limited to 'src/moves.cpp')
-rw-r--r--src/moves.cpp104
1 files changed, 88 insertions, 16 deletions
diff --git a/src/moves.cpp b/src/moves.cpp
index 2ff8ed6..e36d77c 100644
--- a/src/moves.cpp
+++ b/src/moves.cpp
@@ -4,29 +4,90 @@
#include "board.hpp"
#include "moves.hpp"
-std::vector<Move> GetLegalMoves(board *b) {
+static void GeneratePawnMoves(board *b, int from, std::vector<Move> &moves)
+{
+ Piece pawn = b->pieces[from];
+ if (pawn.type != PAWN)
+ {
+ return;
+ }
+ Position position = IndexToPosition(from);
+ int step = (pawn.color ? -1 : 1) * 8;
+ int next = from + step;
+
+ // wrap check
+ if (position.rank + (pawn.color ? -1 : 1) < 0 || position.rank + (pawn.color ? -1 : 1) >= 8)
+ {
+ return;
+ }
+
+ // if piece it want to move to is none and it as legal move
+ if (b->pieces[next].type == NONE)
+ {
+ moves.push_back({position, IndexToPosition(next)}); // adding legal move
+
+ // todo find why this code is here and what it does
+ int startingRank = pawn.color ? 6 : 1;
+ int twoSteps = from + step * 2;
+ if (position.rank == startingRank && b->pieces[twoSteps].type == NONE)
+ {
+ moves.push_back({position, IndexToPosition(twoSteps)});
+ }
+ }
+
+ // check
+ for (int fileOffset : {-1, 1})
+ {
+ int targetFile = position.file + fileOffset;
+ if (targetFile < 0 || targetFile >= 8)
+ {
+ continue;
+ }
+
+ int target = (position.rank + (pawn.color ? -1 : 1)) * 8 + targetFile;
+ if (b->pieces[target].type != NONE &&
+ b->pieces[target].color != pawn.color)
+ {
+ moves.push_back({position, IndexToPosition(target)});
+ }
+ }
+ // the end
+}
+
+std::vector<Move> GetLegalMoves(board *b)
+{
std::vector<Move> moves;
moves.reserve(50); // almost all position dont have that many moves
constexpr std::array<int, 4> rook_Moves{-1, 1, 8, -8};
constexpr std::array<int, 4> bishop_Moves{-9, 9, -7, 7};
- for (int i = 0; i < sizeof(b->pieces) / sizeof(b->pieces[0]); i++) {
+ for (int i = 0; i < sizeof(b->pieces) / sizeof(b->pieces[0]); i++)
+ {
Piece piece = b->pieces[i];
- if (piece.type == NONE) {
+ if (piece.type == NONE)
+ {
continue;
}
if (piece.color != b->turn)
continue;
- // Add support for other pieces
- if (piece.type == BISHOP) {
+ // add support for knight and king later
+
+ if (piece.type == PAWN)
+ {
+ GeneratePawnMoves(b, i, moves);
+ }
+ if (piece.type == BISHOP)
+ {
GenerateSlidingMoves(b, i, bishop_Moves, moves);
}
- if (piece.type == ROOK) {
+ if (piece.type == ROOK)
+ {
GenerateSlidingMoves(b, i, rook_Moves, moves);
}
- if (piece.type == QUEEN) {
+ if (piece.type == QUEEN)
+ {
GenerateSlidingMoves(b, i, rook_Moves, moves);
GenerateSlidingMoves(b, i, bishop_Moves, moves);
}
@@ -34,40 +95,51 @@ std::vector<Move> GetLegalMoves(board *b) {
return moves;
}
-Position IndexToPosition(int i) {
+
+Position IndexToPosition(int i)
+{
uint8_t rank = i / 8; // 0-7
uint8_t file = i % 8; // 0-7
return {rank, file};
}
+
void GenerateSlidingMoves(board *b, int from,
const std::array<int, 4> &directions,
- std::vector<Move> &moves) {
- for (int i = 0; i < directions.size(); i++) {
+ std::vector<Move> &moves)
+{
+ for (int i = 0; i < directions.size(); i++)
+ {
int direction = directions[i];
int i2 = from;
- while (true) {
+ while (true)
+ {
i2 += direction;
int oldFile = (i2 - direction) % 8;
int newFile = i2 % 8;
if (direction == 7 || direction == -7 || direction == 9 ||
- direction == -9) {
+ direction == -9)
+ {
if (std::abs(newFile - oldFile) != 1)
break;
}
- if (i2 >= 64 || i2 < 0) {
+ if (i2 >= 64 || i2 < 0)
+ {
break;
}
- if (b->pieces[i2].color == b->turn && b->pieces[i2].type != NONE) {
+ if (b->pieces[i2].color == b->turn && b->pieces[i2].type != NONE)
+ {
break;
}
if ((direction == 1 || direction == -1) &&
- (i2 / 8 != (i2 - direction) / 8)) {
+ (i2 / 8 != (i2 - direction) / 8))
+ {
break;
}
moves.push_back({IndexToPosition(from), IndexToPosition(i2)});
- if (b->pieces[i2].color != b->turn && b->pieces[i2].type != NONE) {
+ if (b->pieces[i2].color != b->turn && b->pieces[i2].type != NONE)
+ {
break;
}
}