From 40e242dc5450e1661c817b0bad8f9748388c278a Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 28 Jul 2026 19:03:20 +0200 Subject: removing repl and unsuded featuares --- src/board.cpp | 140 ------------------------------------------------ src/board.hpp | 4 +- src/main.cpp | 44 +--------------- src/uci.cpp | 167 ++++------------------------------------------------------ src/uci.hpp | 5 -- 5 files changed, 14 insertions(+), 346 deletions(-) diff --git a/src/board.cpp b/src/board.cpp index 41eef19..4daddfd 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -1,34 +1,8 @@ #include "board.hpp" -#include "bot.hpp" #include "zobrist.hpp" #include -#include -#include -#include #include -std::string toChar(PieceType type, bool white) { - switch (type) { - case NONE: - return " "; - case KING: - return white ? "♚" : "♔"; - case QUEEN: - return white ? "♛" : "♕"; - case ROOK: - return white ? "♜" : "♖"; - case BISHOP: - return white ? "♝" : "♗"; - case KNIGHT: - return white ? "♞" : "♘"; - case PAWN: - return white ? "♟" : "♙"; - default: - assert(false && "Unknown piece"); - return " "; - } -} - Piece createPiece(PieceType type, bool color, bool moved) { Piece piece; piece.type = type; @@ -36,121 +10,7 @@ Piece createPiece(PieceType type, bool color, bool moved) { piece.moved = moved; return piece; } -void printBoard(Game *b) { - std::string line = " +------------------------+\n"; - std::string whiteLetters = " a b c d e f g h \n"; - std::string blackLetters = " h g f e d c b a \n"; - - const char *lightSquare = "\033[48;5;245m"; - const char *darkSquare = "\033[48;5;29m"; - const char *reset = "\033[0m"; - - std::cout << (b->turn ? whiteLetters : blackLetters); - std::cout << line; - - if (b->turn) { - for (int rank = 0; rank < 8; rank++) { - std::printf("%d |", 8 - rank); - - for (int file = 0; file < 8; file++) { - Piece piece = b->pieces[rank * 8 + file]; - std::string symbol = toChar(piece.type, piece.color); - - const char *bg = ((rank + file) % 2 == 0) ? lightSquare : darkSquare; - - std::cout << bg << " " << symbol << " " << reset; - } - - std::printf("| %d\n", 8 - rank); - } - } else { - for (int rank = 7; rank >= 0; rank--) { - std::printf("%d |", 8 - rank); - - for (int file = 7; file >= 0; file--) { - Piece piece = b->pieces[rank * 8 + file]; - std::string symbol = toChar(piece.type, piece.color); - - const char *bg = ((rank + file) % 2 == 0) ? lightSquare : darkSquare; - - std::cout << bg << " " << symbol << " " << reset; - } - - std::printf("| %d\n", 8 - rank); - } - } - - std::cout << line; - std::cout << (b->turn ? whiteLetters : blackLetters); - - std::println(); - std::println("Turn: {}", b->turn ? "White" : "Black"); - - 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); - std::printf("Move clock: %d\n", b->MoveClock); - std::printf("Half move clock: %d\n", b->halfMoveClock); - std::printf("Board Eval: %f\n", EvaluateBoardForWhite(b, 0)); -} -void PlayMove(Move move, Game *b) { - // TODO: add all fide behavior - Piece piece = b->pieces[PositionToIndex(move.From)]; - Piece piece2 = b->pieces[PositionToIndex(move.To)]; - if (piece2.type != NONE) { - if (piece2.color == b->turn) { - assert(false && "capturing friendly piece error"); - } - } - 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; - }; - } - - if (piece.type == PAWN && move.To.rank == (piece.color ? 0 : 7)) { - // TODO parse the promote type from uci isntead of ignoreting it - piece.type = move.promotion == NONE ? QUEEN : move.promotion; - } - if (b->halfMoveClock >= 50) { - b->state = DRAW; - } - b->pieces[PositionToIndex(move.To)] = piece; - b->pieces[PositionToIndex(move.From)] = {false, NONE, false}; - - b->turn = !b->turn; - - uint64_t key = GenerateZobristKey(b); - - b->ThreeFoldMap[key]++; - if (b->ThreeFoldMap[key] >= 3) { - b->state = DRAW; - } -}; int PositionToIndex(Position i) { return i.rank * 8 + i.file; } UndoMove MakeMove(Move move, Game *g) { diff --git a/src/board.hpp b/src/board.hpp index 2c3bba9..993a66d 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -4,6 +4,7 @@ #include #include #include + enum PieceType { NONE, PAWN, @@ -21,7 +22,6 @@ enum GameState { STALEMATE, DRAW, }; -std::string toChar(PieceType type, bool white); struct Piece { bool color = 0; @@ -77,10 +77,8 @@ struct UndoMove { uint64_t zobristKey; GameState oldState; }; -void printBoard(Game *b); Piece createPiece(PieceType type, bool color, bool moved = false); -// void PlayMove(Move move, Game *b); int PositionToIndex(Position i); UndoMove MakeMove(Move move, Game *g); void UnMakeMove(UndoMove undo, Game *g); diff --git a/src/main.cpp b/src/main.cpp index ddeb304..8a1821e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,52 +1,12 @@ -#include "board.hpp" -#include "fen.hpp" -#include "moves.hpp" #include "uci.hpp" #include "zobrist.hpp" -#include + #include #include -#include - -using namespace std; - -enum Mode { - EXIT, - REPL, -}; - -void setAllPiecesToEmpty(Game *b) { - Piece EmptyPiece = { - false, - NONE, - false, - }; - for (int i = 0; i < 64; i++) { - b->pieces[i] = EmptyPiece; - }; -}; -Game initBoard(string startingFEN) { - Game b; - b.enPassant = IndexToPosition(0); // default value - b.canEnpassant = false; - b.state = TURN; - b.turn = true; - b.castle = ""; - b.halfMoveClock = 0; - b.MoveClock = 0; - setBoardFen(startingFEN, &b); - return b; -}; -int main(int argc, char **argv) { +int main() { InitZobrist(); srand(time(0)); - if (argc < 1 || argc > 2) { - printf("wrong arg count, use: ./chess [--repl for debugging]\n"); - return 1; - } - if (argc > 1 && std::string(argv[1]) == "--repl") - return startREPL(); Uci(); return 0; diff --git a/src/uci.cpp b/src/uci.cpp index 6f0e5e6..1f634fe 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -7,8 +7,6 @@ #include #include #include -#include -#include #include #include #include @@ -33,20 +31,7 @@ void LogUci(const std::string &message) { uciLog.flush(); } using namespace std; -bool isValidChessRank(char rank) { - if (rank >= '1' && rank <= '8') { - return true; - } else { - return false; - } -} -bool isValidChessFile(char rank) { - if (rank >= 'A' && rank <= 'H') { - return true; - } else { - return false; - } -} + Move UciToMove(string uci) { Position from; Position to; @@ -69,6 +54,16 @@ static Game initBoard(string startingFEN) { b.halfMoveClock = 0; b.MoveClock = 0; setBoardFen(startingFEN, &b); + + Piece EmptyPiece = { + false, + NONE, + false, + }; + for (int i = 0; i < 64; i++) { + b.pieces[i] = EmptyPiece; + }; + return b; }; void Uci() { @@ -161,143 +156,3 @@ void Uci() { } } } -int startREPL() { - - Game g = - initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - Game *b = &g; - printBoard(b); - while (true) { - GetLegalMoves(b); - if (b->state == STALEMATE) { - printBoard(b); - std::println(); - std::cout << "\033[1mStalemate\033[0m" << "\n"; - std::println(); - return 0; - } - if (b->state == DRAW) { - printBoard(b); - std::println(); - std::cout << "\033[1mDraw\033[0m" << "\n"; - std::println(); - return 0; - } - if (b->state == WHITE_WON) { - printBoard(b); - std::cout << "\033[1mChechmate White won\033[0m" << "\n"; - return 0; - } - if (b->state == BLACK_WON) { - printBoard(b); - std::cout << "\033[1mCheckmate Black won\033[0m" << "\n"; - return 0; - } - if (b->state == TURN) { - if (true) { - Move move = EngineGetBestMove(b); - MakeMove(move, b); - printBoard(b); - continue; - } - } - std::cout << "> "; - std::string command; - if (!(std::cin >> command)) { - std::println(); - std::cout << "Exiting...\n"; - return EXIT_SUCCESS; - } - - std::transform(command.begin(), command.end(), command.begin(), ::toupper); - - std::cout << command << "\n"; - if (command == "EXIT") { - std::println(); - std::cout << "Exiting...\n"; - return EXIT_SUCCESS; - } - if (command == "RESING") { - std::cout << "\033[1mOK\033[0m" << "\n"; - if (b->turn) { - b->state = BLACK_WON; - } else { - b->state = WHITE_WON; - } - continue; - } - if (command == "BOARD") { - printBoard(b); - continue; - } - if (command == "MOVES" || command == "MOVE") { - auto moves = GetLegalMoves(b); - PrintMoves(moves); - continue; - } - if (command.length() != 4) { - std::cout << "Unknown Command, use EXIT to exit"; - std::println(); - continue; - } - if (b->state == TURN) { - if (!isValidChessFile(command[0])) { - std::cout << "ERROR: Expected valid file at first place"; - std::println(); - continue; - } - - if (!isValidChessRank(command[1])) { - std::cout << "ERROR: Expected valid rank at second place"; - std::println(); - continue; - } - - if (!isValidChessFile(command[2])) { - std::cout << "ERROR: Expected valid file at third place"; - std::println(); - continue; - } - - if (!isValidChessRank(command[3])) { - std::cout << "ERROR: Expected valid rank at four place"; - std::println(); - continue; - } - - // we know its a valid chess pos - int fromFile = command[0] - 'A'; - int fromRank = '8' - command[1]; - int toFile = command[2] - 'A'; - int toRank = '8' - command[3]; - std::cout << fromFile << "\n"; - std::cout << fromRank << "\n"; - std::cout << toFile << "\n"; - std::cout << toRank << "\n"; - Move move = { - {static_cast(fromRank), static_cast(fromFile)}, - {static_cast(toRank), static_cast(toFile)}}; - - auto moves = GetLegalMoves(b); - if (!(std::ranges::find(moves, move) != moves.end())) { - std::cout << "Invalid move!"; - std::println(); - PrintMoves(moves); - continue; - } - std::println(); - MakeMove(move, b); - printBoard(b); - } - } -} - -void PrintMoves(const std::vector &moves) { - std::cout << "Moves (" << moves.size() << "):\n"; - - for (const Move &move : moves) { - std::cout << (char)((int)move.From.file + 'A') << 8 - (int)move.From.rank - << " -> " << (char)((int)move.To.file + 'A') - << 8 - (int)move.To.rank << "\n"; - } -} diff --git a/src/uci.hpp b/src/uci.hpp index 18e216e..4d0a91b 100644 --- a/src/uci.hpp +++ b/src/uci.hpp @@ -3,12 +3,7 @@ #include "board.hpp" #include -#include -int startREPL(); -void PrintMoves(const std::vector &moves); -bool isValidChessRank(char rank); -bool isValidChessFile(char rank); Move UciToMove(std::string uci); void LogUci(const std::string &message); void Uci(); -- cgit v1.2.3