Compare commits

..

No commits in common. "c55645ad7307473daa7c75d274d0b52cab95f7e7" and "84c3af2bb13e8de1b3634548694bee3c59231989" have entirely different histories.

12 changed files with 40 additions and 444 deletions

View File

@ -1,5 +1,3 @@
CXXFLAGS += -O3 -Wall
# Add .d to Make's recognized suffixes. # Add .d to Make's recognized suffixes.
SUFFIXES += .d SUFFIXES += .d

View File

@ -169,6 +169,8 @@ std::string Board::to_fen() const {
} }
Board Board::make_move(Move move) const { Board Board::make_move(Move move) const {
int8_t dest_piece = this->squares[move.target_square];
Board ret; Board ret;
std::copy( std::copy(
std::begin(this->squares), std::begin(this->squares),
@ -183,89 +185,13 @@ Board Board::make_move(Move move) const {
// -- Handle en passant target being eaten // -- Handle en passant target being eaten
if (move.en_passant) if (move.en_passant)
ret.squares[move.target_square + (white_to_play ? -8 : 8)] = ret.squares[move.target_square - 8] = Piece::None;
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; return ret;
} }
int8_t Board::get_king_of(int8_t colour) const { int8_t Board::get_king_of(int8_t colour) const {
for (int i = 0; i < 64; i++) for (int i = 0; i < 64; i++)
if (squares[i] == (colour | Piece::King)) if (this->squares[i] == (colour | Piece::King))
return i; return i;
throw std::domain_error( throw std::domain_error(
"Apparently there no kings of the such color in this board" "Apparently there no kings of the such color in this board"
@ -282,57 +208,13 @@ std::vector<int8_t> to_target_square(std::vector<Move> moves) {
bool Board::is_check_for(int8_t colour) const { bool Board::is_check_for(int8_t colour) const {
int8_t king_idx = this->get_king_of(colour); int8_t king_idx = this->get_king_of(colour);
for (int i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {
if (this->squares[i] == Piece::None || colour_at(i) == colour) if (this->squares[i] == Piece::None)
continue; continue;
std::vector<int8_t> targets; std::vector<Move> moves =
if ((squares[i] & 0b00111) == King) { legal_moves(this->squares[i], *this, Coords::from_index(i), true);
// special case for the king, because it creates infinite recursion std::vector<int8_t> targets = to_target_square(moves);
// (since he looks if he's walking into a check) if (std::find(targets.begin(), targets.end(), i) != targets.end())
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 true;
} }
return false; 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;
}

View File

@ -8,8 +8,7 @@
struct Board { struct Board {
private: private:
int8_t get_king_of(int8_t) const; int8_t get_king_of(int8_t colour) const;
bool no_legal_moves_for(int8_t) const;
public: public:
int8_t squares[64] = {Piece::None}; int8_t squares[64] = {Piece::None};
@ -26,21 +25,6 @@ struct Board {
std::string to_fen() const; std::string to_fen() const;
bool is_check_for(int8_t) 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 { int8_t colour_at(int8_t idx) const {
return squares[idx] & 0b11000; return squares[idx] & 0b11000;
} }

View File

@ -18,8 +18,6 @@ struct Coords {
} }
static Coords from_index(int idx) { 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}; return {idx % 8, idx / 8};
} }
@ -29,17 +27,17 @@ struct Coords {
"An algebraic coordinate should only have two characters" "An algebraic coordinate should only have two characters"
); );
size_t x = _FILES.find(pos[0]); int x = _FILES.find(pos[0]);
if (x == std::string::npos) if (x == std::string::npos)
throw std::invalid_argument("The first character of the given " throw std::invalid_argument("The first character of the given "
"algebraic coordinate is invalid"); "algebraic coordinate is invalid");
size_t y = _RANKS.find(pos[1]); int y = _RANKS.find(pos[1]);
if (y == std::string::npos) if (y == std::string::npos)
throw std::invalid_argument("The second character of the given " throw std::invalid_argument("The second character of the given "
"algebraic coordinate is invalid"); "algebraic coordinate is invalid");
return Coords{(int) x, (int) y}; return Coords{x, y};
} }
std::string to_algebraic() const { std::string to_algebraic() const {
@ -54,7 +52,7 @@ struct Coords {
} }
bool is_within_bounds() const { bool is_within_bounds() const {
return 0 <= x && x < 8 && 0 <= y && y < 8; return this->to_index() < 64;
} }
}; };

View File

@ -1,12 +1,11 @@
#include "board.hpp" #include "board.hpp"
#include "stickfosh.hpp"
#include <iostream> #include <iostream>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::string pos = std::string pos =
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5"; "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
// Board b = Board::setup_fen_position(pos); Board b = Board::setup_fen_position(pos);
perft(); sizeof(b);
return 0; return 0;
} }

View File

@ -1,11 +1,8 @@
#pragma once #pragma once
#include "castle_side.hpp" #include "castle_side.hpp"
#include "coords.hpp"
#include "pieces/piece.hpp"
#include <cstdint> #include <cstdint>
#include <sstream>
struct Move { struct Move {
int8_t source_square; int8_t source_square;
@ -14,35 +11,5 @@ struct Move {
bool is_capturing = false; bool is_capturing = false;
CastleSide castle_side = CastleSide::NeitherSide; CastleSide castle_side = CastleSide::NeitherSide;
bool en_passant = false; bool en_passant = false;
int8_t promoting_to = Piece::None; int8_t promoting_to = 0;
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;
}

View File

@ -46,7 +46,7 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
} }
if (b.is_check_for(b.colour_at(xy))) if (b.is_check_for(b.colour_at(xy)))
return keep_only_blocking(ret, b); return ret;
// -- Castles // -- Castles
int8_t castling_rights = b.colour_at(xy) == Colour::White int8_t castling_rights = b.colour_at(xy) == Colour::White

View File

@ -14,18 +14,7 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
int8_t capturable_piece = b.squares[left.to_index()]; int8_t capturable_piece = b.squares[left.to_index()];
if (capturable_piece != 0) { if (capturable_piece != 0) {
if (my_colour != b.colour_at(left)) if (my_colour != b.colour_at(left))
if ((my_colour == White && left.y == 7) ret.push_back(Move{xy.to_index(), left.to_index()});
|| (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()});
} }
} }
@ -36,36 +25,11 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
int8_t capturable_piece = b.squares[right.to_index()]; int8_t capturable_piece = b.squares[right.to_index()];
if (capturable_piece != 0) { if (capturable_piece != 0) {
if (my_colour != b.colour_at(right)) if (my_colour != b.colour_at(right))
if ((my_colour == White && right.y == 7) ret.push_back(Move{xy.to_index(), right.to_index()});
|| (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()});
} }
} }
// -- Capture en passant // -- Promotion
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 = bool is_on_starting_rank =
my_colour == Colour::White ? xy.y == 1 : xy.y == 6; my_colour == Colour::White ? xy.y == 1 : xy.y == 6;
int max_dy = is_on_starting_rank ? 3 : 2; int max_dy = is_on_starting_rank ? 3 : 2;
@ -75,17 +39,12 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
if (b.squares[new_xy.to_index()] != Piece::None) if (b.squares[new_xy.to_index()] != Piece::None)
break; break;
if (new_xy.y == 7 || new_xy.y == 0) if (new_xy.y == 7 || new_xy.y == 0)
for (auto piece : {Rook, Knigt, Bishop, Queen}) for (auto piece : {Queen, Knigt, Bishop, Rook})
ret.push_back(Move{ ret.push_back(Move{
xy.to_index(), xy.to_index(),
new_xy.to_index(), new_xy.to_index(),
.promoting_to = (int8_t) (my_colour | piece) .promoting_to = piece
}); });
else
ret.push_back(Move{
xy.to_index(),
new_xy.to_index(),
});
} }
return ret; return ret;

View File

@ -18,28 +18,14 @@ keep_only_blocking(const std::vector<Move> candidates, const Board& board) {
return ret; return ret;
} }
std::vector<Move> std::vector<Move> legal_moves(
legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) { int8_t p, const Board& b, const Coords xy, bool looking_for_check = false
) {
std::vector<Move> ret; std::vector<Move> ret;
int8_t simple_piece = p & 0b00111; switch (p) {
switch (simple_piece) {
case Piece::Pawn: case Piece::Pawn:
ret = pawn_moves(b, xy); ret = pawn_moves(b, xy);
break;
case Piece::Bishop: 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; break;
default: default:
break; break;
@ -48,12 +34,12 @@ legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) {
if (!looking_for_check) if (!looking_for_check)
return keep_only_blocking(ret, b); return keep_only_blocking(ret, b);
return ret; return {};
} }
std::optional<Move> std::optional<Move>
move_for_position(const Board& board, const Coords source, const Coords dest) { move_for_position(const Board& board, const Coords source, const Coords dest) {
if (!dest.is_within_bounds()) if (dest.is_within_bounds())
return {}; return {};
int8_t piece = board.squares[dest.to_index()]; int8_t piece = board.squares[dest.to_index()];
@ -70,7 +56,7 @@ move_for_position(const Board& board, const Coords source, const Coords dest) {
std::vector<Move> std::vector<Move>
look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) { look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
std::vector<Move> ret; std::vector<Move> ret;
for (int d = 1; d < 8; d++) { for (int d = 0; d < 8; d++) {
int dx = mult_dx * d; int dx = mult_dx * d;
int dy = mult_dy * d; int dy = mult_dy * d;
@ -80,8 +66,6 @@ look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
ret.push_back(move.value()); ret.push_back(move.value());
if (move.value().is_capturing) if (move.value().is_capturing)
break; break;
} else {
break;
} }
} }
return ret; return ret;

View File

@ -1,18 +1,19 @@
#pragma once #pragma once
#include "../move.hpp"
#include <cstdint> #include <cstdint>
#include <optional> #include <optional>
#include <ostream>
#include <vector> #include <vector>
enum Piece : int8_t { enum Piece : int8_t {
None = 0, None = 0,
Rook = 1, King = 1,
Knigt = 2, Pawn = 2,
Bishop = 3, Knigt = 3,
Queen = 4, Bishop = 4,
King = 5, Rook = 5,
Pawn = 6, Queen = 6,
}; };
enum Colour : int8_t { enum Colour : int8_t {
@ -20,16 +21,10 @@ enum Colour : int8_t {
Black = 16, Black = 16,
}; };
inline std::ostream& operator<<(std::ostream& os, const int8_t& i) {
os << std::to_string(i);
return os;
}
class Board; class Board;
struct Coords; struct Coords;
struct Move;
std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool = false); std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool);
std::vector<Move> keep_only_blocking(const std::vector<Move>, const Board&); 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::optional<Move> move_for_position(const Board&, const Coords, const Coords);
std::vector<Move> look_direction(const Board&, const Coords, int, int); std::vector<Move> look_direction(const Board&, const Coords, int, int);

View File

@ -1,164 +0,0 @@
#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);
}
}
}

View File

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