diff options
| author | Adam <adammegarules1@gmail.com> | 2026-07-26 19:16:57 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-07-26 19:16:57 +0200 |
| commit | 7e8d89f357aaf8d8a6148ef051752dd53eb0285f (patch) | |
| tree | b169dcc4e0bc00bc2bb39c9c4b6ced2d3f518864 /src/main.cpp | |
| parent | a8b482443dca91f6410a60e9023c6d9dd853b400 (diff) | |
adding repl
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 146 |
1 files changed, 39 insertions, 107 deletions
diff --git a/src/main.cpp b/src/main.cpp index 261c19d..4b9e965 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,50 +1,38 @@ #include "board.hpp" #include "fen.hpp" -#include "moves.hpp" -#include <algorithm> -#include <cctype> -#include <cstdint> +#include "repl.hpp" #include <cstdio> -#include <cstdlib> +#include <iostream> #include <print> #include <string> -#include <vector> -#include <iostream> +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; - } -} -void setAllPiecesToEmpty(board *b) { - Piece NonePiece = { +enum Mode { + EXIT, + REPL, +}; + +void setAllPiecesToEmpty(Board *b) { + Piece EmptyPiece = { false, NONE, false, }; for (int i = 0; i < 64; i++) { - b->pieces[i] = NonePiece; + b->pieces[i] = EmptyPiece; }; -} -void PrintMoves(const std::vector<Move> &moves) { - std::cout << "Moves (" << moves.size() << "):\n"; +}; +Board initBoard(string startingFEN) { + Board b; + b.turn = true; + b.castle = ""; + b.halfMoveClock = 0; + b.MoveClock = 0; + setBoardFen(startingFEN, &b); + return b; +}; - for (const Move &move : moves) { - std::cout << "(" << (int)move.From.file << ", " << (int)move.From.rank - << ") -> (" << (int)move.To.file << ", " << (int)move.To.rank - << ")\n"; - } -} int main(int argc, char **argv) { std::string startingFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; @@ -56,81 +44,25 @@ int main(int argc, char **argv) { if (argc == 2) { startingFen = argv[1]; } - board b; - b.turn = true; - b.castle = ""; - b.halfMoveClock = 0; - b.MoveClock = 0; - setBoardFen(startingFen, &b); - printBoard(&b); - while (true) { - std::cout << "> "; - std::string command; - std::cin >> command; - std::cout << command << "\n"; - - transform(command.begin(), command.end(), command.begin(), ::toupper); - if (command == "EXIT") { - std::println(); - std::cout << "Exiting...\n"; - return 0; - } + println("ENTER which mode you want (REPL)"); + Mode mode = EXIT; + string modeString; + cin >> modeString; + std::transform(modeString.begin(), modeString.end(), modeString.begin(), + ::toupper); - 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<uint8_t>(fromRank), static_cast<uint8_t>(fromFile)}, - {static_cast<uint8_t>(toRank), static_cast<uint8_t>(toFile)}}; + if (modeString == "REPL") { + mode = REPL; + } + // ADD support for other modes here - auto moves = GetLegalMoves(&b); - if (std::ranges::find(moves, move) != moves.end()) { - std::cout << "Move is legal!\n"; - } else { - std::cout << "Move is not legal!\n"; - PrintMoves(moves); - continue; - } - std::println(); - // PlayMove(move, &b); // todo implement - b.MoveClock++; - b.turn = !b.turn; - printBoard(&b); + Board b = initBoard(startingFen); + if (mode == REPL) { + int replExitCode = startREPL(&b); + return replExitCode; + } + if (mode == EXIT) { + return 0; } - return EXIT_SUCCESS; + return 0; } |
