From 92cd6b4cd6321437bcbccb6b7f4396e60ff888b7 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 27 Jul 2026 13:28:57 +0200 Subject: adding bot and fixing bugs --- src/board.cpp | 11 ++++++----- src/repl.cpp | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/board.cpp b/src/board.cpp index a74c5dc..724f8ca 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -90,6 +90,8 @@ void printBoard(Board *b) { std::println("Can enpassant: {}", b->canEnpassant ? "Yes" : "No"); std::println("Where enpassant: {}{}", (char)((int)b->enPassant.file + 'A'), 8 - (int)b->enPassant.rank); + std::printf("Move clock: %d\n", b->MoveClock); + std::printf("Half move clock: %d\n", b->halfMoveClock); } void PlayMove(Move move, Board *b) { @@ -102,7 +104,7 @@ void PlayMove(Move move, Board *b) { } } piece.moved = true; - if (piece.type == PAWN || piece2.type == NONE) { + if (piece.type == PAWN || piece2.type != NONE) { b->halfMoveClock = 0; } else { b->halfMoveClock++; @@ -125,10 +127,9 @@ void PlayMove(Move move, Board *b) { target.rank += piece.color ? -1 : 1; b->enPassant = target; }; - - b->pieces[PositionToIndex(move.To)] = piece; - b->pieces[PositionToIndex(move.From)] = {false, NONE, false}; - return; } + + b->pieces[PositionToIndex(move.To)] = piece; + b->pieces[PositionToIndex(move.From)] = {false, NONE, false}; }; int PositionToIndex(Position i) { return i.rank * 8 + i.file; } diff --git a/src/repl.cpp b/src/repl.cpp index 783d96c..76f921e 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -25,8 +25,8 @@ bool isValidChessFile(char rank) { } int startREPL(Board *b) { printBoard(b); - GetLegalMoves(b); while (true) { + GetLegalMoves(b); if (b->state == STALEMATE) { printBoard(b); std::println(); @@ -36,14 +36,29 @@ int startREPL(Board *b) { } if (b->state == WHITE_WON) { printBoard(b); - std::cout << "\033[1mWhite won\033[0m" << "\n"; + std::cout << "\033[1mChechmate White won\033[0m" << "\n"; return 0; } if (b->state == BLACK_WON) { printBoard(b); - std::cout << "\033[1mBlack won\033[0m" << "\n"; + std::cout << "\033[1mCheckmate Black won\033[0m" << "\n"; return 0; } + if (b->state == TURN) { + if (!b->turn) { + auto moves = GetLegalMoves(b); + std::srand( + std::time(0)); // use current time as seed for random generator + int random_pos = std::rand() % + moves.size(); // Modulo to restrict the number of + // random values to be at most A.size()-1 + Move move = moves[random_pos]; + PlayMove(move, b); + b->turn = !b->turn; + printBoard(b); + continue; + } + } std::cout << "> "; std::string command; if (!(std::cin >> command)) { -- cgit v1.2.3