diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/board.cpp | 1 | ||||
| -rw-r--r-- | src/main.cpp | 13 | ||||
| -rw-r--r-- | src/moves.cpp | 60 | ||||
| -rw-r--r-- | src/moves.hpp | 6 |
4 files changed, 80 insertions, 0 deletions
diff --git a/src/board.cpp b/src/board.cpp index 92704c3..64a21d2 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -48,6 +48,7 @@ void printBoard(board *b) { std::printf("%d to play\n", b->turn); std::cout << "castle: " << b->castle << "\n"; } + void setBoardFen(std::string fen, board *b) { // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 int file = 0; diff --git a/src/main.cpp b/src/main.cpp index fa4b3a6..fd95755 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,13 +3,24 @@ #include <string> #include "board.hpp" +#include "moves.hpp" Piece NonePiece = { false, NONE, false, }; +#include <iostream> +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"; + } +} int main(int argc, char **argv) { printf("arg count: %d", argc); std::string startingFen = @@ -31,5 +42,7 @@ int main(int argc, char **argv) { }; setBoardFen(startingFen, &b); printBoard(&b); + auto moves = GetLegalMoves(&b); + PrintMoves(moves); return EXIT_SUCCESS; } diff --git a/src/moves.cpp b/src/moves.cpp index f417499..65a380a 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -1,3 +1,63 @@ +#include <array> +#include <cstdint> + +#include "board.hpp" #include "moves.hpp" +std::vector<Move> GetLegalMoves(board *b) { + std::vector<Move> moves; + moves.reserve(50); // almost all position dont have that many moves + + constexpr std::array<int, 4> rook_Moves{-1, 1, 8, -8}; + constexpr std::array<int, 4> bishop_Moves{-9, 9, -7, 7}; + + for (int i = 0; i < sizeof(b->pieces) / sizeof(b->pieces[0]); i++) { + Piece piece = b->pieces[i]; + if (piece.type == NONE) { + continue; + } + if (piece.color != b->turn) + continue; + + // Add support for other pieces + if (piece.type == BISHOP) { + GenerateSlidingMoves(b, i, bishop_Moves, moves); + } + if (piece.type == ROOK) { + GenerateSlidingMoves(b, i, rook_Moves, moves); + } + }; + + return moves; +} +Position IndexToPosition(int i) { + uint8_t rank = i / 8; // 0-7 + uint8_t file = i % 8; // 0-7 + return {rank, file}; +} +void GenerateSlidingMoves(board *b, int from, + const std::array<int, 4> &directions, + std::vector<Move> &moves) { + for (int i = 0; i < directions.size(); i++) { + int direction = directions[i]; + int i2 = from; + while (true) { + i2 += direction; + if (i2 >= 64 || i2 < 0) { + break; + } + if (b->pieces[i2].color == b->turn && b->pieces[i2].type != NONE) { + break; + } + if ((direction == 1 || direction == -1) && + (i2 / 8 != (i2 - direction) / 8)) { + break; + } + moves.push_back({IndexToPosition(i2), IndexToPosition(i2)}); + if (b->pieces[i2].color != b->turn && b->pieces[i2].type != NONE) { + break; + } + } + }; +}; diff --git a/src/moves.hpp b/src/moves.hpp index 17a9d7d..7e95910 100644 --- a/src/moves.hpp +++ b/src/moves.hpp @@ -3,6 +3,7 @@ #include "board.hpp" #include <cstdint> +#include <vector> struct Position { uint8_t rank; @@ -21,5 +22,10 @@ enum MoveResult { }; MoveResult PlayMove(Move move, board *b); // TODO implement +std::vector<Move> GetLegalMoves(board *b); +Position IndexToPosition(int i); +void GenerateSlidingMoves(board *b, int from, + const std::array<int, 4> &directions, + std::vector<Move> &moves); #endif /* SRC_MOVES_H_ */ |
