From 658a797ec8cb5b6d8607fac089e036685f2a3958 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 27 Jul 2026 10:26:54 +0200 Subject: adding enpassant --- src/board.cpp | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'src/board.cpp') 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; } -- cgit v1.2.3