#include #include #include #include #include #include #include "board.hpp" #include "moves.hpp" Piece NonePiece = { false, NONE, false, }; #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; } } 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"; } } int main(int argc, char **argv) { std::string startingFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; if (argc < 1 || argc > 2) { printf("wrong arg count, use: ./chess [optinal starting fen]\n"); return 1; } printf("arg count: %d\n", argc); if (argc == 2) { startingFen = argv[1]; } board b; b.turn = true; 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.length() != 4) { std::cout << "Unknown Command, use EXIT to exit"; std::println(); continue; } if (command == "EXIT") { std::println(); std::cout << "Exiting...\n"; return 0; } 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 uint8_t fromFile = command[0] - 'A'; uint8_t fromRank = command[1] - '0'; uint8_t toFile = command[2] - 'A'; uint8_t toRank = command[3] - '0'; std::cout << static_cast(fromFile) << "\n"; std::cout << static_cast(fromRank) << "\n"; std::cout << static_cast(toFile) << "\n"; std::cout << static_cast(toRank) << "\n"; Move move = {fromRank, fromFile, toRank, toFile}; std::println(); } return EXIT_SUCCESS; }