diff options
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 114 |
1 files changed, 101 insertions, 13 deletions
diff --git a/src/main.cpp b/src/main.cpp index 5be1194..261c19d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,17 +1,41 @@ +#include "board.hpp" +#include "fen.hpp" +#include "moves.hpp" +#include <algorithm> +#include <cctype> +#include <cstdint> #include <cstdio> #include <cstdlib> +#include <print> #include <string> +#include <vector> -#include "board.hpp" -#include "moves.hpp" - -Piece NonePiece = { - false, - NONE, - false, -}; #include <iostream> +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 = { + false, + NONE, + false, + }; + for (int i = 0; i < 64; i++) { + b->pieces[i] = NonePiece; + }; +} void PrintMoves(const std::vector<Move> &moves) { std::cout << "Moves (" << moves.size() << "):\n"; @@ -37,12 +61,76 @@ int main(int argc, char **argv) { b.castle = ""; b.halfMoveClock = 0; b.MoveClock = 0; - for (int i = 0; i < 64; i++) { - b.pieces[i] = NonePiece; - }; setBoardFen(startingFen, &b); printBoard(&b); - auto moves = GetLegalMoves(&b); - PrintMoves(moves); + 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; + } + + 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)}}; + + 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); + } return EXIT_SUCCESS; } |
