diff options
| author | Adam <adammegarules1@gmail.com> | 2026-07-27 09:45:58 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-07-27 09:45:58 +0200 |
| commit | 9b1665430fd0379db92000740c7a70045baa1746 (patch) | |
| tree | 728993a22f9610a1299c0ef1dd27a63a292b3fc3 /src/repl.cpp | |
| parent | 94cc9240eabf78cbbc9006cb70ca74b6bc3f54e6 (diff) | |
lot of changes
Diffstat (limited to 'src/repl.cpp')
| -rw-r--r-- | src/repl.cpp | 124 |
1 files changed, 77 insertions, 47 deletions
diff --git a/src/repl.cpp b/src/repl.cpp index 642419a..ffa52b6 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -25,12 +25,36 @@ bool isValidChessFile(char rank) { } int startREPL(Board *b) { printBoard(b); + GetLegalMoves(b); while (true) { + if (b->state == STALEMATE) { + printBoard(b); + std::println(); + std::cout << "\033[1mStalemate\033[0m" << "\n"; + std::println(); + return 0; + } + if (b->state == WHITE_WON) { + printBoard(b); + std::cout << "\033[1mWhite won\033[0m" << "\n"; + return 0; + } + if (b->state == BLACK_WON) { + printBoard(b); + std::cout << "\033[1mBlack won\033[0m" << "\n"; + return 0; + } std::cout << "> "; std::string command; - std::cin >> 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"; @@ -40,61 +64,67 @@ int startREPL(Board *b) { printBoard(b); continue; } - - if (command.length() != 4) { - std::cout << "Unknown Command, use EXIT to exit"; - std::println(); + if (command == "MOVES" || command == "MOVE") { + auto moves = GetLegalMoves(b); + PrintMoves(moves); continue; } - if (!isValidChessFile(command[0])) { - std::cout << "ERROR: Expected valid file at first place"; + 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 (!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 (!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; - } + 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<uint8_t>(fromRank), static_cast<uint8_t>(fromFile)}, - {static_cast<uint8_t>(toRank), static_cast<uint8_t>(toFile)}}; + // 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<uint8_t>(fromRank), static_cast<uint8_t>(fromFile)}, + {static_cast<uint8_t>(toRank), static_cast<uint8_t>(toFile)}}; - auto moves = GetLegalMoves(b); - if (!(std::ranges::find(moves, move) != moves.end())) { - std::cout << "Invalid move!"; + auto moves = GetLegalMoves(b); + if (!(std::ranges::find(moves, move) != moves.end())) { + std::cout << "Invalid move!"; + std::println(); + PrintMoves(moves); + continue; + } std::println(); - PrintMoves(moves); - continue; + PlayMove(move, b); + b->MoveClock++; + b->turn = !b->turn; + printBoard(b); } - std::println(); - PlayMove(move, b); - b->MoveClock++; - b->turn = !b->turn; - printBoard(b); } } @@ -102,8 +132,8 @@ void PrintMoves(const std::vector<Move> &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"; + 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"; } } |
