#include "repl.hpp" #include "board.hpp" #include "moves.hpp" #include #include #include #include #include #include #include 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; } } int startREPL(Board *b) { printBoard(b); while (true) { std::cout << "> "; std::string command; std::cin >> command; std::transform(command.begin(), command.end(), command.begin(), ::toupper); if (command == "EXIT") { std::println(); std::cout << "Exiting...\n"; return EXIT_SUCCESS; } if (command == "BOARD") { printBoard(b); continue; } if (command.length() != 4) { std::cout << "Unknown Command, use EXIT to exit"; std::println(); continue; } 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(); PlayMove(move, b); b->MoveClock++; b->turn = !b->turn; printBoard(b); } } void PrintMoves(const std::vector &moves) { std::cout << "Moves (" << moves.size() << "):\n"; for (const Move &move : moves) { std::cout << "(" << (int)move.From.file << ", " << (int)move.From.rank << ") -> (" << (int)move.To.file << ", " << (int)move.To.rank << ")\n"; } }