diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index bb382f2..69872fd 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -1,11 +1,12 @@ #include "board.hpp" +#include "stickfosh.hpp" #include int main(int argc, char* argv[]) { std::string pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; - Board b = Board::setup_fen_position(pos); - sizeof(b); + // Board b = Board::setup_fen_position(pos); + perft(pos); return 0; } diff --git a/cpp/src/stickfosh.cpp b/cpp/src/stickfosh.cpp index e69de29..0d91941 100644 --- a/cpp/src/stickfosh.cpp +++ b/cpp/src/stickfosh.cpp @@ -0,0 +1,74 @@ +#include "board.hpp" +#include "move.hpp" + +#include +#include +#include +#include + +static std::string tick = "\033[92m✔️\033[0m"; // Green Tick; +static std::string cross = "\033[91m❌\033[0m"; // Red Cross + +static std::map> pos2expected{ + // -- Position 1 + { + "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", + { + {1, 20}, + {2, 400}, + {3, 8902}, + {4, 197281}, + }, + }, + + // -- Position 2 + { + "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1", + { + {1, 48}, + {2, 2039}, + {3, 97862}, + {4, 4085603}, + }, + }, +}; + +int move_generation_test(Board& b, int depth, int max_depth) { + std::cout << "hello" << std::endl; + if (b.is_terminal()) + return 0; + if (depth == 0) + return 1; + + std::vector moves = b.all_legal_moves(); + if (depth == 1) + return moves.size(); + + int num_pos = 0; + for (const Move& move : moves) { + Board tmp_board = b.make_move(move); + num_pos += move_generation_test(tmp_board, depth - 1, max_depth); + } + return num_pos; +} + +void perft(std::string pos) { + std::map expected = pos2expected[pos]; + Board b = Board::setup_fen_position(pos); + + for (const auto& [depth, expected_n_moves] : expected) { + std::cout << "Depth: " << depth << " "; + auto start = std::chrono::steady_clock::now(); + int moves = move_generation_test(b, depth, depth); + auto end = std::chrono::steady_clock::now(); + auto elapsed = (end - start).count(); + + std::cout << "Results: " << depth << " "; + if (moves == expected_n_moves) + std::cout << tick << " "; + else + std::cout << cross << " (expected " << expected_n_moves << ") "; + std::cout << "positions Time: " << elapsed << " milliseconds" + << std::endl; + } +} diff --git a/cpp/src/stickfosh.hpp b/cpp/src/stickfosh.hpp new file mode 100644 index 0000000..c560c39 --- /dev/null +++ b/cpp/src/stickfosh.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include + +void perft(std::string pos);