aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-07-27 10:26:54 +0200
committerAdam <adammegarules1@gmail.com>2026-07-27 10:26:54 +0200
commit658a797ec8cb5b6d8607fac089e036685f2a3958 (patch)
tree2de2efc25079b74ab4cf8cdcba9ea5c4e0469ece /src
parent9b1665430fd0379db92000740c7a70045baa1746 (diff)
adding enpassant
Diffstat (limited to 'src')
-rw-r--r--src/board.cpp39
-rw-r--r--src/board.hpp16
-rw-r--r--src/fen.cpp2
-rw-r--r--src/main.cpp3
-rw-r--r--src/moves.cpp5
-rw-r--r--src/repl.cpp1
6 files changed, 52 insertions, 14 deletions
diff --git a/src/board.cpp b/src/board.cpp
index 877e8c7..a74c5dc 100644
--- a/src/board.cpp
+++ b/src/board.cpp
@@ -35,7 +35,7 @@ Piece createPiece(PieceType type, bool color, bool moved) {
piece.color = color;
piece.moved = moved;
return piece;
-};
+}
void printBoard(Board *b) {
std::string line = " +-----------------+\n";
std::string whiteLetters = " a b c d e f g h\n";
@@ -87,6 +87,9 @@ void printBoard(Board *b) {
assert(b->castle != "");
std::println("Castling: {}", b->castle);
+ std::println("Can enpassant: {}", b->canEnpassant ? "Yes" : "No");
+ std::println("Where enpassant: {}{}", (char)((int)b->enPassant.file + 'A'),
+ 8 - (int)b->enPassant.rank);
}
void PlayMove(Move move, Board *b) {
@@ -98,8 +101,34 @@ void PlayMove(Move move, Board *b) {
assert(false && "capturing friendly piece error");
}
}
- b->pieces[PositionToIndex(move.To)] = piece;
- b->pieces[PositionToIndex(move.From)] = {false, NONE, false};
- return;
-}
+ piece.moved = true;
+ if (piece.type == PAWN || piece2.type == NONE) {
+ b->halfMoveClock = 0;
+ } else {
+ b->halfMoveClock++;
+ }
+ if (piece.type == PAWN && b->canEnpassant && move.To == b->enPassant) {
+ Position capturedPawn = move.To;
+ capturedPawn.rank += piece.color ? 1 : -1;
+ b->pieces[PositionToIndex(capturedPawn)] = {false, NONE, false};
+ }
+
+ b->canEnpassant = false;
+ b->MoveClock++;
+
+ if (piece.type == PAWN) {
+ Position to = move.To;
+ to.rank -= (piece.color ? -2 : 2);
+ if (to == move.From) {
+ b->canEnpassant = true;
+ Position target = move.From;
+ target.rank += piece.color ? -1 : 1;
+ b->enPassant = target;
+ };
+
+ b->pieces[PositionToIndex(move.To)] = piece;
+ b->pieces[PositionToIndex(move.From)] = {false, NONE, false};
+ return;
+ }
+};
int PositionToIndex(Position i) { return i.rank * 8 + i.file; }
diff --git a/src/board.hpp b/src/board.hpp
index 05706ff..44d32ee 100644
--- a/src/board.hpp
+++ b/src/board.hpp
@@ -28,22 +28,24 @@ struct Piece {
bool moved;
};
+struct Position {
+ uint8_t rank;
+ uint8_t file;
+
+ bool operator==(const Position &) const = default;
+};
+
struct Board {
Piece pieces[64];
bool turn; // 1 white; 0 black
std::string castle;
uint8_t halfMoveClock;
uint16_t MoveClock;
+ Position enPassant;
+ bool canEnpassant;
GameState state;
};
-struct Position {
- uint8_t rank;
- uint8_t file;
-
- bool operator==(const Position &) const = default;
-};
-
struct Move {
Position From;
Position To;
diff --git a/src/fen.cpp b/src/fen.cpp
index 2808a4c..fa8f289 100644
--- a/src/fen.cpp
+++ b/src/fen.cpp
@@ -82,7 +82,7 @@ void setBoardFen(std::string fen, Board *b) {
if (isdigit(fen[i])) {
int move = fen[i] - '0';
file += move;
- if (file >= 8) {
+ if (file > 8) {
std::println("Fatal: file was to big");
exit(1);
return; // malformed rank
diff --git a/src/main.cpp b/src/main.cpp
index b8dd297..706ca28 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,5 +1,6 @@
#include "board.hpp"
#include "fen.hpp"
+#include "moves.hpp"
#include "repl.hpp"
#include <cstdio>
#include <string>
@@ -23,6 +24,8 @@ void setAllPiecesToEmpty(Board *b) {
};
Board initBoard(string startingFEN) {
Board b;
+ b.enPassant = IndexToPosition(0); // default value
+ b.canEnpassant = false;
b.state = TURN;
b.turn = true;
b.castle = "";
diff --git a/src/moves.cpp b/src/moves.cpp
index 7efd00a..81ea1df 100644
--- a/src/moves.cpp
+++ b/src/moves.cpp
@@ -70,6 +70,11 @@ void GeneratePawnMoves(Board *b, int from, std::vector<Move> &moves) {
}
int target = (position.rank + (pawn.color ? -1 : 1)) * 8 + targetFile;
+
+ if (IndexToPosition(target) == b->enPassant && b->canEnpassant) {
+ // enpasstant!!!!!!!
+ moves.push_back({position, IndexToPosition(target)});
+ }
if (b->pieces[target].type != NONE &&
b->pieces[target].color != pawn.color) {
moves.push_back({position, IndexToPosition(target)});
diff --git a/src/repl.cpp b/src/repl.cpp
index ffa52b6..2ffa59b 100644
--- a/src/repl.cpp
+++ b/src/repl.cpp
@@ -121,7 +121,6 @@ int startREPL(Board *b) {
}
std::println();
PlayMove(move, b);
- b->MoveClock++;
b->turn = !b->turn;
printBoard(b);
}