From 658a797ec8cb5b6d8607fac089e036685f2a3958 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 27 Jul 2026 10:26:54 +0200 Subject: adding enpassant --- CMakeLists.txt | 22 +--------------------- src/board.cpp | 39 ++++++++++++++++++++++++++++++++++----- src/board.hpp | 16 +++++++++------- src/fen.cpp | 2 +- src/main.cpp | 3 +++ src/moves.cpp | 5 +++++ src/repl.cpp | 1 - 7 files changed, 53 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ecb495..9ba5a08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,27 +21,7 @@ else() -Wpedantic -Werror - -Wshadow - -Wconversion - -Wsign-conversion - -Wcast-align - -Wcast-qual - -Wformat=2 - -Wnull-dereference - -Wdouble-promotion - -Wimplicit-fallthrough - -Wmissing-declarations - -Wredundant-decls - -Wundef - -Wwrite-strings - -Wold-style-cast # C++ only - -Woverloaded-virtual # C++ only - -Wnon-virtual-dtor # C++ only - -Wduplicated-cond # GCC - -Wduplicated-branches # GCC - -Wlogical-op # GCC - -Wuseless-cast # C++ only - -Wextra-semi # Clang + ) endif() 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 #include @@ -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 &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); } -- cgit v1.2.3