checking perft to see if my cpp implementation is

correct
This commit is contained in:
Karma Riuk 2025-02-03 00:33:12 +01:00
parent 540ffa4fb3
commit 5e4b880aad
3 changed files with 82 additions and 2 deletions

View File

@ -1,11 +1,12 @@
#include "board.hpp"
#include "stickfosh.hpp"
#include <iostream>
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;
}

View File

@ -0,0 +1,74 @@
#include "board.hpp"
#include "move.hpp"
#include <chrono>
#include <map>
#include <string>
#include <vector>
static std::string tick = "\033[92m✔\033[0m"; // Green Tick;
static std::string cross = "\033[91m❌\033[0m"; // Red Cross
static std::map<std::string, std::map<int, int>> 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<Move> 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<int, int> 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;
}
}

5
cpp/src/stickfosh.hpp Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <string>
void perft(std::string pos);