diff options
| -rw-r--r-- | src/main.cpp | 36 | ||||
| -rw-r--r-- | src/moves.hpp | 4 |
2 files changed, 27 insertions, 13 deletions
diff --git a/src/main.cpp b/src/main.cpp index a508042..bea30b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,11 @@ +#include <algorithm> #include <cctype> #include <cstdint> #include <cstdio> #include <cstdlib> #include <print> #include <string> +#include <vector> #include "board.hpp" #include "moves.hpp" @@ -39,8 +41,7 @@ void PrintMoves(const std::vector<Move> &moves) { } } int main(int argc, char **argv) { - std::string startingFen = - "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + std::string startingFen = "rnbqkbnr/pppppppp/8/8/8/8/8/RNBQKBNR w KQkq - 0 1"; if (argc < 1 || argc > 2) { printf("wrong arg count, use: ./chess [optinal starting fen]\n"); return 1; @@ -59,8 +60,6 @@ int main(int argc, char **argv) { }; setBoardFen(startingFen, &b); printBoard(&b); - auto moves = GetLegalMoves(&b); - PrintMoves(moves); while (true) { std::cout << "> "; std::string command; @@ -103,15 +102,26 @@ int main(int argc, char **argv) { } // 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<int>(fromFile) << "\n"; - std::cout << static_cast<int>(fromRank) << "\n"; - std::cout << static_cast<int>(toFile) << "\n"; - std::cout << static_cast<int>(toRank) << "\n"; - Move move = {fromRank, fromFile, toRank, toFile}; + 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(); } return EXIT_SUCCESS; diff --git a/src/moves.hpp b/src/moves.hpp index 7e95910..5f893ca 100644 --- a/src/moves.hpp +++ b/src/moves.hpp @@ -8,11 +8,15 @@ struct Position { uint8_t rank; uint8_t file; + + bool operator==(const Position &) const = default; }; struct Move { Position From; Position To; + + bool operator==(const Move &) const = default; }; enum MoveResult { |
