Compare commits
53 Commits
84c3af2bb1
...
c55645ad73
Author | SHA1 | Date | |
---|---|---|---|
|
c55645ad73 | ||
|
1b0af3b486 | ||
|
df20060075 | ||
|
151a50fba9 | ||
|
609d5f8c98 | ||
|
e9c96e83da | ||
|
76310400e8 | ||
|
e5bb2d1a5f | ||
|
4c86d5e815 | ||
|
b1812f1df6 | ||
|
9d21141d3c | ||
|
974af5d19f | ||
|
df07b4399d | ||
|
fa59784230 | ||
|
be34045f92 | ||
|
4eaed699c0 | ||
|
310abb0611 | ||
|
b70ca5302a | ||
|
83af5f0b97 | ||
|
72ba6a80ae | ||
|
1723356f62 | ||
|
75adb4b1ba | ||
|
6951b860da | ||
|
805d9fa95c | ||
|
4df7cbf01a | ||
|
00137e022a | ||
|
570b8df4a7 | ||
|
9a66a71c38 | ||
|
41e4119468 | ||
|
e7fc1b06de | ||
|
5f79b81ce4 | ||
|
e5819ee83b | ||
|
58109c5120 | ||
|
6478176a7d | ||
|
ab088da33c | ||
|
31799cb0ec | ||
|
96dcbc0a6c | ||
|
ce06097063 | ||
|
3fc2aa73ad | ||
|
de6a03ce08 | ||
|
dc3031ddc1 | ||
|
5e4b880aad | ||
|
540ffa4fb3 | ||
|
21136a26f0 | ||
|
fa57dcfc30 | ||
|
8da349579d | ||
|
f0467bc516 | ||
|
a7cf8e0c21 | ||
|
2128159914 | ||
|
c564add509 | ||
|
31b0656332 | ||
|
24165bb5bb | ||
|
4c8fdfa3b4 |
@ -1,3 +1,5 @@
|
||||
CXXFLAGS += -O3 -Wall
|
||||
|
||||
# Add .d to Make's recognized suffixes.
|
||||
SUFFIXES += .d
|
||||
|
||||
|
@ -169,8 +169,6 @@ std::string Board::to_fen() const {
|
||||
}
|
||||
|
||||
Board Board::make_move(Move move) const {
|
||||
int8_t dest_piece = this->squares[move.target_square];
|
||||
|
||||
Board ret;
|
||||
std::copy(
|
||||
std::begin(this->squares),
|
||||
@ -185,13 +183,89 @@ Board Board::make_move(Move move) const {
|
||||
|
||||
// -- Handle en passant target being eaten
|
||||
if (move.en_passant)
|
||||
ret.squares[move.target_square - 8] = Piece::None;
|
||||
ret.squares[move.target_square + (white_to_play ? -8 : 8)] =
|
||||
Piece::None;
|
||||
|
||||
// -- Handle promotion
|
||||
if (move.promoting_to != Piece::None)
|
||||
ret.squares[move.target_square] = move.promoting_to;
|
||||
|
||||
// -- Set en passant target if need
|
||||
if ((squares[move.source_square] & 0b111) == Piece::Pawn
|
||||
&& std::abs(move.target_square - move.source_square) == 16) {
|
||||
if (white_to_play)
|
||||
ret.en_passant_target = move.target_square - 8;
|
||||
else
|
||||
ret.en_passant_target = move.target_square + 8;
|
||||
} else {
|
||||
ret.en_passant_target = -1;
|
||||
}
|
||||
|
||||
// -- Handle castling (just move the rook over)
|
||||
Coords c = Coords::from_index(move.source_square);
|
||||
if (move.castle_side & KingSide) {
|
||||
Coords rook_source{7, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{5, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
} else if (move.castle_side & QueenSide) {
|
||||
Coords rook_source{0, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{3, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
}
|
||||
|
||||
// -- Check for castling rights
|
||||
ret.w_castle_rights = w_castle_rights;
|
||||
ret.b_castle_rights = b_castle_rights;
|
||||
if (white_to_play) {
|
||||
if ((squares[move.source_square] & 0b111) == King)
|
||||
ret.w_castle_rights = NeitherSide;
|
||||
|
||||
if ((squares[move.source_square] & 0b111) == Rook) {
|
||||
if (c.x == 0 && (ret.w_castle_rights & QueenSide))
|
||||
ret.w_castle_rights &= ~(QueenSide);
|
||||
if (c.x == 7 && (ret.w_castle_rights & KingSide))
|
||||
ret.w_castle_rights &= ~(KingSide);
|
||||
}
|
||||
|
||||
Coords target = Coords::from_index(move.target_square);
|
||||
if (move.is_capturing && target.y == 7
|
||||
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||
if (target.x == 0 && (ret.b_castle_rights & QueenSide))
|
||||
ret.b_castle_rights &= ~(QueenSide);
|
||||
if (target.x == 7 && (ret.b_castle_rights & KingSide))
|
||||
ret.b_castle_rights &= ~(KingSide);
|
||||
}
|
||||
} else {
|
||||
if ((squares[move.source_square] & 0b111) == King)
|
||||
ret.b_castle_rights = NeitherSide;
|
||||
|
||||
if ((squares[move.source_square] & 0b111) == Rook) {
|
||||
if (c.x == 0 && (ret.b_castle_rights & QueenSide))
|
||||
ret.b_castle_rights &= ~(QueenSide);
|
||||
if (c.x == 7 && (ret.b_castle_rights & KingSide))
|
||||
ret.b_castle_rights &= ~(KingSide);
|
||||
}
|
||||
|
||||
Coords target = Coords::from_index(move.target_square);
|
||||
if (move.is_capturing && target.y == 0
|
||||
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||
if (target.x == 0 && (ret.w_castle_rights & QueenSide))
|
||||
ret.w_castle_rights &= ~(QueenSide);
|
||||
if (target.x == 7 && (ret.w_castle_rights & KingSide))
|
||||
ret.w_castle_rights &= ~(KingSide);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int8_t Board::get_king_of(int8_t colour) const {
|
||||
for (int i = 0; i < 64; i++)
|
||||
if (this->squares[i] == (colour | Piece::King))
|
||||
if (squares[i] == (colour | Piece::King))
|
||||
return i;
|
||||
throw std::domain_error(
|
||||
"Apparently there no kings of the such color in this board"
|
||||
@ -208,13 +282,57 @@ std::vector<int8_t> to_target_square(std::vector<Move> moves) {
|
||||
bool Board::is_check_for(int8_t colour) const {
|
||||
int8_t king_idx = this->get_king_of(colour);
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (this->squares[i] == Piece::None)
|
||||
if (this->squares[i] == Piece::None || colour_at(i) == colour)
|
||||
continue;
|
||||
std::vector<Move> moves =
|
||||
legal_moves(this->squares[i], *this, Coords::from_index(i), true);
|
||||
std::vector<int8_t> targets = to_target_square(moves);
|
||||
if (std::find(targets.begin(), targets.end(), i) != targets.end())
|
||||
std::vector<int8_t> targets;
|
||||
if ((squares[i] & 0b00111) == King) {
|
||||
// special case for the king, because it creates infinite recursion
|
||||
// (since he looks if he's walking into a check)
|
||||
Coords king_pos = Coords::from_index(i);
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
Coords c{king_pos.x + dx, king_pos.y + dy};
|
||||
if (c.is_within_bounds())
|
||||
targets.push_back(c.to_index());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
std::vector<Move> moves = legal_moves(
|
||||
this->squares[i],
|
||||
*this,
|
||||
Coords::from_index(i),
|
||||
true
|
||||
);
|
||||
targets = to_target_square(moves);
|
||||
}
|
||||
if (std::find(targets.begin(), targets.end(), king_idx)
|
||||
!= targets.end())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Board::no_legal_moves_for(int8_t colour) const {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (colour_at(i) == colour) {
|
||||
std::vector<Move> moves =
|
||||
legal_moves(squares[i], *this, Coords::from_index(i));
|
||||
if (moves.size() > 0)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<Move> Board::all_legal_moves() const {
|
||||
std::vector<Move> ret;
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if ((colour_at(i) == White && white_to_play)
|
||||
|| (colour_at(i) == Black && !white_to_play)) {
|
||||
std::vector<Move> moves =
|
||||
legal_moves(squares[i], *this, Coords::from_index(i));
|
||||
ret.insert(ret.end(), moves.begin(), moves.end());
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -8,7 +8,8 @@
|
||||
|
||||
struct Board {
|
||||
private:
|
||||
int8_t get_king_of(int8_t colour) const;
|
||||
int8_t get_king_of(int8_t) const;
|
||||
bool no_legal_moves_for(int8_t) const;
|
||||
|
||||
public:
|
||||
int8_t squares[64] = {Piece::None};
|
||||
@ -25,6 +26,21 @@ struct Board {
|
||||
std::string to_fen() const;
|
||||
bool is_check_for(int8_t) const;
|
||||
|
||||
std::vector<Move> all_legal_moves() const;
|
||||
|
||||
bool is_checkmate_for(int8_t colour) const {
|
||||
return is_check_for(colour) && no_legal_moves_for(colour);
|
||||
}
|
||||
|
||||
bool is_stalemate_for(int8_t colour) const {
|
||||
return !is_check_for(colour) && no_legal_moves_for(colour);
|
||||
}
|
||||
|
||||
bool is_terminal() const {
|
||||
return is_checkmate_for(White) || is_checkmate_for(Black)
|
||||
|| is_stalemate_for(White) || is_stalemate_for(Black);
|
||||
}
|
||||
|
||||
int8_t colour_at(int8_t idx) const {
|
||||
return squares[idx] & 0b11000;
|
||||
}
|
||||
|
@ -18,6 +18,8 @@ struct Coords {
|
||||
}
|
||||
|
||||
static Coords from_index(int idx) {
|
||||
if (idx < 0 || idx > 63)
|
||||
throw std::invalid_argument("The index is outside the board...");
|
||||
return {idx % 8, idx / 8};
|
||||
}
|
||||
|
||||
@ -27,17 +29,17 @@ struct Coords {
|
||||
"An algebraic coordinate should only have two characters"
|
||||
);
|
||||
|
||||
int x = _FILES.find(pos[0]);
|
||||
size_t x = _FILES.find(pos[0]);
|
||||
if (x == std::string::npos)
|
||||
throw std::invalid_argument("The first character of the given "
|
||||
"algebraic coordinate is invalid");
|
||||
|
||||
int y = _RANKS.find(pos[1]);
|
||||
size_t y = _RANKS.find(pos[1]);
|
||||
if (y == std::string::npos)
|
||||
throw std::invalid_argument("The second character of the given "
|
||||
"algebraic coordinate is invalid");
|
||||
|
||||
return Coords{x, y};
|
||||
return Coords{(int) x, (int) y};
|
||||
}
|
||||
|
||||
std::string to_algebraic() const {
|
||||
@ -52,7 +54,7 @@ struct Coords {
|
||||
}
|
||||
|
||||
bool is_within_bounds() const {
|
||||
return this->to_index() < 64;
|
||||
return 0 <= x && x < 8 && 0 <= y && y < 8;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5";
|
||||
// Board b = Board::setup_fen_position(pos);
|
||||
perft();
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "castle_side.hpp"
|
||||
#include "coords.hpp"
|
||||
#include "pieces/piece.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
struct Move {
|
||||
int8_t source_square;
|
||||
@ -11,5 +14,35 @@ struct Move {
|
||||
bool is_capturing = false;
|
||||
CastleSide castle_side = CastleSide::NeitherSide;
|
||||
bool en_passant = false;
|
||||
int8_t promoting_to = 0;
|
||||
int8_t promoting_to = Piece::None;
|
||||
|
||||
std::string to_string() const {
|
||||
std::stringstream ss;
|
||||
ss << Coords::from_index(source_square)
|
||||
<< Coords::from_index(target_square);
|
||||
if (promoting_to != Piece::None) {
|
||||
switch (promoting_to & 0b00111) {
|
||||
case Queen:
|
||||
ss << 'q';
|
||||
break;
|
||||
case Bishop:
|
||||
ss << 'b';
|
||||
break;
|
||||
case Knigt:
|
||||
ss << 'n';
|
||||
break;
|
||||
case Rook:
|
||||
ss << 'r';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Move& m) {
|
||||
os << m.to_string();
|
||||
return os;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
||||
}
|
||||
|
||||
if (b.is_check_for(b.colour_at(xy)))
|
||||
return ret;
|
||||
return keep_only_blocking(ret, b);
|
||||
|
||||
// -- Castles
|
||||
int8_t castling_rights = b.colour_at(xy) == Colour::White
|
||||
|
@ -14,7 +14,18 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
int8_t capturable_piece = b.squares[left.to_index()];
|
||||
if (capturable_piece != 0) {
|
||||
if (my_colour != b.colour_at(left))
|
||||
ret.push_back(Move{xy.to_index(), left.to_index()});
|
||||
if ((my_colour == White && left.y == 7)
|
||||
|| (my_colour == Black && left.y == 0))
|
||||
|
||||
for (auto piece : {Rook, Knigt, Bishop, Queen})
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
left.to_index(),
|
||||
.is_capturing = true,
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
ret.push_back(Move{xy.to_index(), left.to_index()});
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,11 +36,36 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
int8_t capturable_piece = b.squares[right.to_index()];
|
||||
if (capturable_piece != 0) {
|
||||
if (my_colour != b.colour_at(right))
|
||||
ret.push_back(Move{xy.to_index(), right.to_index()});
|
||||
if ((my_colour == White && right.y == 7)
|
||||
|| (my_colour == Black && right.y == 0))
|
||||
|
||||
for (auto piece : {Rook, Knigt, Bishop, Queen})
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
right.to_index(),
|
||||
.is_capturing = true,
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
ret.push_back(Move{xy.to_index(), right.to_index()});
|
||||
}
|
||||
}
|
||||
|
||||
// -- Promotion
|
||||
// -- Capture en passant
|
||||
if (b.en_passant_target != -1) {
|
||||
Coords c = Coords::from_index(b.en_passant_target);
|
||||
int dy = my_colour == Colour::White ? 1 : -1;
|
||||
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1)) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
c.to_index(),
|
||||
.is_capturing = true,
|
||||
.en_passant = true
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// -- Normal move + promotion
|
||||
bool is_on_starting_rank =
|
||||
my_colour == Colour::White ? xy.y == 1 : xy.y == 6;
|
||||
int max_dy = is_on_starting_rank ? 3 : 2;
|
||||
@ -39,12 +75,17 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
if (b.squares[new_xy.to_index()] != Piece::None)
|
||||
break;
|
||||
if (new_xy.y == 7 || new_xy.y == 0)
|
||||
for (auto piece : {Queen, Knigt, Bishop, Rook})
|
||||
for (auto piece : {Rook, Knigt, Bishop, Queen})
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
new_xy.to_index(),
|
||||
.promoting_to = piece
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
new_xy.to_index(),
|
||||
});
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -18,14 +18,28 @@ keep_only_blocking(const std::vector<Move> candidates, const Board& board) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<Move> legal_moves(
|
||||
int8_t p, const Board& b, const Coords xy, bool looking_for_check = false
|
||||
) {
|
||||
std::vector<Move>
|
||||
legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) {
|
||||
std::vector<Move> ret;
|
||||
switch (p) {
|
||||
int8_t simple_piece = p & 0b00111;
|
||||
switch (simple_piece) {
|
||||
case Piece::Pawn:
|
||||
ret = pawn_moves(b, xy);
|
||||
break;
|
||||
case Piece::Bishop:
|
||||
ret = bishop_moves(b, xy);
|
||||
break;
|
||||
case Piece::Rook:
|
||||
ret = rook_moves(b, xy);
|
||||
break;
|
||||
case Piece::Knigt:
|
||||
ret = knight_moves(b, xy);
|
||||
break;
|
||||
case Piece::Queen:
|
||||
ret = queen_moves(b, xy);
|
||||
break;
|
||||
case Piece::King:
|
||||
ret = king_moves(b, xy);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -34,12 +48,12 @@ std::vector<Move> legal_moves(
|
||||
if (!looking_for_check)
|
||||
return keep_only_blocking(ret, b);
|
||||
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::optional<Move>
|
||||
move_for_position(const Board& board, const Coords source, const Coords dest) {
|
||||
if (dest.is_within_bounds())
|
||||
if (!dest.is_within_bounds())
|
||||
return {};
|
||||
|
||||
int8_t piece = board.squares[dest.to_index()];
|
||||
@ -56,7 +70,7 @@ move_for_position(const Board& board, const Coords source, const Coords dest) {
|
||||
std::vector<Move>
|
||||
look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
|
||||
std::vector<Move> ret;
|
||||
for (int d = 0; d < 8; d++) {
|
||||
for (int d = 1; d < 8; d++) {
|
||||
int dx = mult_dx * d;
|
||||
int dy = mult_dy * d;
|
||||
|
||||
@ -66,6 +80,8 @@ look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
|
||||
ret.push_back(move.value());
|
||||
if (move.value().is_capturing)
|
||||
break;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
|
@ -1,19 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "../move.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
enum Piece : int8_t {
|
||||
None = 0,
|
||||
King = 1,
|
||||
Pawn = 2,
|
||||
Knigt = 3,
|
||||
Bishop = 4,
|
||||
Rook = 5,
|
||||
Queen = 6,
|
||||
Rook = 1,
|
||||
Knigt = 2,
|
||||
Bishop = 3,
|
||||
Queen = 4,
|
||||
King = 5,
|
||||
Pawn = 6,
|
||||
};
|
||||
|
||||
enum Colour : int8_t {
|
||||
@ -21,10 +20,16 @@ enum Colour : int8_t {
|
||||
Black = 16,
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const int8_t& i) {
|
||||
os << std::to_string(i);
|
||||
return os;
|
||||
}
|
||||
|
||||
class Board;
|
||||
struct Coords;
|
||||
struct Move;
|
||||
|
||||
std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool);
|
||||
std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool = false);
|
||||
std::vector<Move> keep_only_blocking(const std::vector<Move>, const Board&);
|
||||
std::optional<Move> move_for_position(const Board&, const Coords, const Coords);
|
||||
std::vector<Move> look_direction(const Board&, const Coords, int, int);
|
||||
|
@ -0,0 +1,164 @@
|
||||
#include "stickfosh.hpp"
|
||||
|
||||
#include "board.hpp"
|
||||
#include "move.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#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}, // 0
|
||||
{2, 400}, // 1
|
||||
{3, 8902}, // 38
|
||||
{4, 197281}, // 971
|
||||
// {5, 4865609}, // 23032
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 2
|
||||
{
|
||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 2",
|
||||
{
|
||||
{1, 48}, // 0
|
||||
{2, 2039}, // 16
|
||||
{3, 97862}, // 602
|
||||
// {4, 4085603}, // 26612
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 3
|
||||
{
|
||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 3",
|
||||
{
|
||||
{1, 14}, // 0
|
||||
{2, 191}, // 1
|
||||
{3, 2812}, // 11
|
||||
{4, 43238}, // 157
|
||||
{5, 674624}, // 2199
|
||||
// {6, 11030083},
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 4a
|
||||
{
|
||||
"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 4",
|
||||
{
|
||||
{1, 6}, // 0
|
||||
{2, 264}, // 1
|
||||
{3, 9467}, // 69
|
||||
{4, 422333}, // 3085
|
||||
// {5, 15833292}, // 124452
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 4b
|
||||
{
|
||||
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5",
|
||||
{
|
||||
{1, 6}, // 0
|
||||
{2, 264}, // 2
|
||||
{3, 9467}, // 104
|
||||
{4, 422333}, // 3742
|
||||
// {5, 15833292}, // 136784
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 5
|
||||
{
|
||||
"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 6",
|
||||
{
|
||||
{1, 44}, // 0
|
||||
{2, 1486}, // 12
|
||||
{3, 62379}, // 357
|
||||
// {4, 2103487}, // 13804
|
||||
// {5, 89941194}, // 1230428
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 6
|
||||
{
|
||||
"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 "
|
||||
"7",
|
||||
{
|
||||
{1, 46}, // 0
|
||||
{2, 2079}, // 16
|
||||
{3, 89890}, // 602
|
||||
// {4, 3894594}, // 26612
|
||||
// {5, 164075551}, // 1230428
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static std::stringstream res;
|
||||
|
||||
int move_generation_test(Board& b, int depth, int max_depth) {
|
||||
if (depth == max_depth) {
|
||||
res.str("");
|
||||
res.clear();
|
||||
}
|
||||
|
||||
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);
|
||||
// std::cout << ">" << move << std::endl;
|
||||
int n = move_generation_test(tmp_board, depth - 1, max_depth);
|
||||
// std::cout << "<" << move << std::endl;
|
||||
if (depth == max_depth)
|
||||
res << move << ": " << n << std::endl;
|
||||
num_pos += n;
|
||||
}
|
||||
return num_pos;
|
||||
}
|
||||
|
||||
void perft() {
|
||||
for (auto& [key, value] : pos2expected)
|
||||
perft(key);
|
||||
}
|
||||
|
||||
void perft(std::string pos) {
|
||||
std::cout << pos << std::endl;
|
||||
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 << " " << std::flush;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
int moves = move_generation_test(b, depth, depth);
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
auto elapsed =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
|
||||
.count();
|
||||
|
||||
std::cout << "Results: " << moves << " ";
|
||||
if (moves == expected_n_moves)
|
||||
std::cout << tick << " ";
|
||||
else
|
||||
std::cout << cross << " (expected " << expected_n_moves << ") ";
|
||||
std::cout << "positions Time: " << elapsed << " milliseconds"
|
||||
<< std::endl;
|
||||
if (moves != expected_n_moves) {
|
||||
std::cout << std::endl << res.str();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
6
cpp/src/stickfosh.hpp
Normal file
6
cpp/src/stickfosh.hpp
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
void perft(std::string pos);
|
||||
void perft();
|
Loading…
x
Reference in New Issue
Block a user