Removed the python version and kept just the c++ one
Some checks failed
pre-release / Pre Release (push) Waiting to run
tagged-release / Tagged Release (push) Has been cancelled

This commit is contained in:
Karma Riuk
2025-02-16 09:36:04 +01:00
parent 0145664567
commit 5cbe57dc55
91 changed files with 29 additions and 1531 deletions

View File

@ -0,0 +1,38 @@
#include "ai_vs_ai.hpp"
#include "controller.hpp"
#include <thread>
AIvsAIController::AIvsAIController(Board b, View& v, ai::AI& p1, ai::AI& p2)
: Controller(b, v),
p1(p1),
p2(p2) {}
void AIvsAIController::start() {
std::thread view_thread([&]() { view.show(); });
ai::AI* current_player;
while (!board.is_terminal()) {
current_player = board.white_to_play ? &p1 : &p2;
std::cout << typeid(*current_player).name() << " to play" << std::endl;
Move move = current_player->search(board);
make_move(move);
}
view_thread.join();
}
void AIvsAIController::make_move(Move move) {
board = board.make_move(move);
std::cout << "Made move: " << move << std::endl;
view.update_board(board, -1, {});
Colour current_colour = board.white_to_play ? White : Black;
if (board.is_checkmate())
view.notify_checkmate(current_colour);
if (board.is_stalemate())
view.notify_stalemate(current_colour);
}
void AIvsAIController::on_tile_selected(int, int) {}

View File

@ -0,0 +1,19 @@
#pragma once
#include "../model/ais/ai.hpp"
#include "../model/utils/coords.hpp"
#include "../model/utils/move.hpp"
#include "../view/view.hpp"
#include "controller.hpp"
class AIvsAIController : public Controller {
ai::AI &p1, &p2;
protected:
void make_move(Move) override;
public:
AIvsAIController(Board, View&, ai::AI&, ai::AI&);
void on_tile_selected(int, int) override;
void start() override;
};

View File

@ -0,0 +1,7 @@
#include "controller.hpp"
#include "../view/view.hpp"
Controller::Controller(Board b, View& v): board(b), view(v) {
v.set_controller(this);
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "../model/board/board.hpp"
class View;
class Controller {
protected:
Board board;
View& view;
virtual void make_move(Move) = 0;
public:
Controller(Board, View&);
virtual void start() = 0;
virtual void on_tile_selected(int, int) = 0;
};

View File

@ -0,0 +1,29 @@
#include "human_vs_ai.hpp"
#include <algorithm>
HumanVsAIController::HumanVsAIController(Board b, View& v, ai::AI& ai)
: ManualController(b, v),
ai(ai) {}
void HumanVsAIController::on_tile_selected(int x, int y) {
Coords c{x, y};
Piece piece = board.piece_at(c);
if (selected_index == -1
|| (piece != Piece::None && piece != selected_piece
&& (piece & 0b11000) != (selected_piece & 0b11000))) {
show_legal_moves(c);
} else {
auto boh = std::find(targets.begin(), targets.end(), c.to_index());
if (boh != targets.end()) {
make_move(Move{selected_index, c.to_index()});
if (board.is_terminal())
return;
Move ai_move = ai.search(board);
make_move(ai_move);
} else
reset_selection();
}
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "../model/ais/ai.hpp"
#include "../view/view.hpp"
#include "manual.hpp"
class HumanVsAIController : public ManualController {
protected:
ai::AI& ai;
public:
HumanVsAIController(Board, View&, ai::AI&);
void on_tile_selected(int, int) override;
};

74
src/controller/manual.cpp Normal file
View File

@ -0,0 +1,74 @@
#include "manual.hpp"
#include "../model/utils/utils.hpp"
#include <algorithm>
ManualController::ManualController(Board b, View& v): Controller(b, v) {
reset_selection();
}
void ManualController::start() {
reset_selection();
view.show();
}
void ManualController::on_tile_selected(int x, int y) {
Coords c{x, y};
Piece piece = board.piece_at(c);
if (selected_index == -1
|| (piece != Piece::None && piece != selected_piece
&& (piece & 0b11000) != (selected_piece & 0b11000))) {
show_legal_moves(c);
} else {
auto boh = std::find(targets.begin(), targets.end(), c.to_index());
if (boh != targets.end())
make_move(Move{selected_index, c.to_index()});
else
reset_selection();
}
}
void ManualController::reset_selection() {
selected_index = -1;
selected_piece = Piece::None;
targets = {};
view.update_board(board, selected_index, targets);
}
void ManualController::show_legal_moves(Coords xy) {
Colour colour = board.colour_at(xy);
if (colour) {
Colour to_play = board.white_to_play ? White : Black;
if (colour != to_play)
return;
selected_index = xy.to_index();
selected_piece = board.piece_at(xy);
targets = to_target_square(legal_moves(selected_piece, board, xy));
view.update_board(board, xy.to_index(), targets);
}
}
void ManualController::make_move(Move move) {
// handle promotion before making the move
Colour colour = board.white_to_play ? White : Black;
Coords source = Coords::from_index(move.source_square);
if (board.piece_at(move.source_square) == Piece::Pawn
&& board.colour_at(move.source_square) == White && source.y == 6) {
Piece promotion_piece = (Piece) (colour | view.ask_about_promotion());
move.promoting_to = promotion_piece;
}
std::cout << "Move made: " << move << std::endl;
board = board.make_move(move);
reset_selection();
Colour current_colour = board.white_to_play ? White : Black;
if (board.is_checkmate())
view.notify_checkmate(current_colour);
if (board.is_stalemate())
view.notify_stalemate(current_colour);
}

22
src/controller/manual.hpp Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include "../model/utils/coords.hpp"
#include "../model/utils/move.hpp"
#include "../view/view.hpp"
#include "controller.hpp"
class ManualController : public Controller {
protected:
int8_t selected_index;
Piece selected_piece;
std::vector<int8_t> targets;
void reset_selection();
void show_legal_moves(Coords);
void make_move(Move) override;
public:
ManualController(Board, View&);
void on_tile_selected(int, int) override;
void start() override;
};

41
src/main.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "controller/ai_vs_ai.hpp"
#include "controller/controller.hpp"
#include "controller/human_vs_ai.hpp"
#include "controller/manual.hpp"
#include "model/ais/ai.hpp"
#include "model/perft/perft.hpp"
#include "view/gui.hpp"
#include "view/noop.hpp"
#include "view/view.hpp"
#include <chrono>
int main(int argc, char* argv[]) {
// std::string pos =
// "r2qkb1r/2p1pppp/p1n1b3/1p6/B2P4/2P1P3/P4PPP/R1BQK1NR w KQkq - 0 9 ";
// std::string pos = "8/6K1/5P2/8/1k6/8/8/8 w - - 0 1";
// pos for ai timing<
std::string pos =
"r3k2r/p1ppqpb1/Bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPB1PPP/R3K2R b KQkq - 0 3";
Board b = Board::setup_fen_position(pos);
ai::v0_random p1(true, std::chrono::milliseconds(1000));
// ai::v1_pure_minimax p2(false, std::chrono::milliseconds(20000));
// ai::v2_alpha_beta p2(false, std::chrono::milliseconds(20000));
ai::v3_AB_ordering p2(false, std::chrono::milliseconds(20000));
// ai::v4_search_captures p2(false, std::chrono::milliseconds(20000));
// GUI gui;
NoOpView gui;
AIvsAIController manual(b, gui, p1, p2);
// HumanVsAIController manual(b, gui, p2);
Controller& controller = manual;
controller.start();
// perft();
return 0;
}

52
src/model/ais/ai.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "ai.hpp"
#include <condition_variable>
#include <ostream>
#include <thread>
Move ai::AI::search(const Board& b) {
Move result;
std::condition_variable cv;
std::mutex cv_mutex;
double elapsed;
stop_computation = false;
// Start computation in a separate thread
std::thread computation_thread([&]() {
auto start = std::chrono::steady_clock::now();
result = _search(b);
auto end = std::chrono::steady_clock::now();
elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count();
// Notify the timer thread that computation is done
{
std::lock_guard<std::mutex> lock(cv_mutex);
stop_computation = true;
cv.notify_one();
}
});
// Start a timer thread to stop computation after given time
std::thread timer_thread([&]() {
std::unique_lock<std::mutex> lock(cv_mutex);
if (!cv.wait_for(lock, thinking_time, [&] {
return stop_computation.load();
})) {
// Timeout reached; set stop flag
stop_computation = true;
}
});
// Wait for computation thread to finish
computation_thread.join();
// Ensure timer thread is also stopped
timer_thread.join();
std::cout << "Took " << elapsed << " ms" << std::endl;
return result;
}

79
src/model/ais/ai.hpp Normal file
View File

@ -0,0 +1,79 @@
#pragma once
#include "../board/board.hpp"
#include <atomic>
#include <chrono>
namespace ai {
class AI {
protected:
bool am_white;
std::chrono::milliseconds thinking_time;
virtual Move _search(const Board&) = 0;
public:
AI(bool am_white, std::chrono::milliseconds tt)
: am_white(am_white),
thinking_time(tt) {}
std::atomic<bool> stop_computation = false;
Move search(const Board& b);
virtual int eval(const Board&) = 0;
};
struct v0_random : public AI {
v0_random(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
Move _search(const Board&) override;
int eval(const Board&) override {
return 0;
};
};
class v1_pure_minimax : public AI { // looks two moves ahead
int _search(const Board&, int);
public:
v1_pure_minimax(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
Move _search(const Board&) override;
int eval(const Board&) override;
};
class v2_alpha_beta : public AI {
// looks two moves ahead, with alpha-beta pruning (no move ordering)
int _search(const Board&, int, int, int);
public:
v2_alpha_beta(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
Move _search(const Board&) override;
int eval(const Board&) override;
};
class v3_AB_ordering : public AI {
// looks two moves ahead, with alpha-beta pruning, with move ordering
virtual int _search(const Board&, int, int, int);
public:
v3_AB_ordering(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
Move _search(const Board&) override;
int eval(const Board&) override;
};
class v4_search_captures : public v3_AB_ordering {
// same as v3, but looking at only at captures when leaf is reached,
// until no captures are left
int _search(const Board&, int, int, int) override;
int _search_captures(const Board&, int, int);
public:
v4_search_captures(bool w, std::chrono::milliseconds tt)
: v3_AB_ordering(w, tt) {}
};
} // namespace ai

View File

@ -0,0 +1,13 @@
#include "ai.hpp"
#include <thread>
Move ai::v0_random::_search(const Board& b) {
std::vector<Move> moves = b.all_legal_moves();
std::this_thread::sleep_for(std::chrono::milliseconds(thinking_time)
); // Simulate work
return moves[rand() % moves.size()];
}

View File

@ -0,0 +1,91 @@
#include "../pieces/piece.hpp"
#include "../utils/threadpool.hpp"
#include "../utils/utils.hpp"
#include "ai.hpp"
#include <map>
#define MULTITHREADED 1
static int position_counter = 0;
Move ai::v1_pure_minimax::_search(const Board& b) {
position_counter = 0;
std::vector<Move> moves = b.all_legal_moves();
Move best_move;
int best_eval = -INFINITY;
#if MULTITHREADED
ThreadPool pool(std::thread::hardware_concurrency());
std::cout << "Have to look at " << moves.size() << " moves" << std::endl;
std::map<Move, std::future<int>> futures;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
futures.insert({move, pool.enqueue([&, tmp_board]() {
return _search(tmp_board, 3);
})});
}
int counter = 0;
for (auto& [move, future] : futures) {
int eval = future.get();
counter++;
if (!am_white)
eval *= -1;
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
#else
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
std::cout << "Looking at " << move << std::endl;
int eval = _search(tmp_board, 3);
if (!am_white)
eval *= -1;
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
#endif
std::cout << "Looked at " << position_counter << " positions" << std::endl;
return best_move;
}
int ai::v1_pure_minimax::_search(const Board& b, int depth) {
if (depth == 0 || stop_computation)
return eval(b);
if (b.no_legal_moves()) {
if (b.is_check())
return -INFINITY;
return 0;
}
std::vector<Move> moves = b.all_legal_moves();
int best_evaluation = -INFINITY;
Move best_move;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int tmp_eval = -_search(tmp_board, depth - 1);
best_evaluation = std::max(best_evaluation, tmp_eval);
}
return best_evaluation;
}
int ai::v1_pure_minimax::eval(const Board& b) {
position_counter++;
int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black);
int evaluation = white_eval - black_eval;
int perspective = b.white_to_play ? 1 : -1;
return perspective * evaluation;
}

View File

@ -0,0 +1,92 @@
#include "../pieces/piece.hpp"
#include "../utils/threadpool.hpp"
#include "../utils/utils.hpp"
#include "ai.hpp"
#include <map>
#define MULTITHREADED 1
static int position_counter = 0;
Move ai::v2_alpha_beta::_search(const Board& b) {
position_counter = 0;
std::vector<Move> moves = b.all_legal_moves();
Move best_move;
int best_eval = -INFINITY;
#if MULTITHREADED
ThreadPool pool(std::thread::hardware_concurrency());
std::cout << "Have to look at " << moves.size() << " moves" << std::endl;
std::map<Move, std::future<int>> futures;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
futures.insert({move, pool.enqueue([&, tmp_board]() {
return _search(tmp_board, 3, -INFINITY, INFINITY);
})});
}
int counter = 0;
for (auto& [move, future] : futures) {
int eval = future.get();
counter++;
if (!am_white)
eval *= -1;
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
#else
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
std::cout << "Looking at " << move << std::endl;
int eval = _search(tmp_board, 3);
if (!am_white)
eval *= -1;
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
#endif
std::cout << "Looked at " << position_counter << " positions" << std::endl;
return best_move;
}
int ai::v2_alpha_beta::_search(const Board& b, int depth, int alpha, int beta) {
if (depth == 0 || stop_computation)
return eval(b);
if (b.no_legal_moves()) {
if (b.is_check())
return -INFINITY;
return 0;
}
std::vector<Move> moves = b.all_legal_moves();
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int tmp_eval = -_search(tmp_board, depth - 1, -beta, -alpha);
if (tmp_eval >= beta)
return beta;
alpha = std::max(alpha, tmp_eval);
}
return alpha;
}
int ai::v2_alpha_beta::eval(const Board& b) {
position_counter++;
int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black);
int evaluation = white_eval - black_eval;
int perspective = b.white_to_play ? 1 : -1;
return perspective * evaluation;
}

View File

@ -0,0 +1,99 @@
#include "../pieces/piece.hpp"
#include "../utils/threadpool.hpp"
#include "../utils/utils.hpp"
#include "ai.hpp"
#include <algorithm>
#include <map>
#define MULTITHREADED 1
static int position_counter;
Move ai::v3_AB_ordering::_search(const Board& b) {
position_counter = 0;
std::vector<Move> moves = b.all_legal_moves();
Move best_move;
int best_eval = -INFINITY;
#if MULTITHREADED
ThreadPool pool(std::thread::hardware_concurrency());
std::cout << "Have to look at " << moves.size() << " moves" << std::endl;
std::map<Move, std::future<int>> futures;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
futures.insert({move, pool.enqueue([&, tmp_board]() {
return _search(tmp_board, 3, -INFINITY, INFINITY);
})});
}
int counter = 0;
for (auto& [move, future] : futures) {
int eval = future.get();
counter++;
if (!am_white)
eval *= -1;
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
#else
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
std::cout << "Looking at " << move << std::endl;
int eval = _search(tmp_board, 3);
if (!am_white)
eval *= -1;
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
#endif
std::cout << "Looked at " << position_counter << " positions" << std::endl;
return best_move;
}
int ai::v3_AB_ordering::_search(
const Board& b, int depth, int alpha, int beta
) {
if (depth == 0 || stop_computation)
return eval(b);
if (b.no_legal_moves()) {
if (b.is_check())
return -INFINITY;
return 0;
}
std::vector<Move> moves = b.all_legal_moves();
std::sort(moves.begin(), moves.end(), [&](Move& m1, Move& m2) {
return m1.score_guess(b) > m2.score_guess(b);
});
Move best_move;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int tmp_eval = -_search(tmp_board, depth - 1, -beta, -alpha);
if (tmp_eval >= beta)
return beta;
alpha = std::max(alpha, tmp_eval);
}
return alpha;
}
int ai::v3_AB_ordering::eval(const Board& b) {
position_counter++;
int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black);
int evaluation = white_eval - black_eval;
int perspective = b.white_to_play ? 1 : -1;
return perspective * evaluation;
}

View File

@ -0,0 +1,61 @@
#include "../pieces/piece.hpp"
#include "../utils/utils.hpp"
#include "ai.hpp"
#include <algorithm>
#define MULTITHREADED 1
static int position_counter;
int ai::v4_search_captures::_search(
const Board& b, int depth, int alpha, int beta
) {
if (depth == 0 || stop_computation)
return _search_captures(b, alpha, beta);
if (b.no_legal_moves()) {
if (b.is_check())
return -INFINITY;
return 0;
}
std::vector<Move> moves = b.all_legal_moves();
std::sort(moves.begin(), moves.end(), [&](Move& m1, Move& m2) {
return m1.score_guess(b) > m2.score_guess(b);
});
Move best_move;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int tmp_eval = -_search(tmp_board, depth - 1, -beta, -alpha);
if (tmp_eval >= beta)
return beta;
alpha = std::max(alpha, tmp_eval);
}
return alpha;
}
int ai::v4_search_captures::_search_captures(
const Board& b, int alpha, int beta
) {
int evaluation = eval(b);
if (evaluation >= beta)
return beta;
alpha = std::max(evaluation, alpha);
std::vector<Move> moves = b.all_capturing_moves();
std::sort(moves.begin(), moves.end(), [&](Move& m1, Move& m2) {
return m1.score_guess(b) > m2.score_guess(b);
});
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int tmp_eval = -_search_captures(tmp_board, -beta, -alpha);
if (tmp_eval >= beta)
return beta;
alpha = std::max(alpha, tmp_eval);
}
return alpha;
}

437
src/model/board/board.cpp Normal file
View File

@ -0,0 +1,437 @@
#include "board.hpp"
#include "../pieces/piece.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
#include "../utils/utils.hpp"
#include <SFML/Graphics/BlendMode.hpp>
#include <algorithm>
#include <cctype>
#include <map>
#include <sstream>
#include <stdexcept>
Board Board::setup_fen_position(std::string fen) {
Board board;
std::map<char, Piece> c2p{
{'k', Piece::King},
{'p', Piece::Pawn},
{'n', Piece::Knigt},
{'b', Piece::Bishop},
{'r', Piece::Rook},
{'q', Piece::Queen},
};
// -- Pieces
std::string fen_board = fen.substr(0, fen.find(' '));
int rank = 7, file = 0;
for (char symbol : fen_board) {
if (symbol == '/') {
file = 0;
rank--;
continue;
}
if (std::isdigit(symbol))
file += symbol - '0';
else {
Colour colour =
std::isupper(symbol) ? Colour::White : Colour::Black;
Piece piece = c2p[std::tolower(symbol)];
board.squares[rank * 8 + file] = colour | piece;
file++;
}
}
// -- Active colour
int index = fen.find(' ');
index++;
board.white_to_play = fen[index] == 'w';
// Castling Rights
index += 2;
for (char symbol : fen.substr(index, fen.find(' ', index + 1))) {
index++;
if (symbol == ' ' || symbol == '-') {
if (symbol == '-')
index++;
break;
}
switch (symbol) {
case 'K':
board.w_castle_rights |= CastleSide::KingSide;
break;
case 'Q':
board.w_castle_rights |= CastleSide::QueenSide;
break;
case 'k':
board.b_castle_rights |= CastleSide::KingSide;
break;
case 'q':
board.b_castle_rights |= CastleSide::QueenSide;
break;
}
}
// -- En passant target
if (fen[index] != '-') {
Coords c = Coords::from_algebraic(fen.substr(index, 2));
index += 2;
board.en_passant_target = c.to_index();
} else {
index++;
}
// -- Half move clock
index = fen.find(' ', index) + 1;
board.n_half_moves =
std::stoi(fen.substr(index, fen.find(' ', index + 1) - index));
// -- Full move number
index = fen.find(' ', index) + 1;
board.n_full_moves = std::stoi(fen.substr(index));
board.check = board._is_check_for(board.white_to_play ? White : Black);
board.nlm = board._no_legal_moves_for(board.white_to_play ? White : Black);
return board;
}
std::string Board::to_fen() const {
std::map<int, char> p2c{
{Piece::King, 'k'},
{Piece::Pawn, 'p'},
{Piece::Knigt, 'n'},
{Piece::Bishop, 'b'},
{Piece::Rook, 'r'},
{Piece::Queen, 'q'},
};
std::string ret;
// -- Pieces
for (int rank = 7; rank >= 0; rank--) {
int empty_cell_counter = 0;
for (int file = 0; file < 8; file++) {
if (piece_at({file, rank}) == Piece::None) {
empty_cell_counter++;
continue;
}
int full_piece = squares[rank * 8 + file];
char piece = p2c[full_piece & 0b111];
Colour colour = colour_at({file, rank});
if (empty_cell_counter > 0) {
ret += std::to_string(empty_cell_counter);
empty_cell_counter = 0;
}
ret += colour == Colour::White ? std::toupper(piece) : piece;
}
if (empty_cell_counter > 0)
ret += std::to_string(empty_cell_counter);
if (rank > 0)
ret += "/";
}
ret += " ";
// -- Active colour
ret += white_to_play ? 'w' : 'b';
ret += " ";
// -- Castling Rights
if (w_castle_rights == CastleSide::NeitherSide
&& b_castle_rights == CastleSide::NeitherSide)
ret += '-';
else {
if (w_castle_rights & CastleSide::KingSide)
ret += 'K';
if (w_castle_rights & CastleSide::QueenSide)
ret += 'Q';
if (b_castle_rights & CastleSide::KingSide)
ret += 'k';
if (b_castle_rights & CastleSide::QueenSide)
ret += 'q';
}
ret += ' ';
// -- En passant target
ret += en_passant_target == -1
? "-"
: Coords::from_index(en_passant_target).to_algebraic();
ret += ' ';
// -- Half move clock
ret += std::to_string(n_half_moves);
ret += ' ';
// -- Full moves number
ret += std::to_string(n_full_moves);
return ret;
}
Board Board::skip_turn() const {
Board ret = *this;
ret.white_to_play = !ret.white_to_play;
ret.check = ret._is_check_for(ret.white_to_play ? White : Black);
return ret;
}
Board Board::make_move(Move move, bool recurse_call) const {
Board ret;
std::copy(
std::begin(this->squares),
std::end(this->squares),
std::begin(ret.squares)
);
ret.white_to_play = !this->white_to_play;
// -- Actually make the move
ret.squares[move.source_square] = Piece::None;
ret.squares[move.target_square] = this->squares[move.source_square];
Piece source_piece = piece_at(move.source_square);
Piece target_piece = piece_at(move.target_square);
// -- Handle en passant target being eaten
if (en_passant_target != -1 && source_piece == Piece::Pawn
&& target_piece == 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 (source_piece == 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 (source_piece == Piece::King) {
if (move.target_square - move.source_square == 2) { // king side castle
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.target_square - move.source_square == -2) { // queen
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;
bool is_capturing = squares[move.target_square] != Piece::None;
if (white_to_play) {
if (source_piece == King)
ret.w_castle_rights = NeitherSide;
if (source_piece == 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 (is_capturing && target.y == 7 && target_piece == 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 (source_piece == King)
ret.b_castle_rights = NeitherSide;
if (source_piece == 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 (is_capturing && target.y == 0 && target_piece == 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);
}
}
ret.n_half_moves = n_half_moves + 1;
if (is_capturing || piece_at(move.source_square) == Piece::Pawn)
ret.n_half_moves = 0;
if (!white_to_play)
ret.n_full_moves = n_full_moves + 1;
if (ret.n_half_moves > 150) {
std::cerr << "too many recursions" << std::endl;
exit(1);
}
if (recurse_call) {
ret.check = ret._is_check_for(ret.white_to_play ? White : Black);
ret.nlm = ret._no_legal_moves_for(ret.white_to_play ? White : Black);
}
return ret;
}
bool Board::insufficient_material_for(Colour current_colour) const {
int n_bishop = 0, n_knight = 0;
for (int i = 0; i < 64; i++) {
Colour colour = colour_at(i);
if (colour != current_colour)
continue;
Piece piece = piece_at(i);
if (piece == Piece::Pawn || piece == Piece::Queen
|| piece == Piece::Rook)
return false;
if (piece == Piece::Bishop)
n_bishop++;
if (piece == Piece::Knigt && colour == Colour::White)
n_knight++;
}
return (n_bishop == 0 && n_knight == 0) || (n_bishop == 1 && n_knight == 0)
|| (n_bishop == 0 && n_knight == 1);
}
int8_t Board::get_king_of(int8_t colour) const {
for (int i = 0; i < 64; i++)
if (squares[i] == (colour | Piece::King))
return i;
std::stringstream ss;
ss << "Apparently there no kings of the such color in this board: "
<< std::endl;
ss << to_fen();
throw std::domain_error(ss.str());
}
bool Board::_is_check_for(Colour colour) const {
int8_t king_idx = this->get_king_of(colour);
std::vector<Move> all_moves;
all_moves.reserve(50);
for (int8_t i = 0; i < 64; i++) {
if (this->squares[i] == Piece::None || colour_at(i) == colour)
continue;
if (piece_at(i) == 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())
all_moves.push_back(Move{i, c.to_index()});
}
}
} else {
std::vector<Move> moves = legal_moves(
this->squares[i],
*this,
Coords::from_index(i),
true
);
all_moves.insert(all_moves.end(), moves.begin(), moves.end());
}
for (const Move& move : all_moves)
if (move.target_square == king_idx)
return true;
all_moves.clear();
}
return false;
}
bool Board::_no_legal_moves_for(Colour colour) const {
for (int i = 0; i < 64; i++) {
if (squares[i] == Piece::None || colour_at(i) != colour)
continue;
std::vector<Move> moves;
moves = legal_moves(squares[i], *this, Coords::from_index(i));
if (moves.size() > 0)
return false;
}
return true;
}
bool Board::no_legal_moves() const {
return nlm;
}
bool Board::is_check() const {
return check;
}
bool Board::is_checkmate() const {
return check && nlm;
}
bool Board::is_stalemate() const {
return !check && nlm;
}
bool Board::is_terminal() const {
return n_half_moves == 100 || insufficient_material() || is_checkmate()
|| is_stalemate();
}
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;
}
std::vector<Move> Board::all_capturing_moves() const {
std::vector<Move> moves = all_legal_moves();
std::vector<Move> ret;
ret.reserve(moves.size());
for (const Move& move : moves)
if (piece_at(move.target_square) != Piece::None)
ret.push_back(move);
return ret;
}
std::vector<int8_t> Board::opponent_pawn_attack_map() const {
std::vector<int8_t> ret;
for (int i = 0; i < 64; i++) {
if (piece_at(i) == Piece::Pawn
&& ((colour_at(i) == White && !white_to_play)
|| (colour_at(i) == Black && white_to_play))) {
std::vector<int8_t> attack_map =
pawn_attack_map(*this, Coords::from_index(i));
ret.insert(ret.end(), attack_map.begin(), attack_map.end());
}
}
return ret;
}

70
src/model/board/board.hpp Normal file
View File

@ -0,0 +1,70 @@
#pragma once
#include "../pieces/piece.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
#include <string>
struct Board {
private:
int8_t get_king_of(int8_t) const;
bool _no_legal_moves_for(Colour) const;
bool _is_check_for(Colour) const;
bool nlm = false, check = false;
public:
int8_t squares[64] = {Piece::None};
bool white_to_play = true;
int8_t w_castle_rights = CastleSide::NeitherSide;
int8_t b_castle_rights = CastleSide::NeitherSide;
int8_t en_passant_target = -1;
int n_half_moves = 0;
int n_full_moves = 0;
static Board setup_fen_position(std::string fen);
Board skip_turn() const;
Board make_move(Move, bool = true) const;
std::string to_fen() const;
bool no_legal_moves() const;
bool is_check() const;
bool insufficient_material_for(Colour) const;
bool insufficient_material() const {
return insufficient_material_for(White)
&& insufficient_material_for(Black);
};
std::vector<Move> all_legal_moves() const;
std::vector<Move> all_capturing_moves() const;
std::vector<int8_t> opponent_pawn_attack_map() const;
bool is_checkmate() const;
bool is_stalemate() const;
bool is_terminal() const;
inline Piece piece_at(int8_t idx) const {
return (Piece) (squares[idx] & 0b00111);
}
inline Piece piece_at(Coords xy) const {
return piece_at(xy.to_index());
}
inline Colour colour_at(int8_t idx) const {
return (Colour) (squares[idx] & 0b11000);
}
inline Colour colour_at(Coords xy) const {
return colour_at(xy.to_index());
}
};
inline bool operator<(const Board& m1, const Board& m2) {
return m1.to_fen() < m2.to_fen(
); // TODO: make this the comparison between the hash of the board
}

View File

@ -0,0 +1,7 @@
#include <cstdint>
enum CastleSide : int8_t {
NeitherSide = 0,
KingSide = 1,
QueenSide = 2,
};

188
src/model/perft/perft.cpp Normal file
View File

@ -0,0 +1,188 @@
#include "perft.hpp"
#include "../board/board.hpp"
#include "../utils/move.hpp"
#include "../utils/threadpool.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(
const Board& b, int depth, int max_depth, ThreadPool& pool
) {
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;
if (depth == max_depth) {
// Parallel execution at the top level
std::vector<std::future<int>> futures;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
futures.push_back(pool.enqueue(
move_generation_test,
tmp_board,
depth - 1,
max_depth,
std::ref(pool)
));
}
for (auto& future : futures)
num_pos += future.get(); // Retrieve the result of each task
} else {
// Regular sequential execution
for (const Move& move : moves) {
// std::cout << "Looking at " << move << std::endl;
Board tmp_board = b.make_move(move);
int n = move_generation_test(tmp_board, depth - 1, max_depth, pool);
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);
ThreadPool pool(std::thread::hardware_concurrency());
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, pool);
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

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

View File

@ -0,0 +1,23 @@
#include "../board/board.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
#include "piece.hpp"
#include <vector>
std::vector<Move> bishop_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
auto ne = look_direction(b, xy, 1, 1);
ret.insert(ret.end(), ne.begin(), ne.end());
auto se = look_direction(b, xy, 1, -1);
ret.insert(ret.end(), se.begin(), se.end());
auto sw = look_direction(b, xy, -1, -1);
ret.insert(ret.end(), sw.begin(), sw.end());
auto nw = look_direction(b, xy, -1, 1);
ret.insert(ret.end(), nw.begin(), nw.end());
return ret;
}

78
src/model/pieces/king.cpp Normal file
View File

@ -0,0 +1,78 @@
#include "../board/board.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
#include "piece.hpp"
static bool is_clear_king_side(const Board& b, const Coords xy) {
for (int dx = 1; dx < 3; dx++) {
Coords c{xy.x + dx, xy.y};
if (b.squares[c.to_index()] != Piece::None)
return false;
std::optional<Move> move = move_for_position(b, xy, c);
Board board_after_move = b.make_move(move.value(), false);
board_after_move = board_after_move.skip_turn();
if (board_after_move.is_check())
return false;
}
return true;
}
static bool is_clear_queen_side(const Board& b, const Coords xy) {
for (int dx = 1; dx < 4; dx++) {
Coords c{xy.x - dx, xy.y};
if (b.squares[c.to_index()] != Piece::None)
return false;
std::optional<Move> move = move_for_position(b, xy, c);
Board board_after_move = b.make_move(move.value(), false);
board_after_move = board_after_move.skip_turn();
if (dx < 3 && board_after_move.is_check())
return false;
}
return true;
}
std::vector<Move> king_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
// -- Regular moves
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) // skip staying in the same position
continue;
Coords c{xy.x + dx, xy.y + dy};
std::optional<Move> move = move_for_position(b, xy, c);
if (move.has_value())
ret.push_back(move.value());
}
}
if (b.is_check())
return keep_only_blocking(ret, b);
// -- Castles
int8_t castling_rights = b.colour_at(xy) == Colour::White
? b.w_castle_rights
: b.b_castle_rights;
if (castling_rights == CastleSide::NeitherSide)
return ret;
if (castling_rights & CastleSide::KingSide && is_clear_king_side(b, xy)) {
ret.push_back(Move{
xy.to_index(),
Coords{6, xy.y}.to_index(),
});
}
if (castling_rights & CastleSide::QueenSide && is_clear_queen_side(b, xy)) {
ret.push_back(Move{
xy.to_index(),
Coords{2, xy.y}.to_index(),
});
}
return ret;
}

View File

@ -0,0 +1,28 @@
#include "../board/board.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
#include "piece.hpp"
#include <vector>
std::vector<Move> knight_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
std::vector<std::pair<int, int>> moves = {
{+2, +1},
{+1, +2}, // north east
{+2, -1},
{+1, -2}, // south east
{-2, -1},
{-1, -2}, // south west
{-2, +1},
{-1, +2} // north west
};
for (const auto& [dx, dy] : moves) {
std::optional<Move> move =
move_for_position(b, xy, Coords{xy.x + dx, xy.y + dy});
if (move.has_value())
ret.push_back(move.value());
}
return ret;
}

102
src/model/pieces/pawn.cpp Normal file
View File

@ -0,0 +1,102 @@
#include "../board/board.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
#include "piece.hpp"
std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
std::vector<Move> ret{};
Colour my_colour = b.colour_at(xy);
// -- Capture to the left
if (xy.x > 0) {
int dy = my_colour == Colour::White ? 1 : -1;
Coords left{xy.x - 1, xy.y + dy};
int8_t capturable_piece = b.squares[left.to_index()];
if (capturable_piece != 0 && my_colour != b.colour_at(left)) {
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(),
(Piece) (my_colour | piece)
});
else
ret.push_back(Move{xy.to_index(), left.to_index()});
}
}
// -- Capture to the right
if (xy.x < 7) {
int dy = my_colour == Colour::White ? 1 : -1;
Coords right{xy.x + 1, xy.y + dy};
int8_t capturable_piece = b.squares[right.to_index()];
if (capturable_piece != 0 && my_colour != b.colour_at(right)) {
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(),
(Piece) (my_colour | piece)
});
else
ret.push_back(Move{xy.to_index(), right.to_index()});
}
}
// -- 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()});
}
// -- 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;
for (int dy = 1; dy < max_dy; dy++) {
int actual_dy = my_colour == Colour::White ? dy : -dy;
Coords new_xy{xy.x, xy.y + actual_dy};
if (b.squares[new_xy.to_index()] != Piece::None)
break;
if (new_xy.y == 7 || new_xy.y == 0)
for (auto piece : {Rook, Knigt, Bishop, Queen})
ret.push_back(Move{
xy.to_index(),
new_xy.to_index(),
(Piece) (my_colour | piece)
});
else
ret.push_back(Move{
xy.to_index(),
new_xy.to_index(),
});
}
return ret;
}
std::vector<int8_t> pawn_attack_map(const Board& b, Coords xy) {
std::vector<int8_t> ret{};
Colour my_colour = b.colour_at(xy);
// -- Capture to the left
if (xy.x > 0) {
int dy = my_colour == Colour::White ? 1 : -1;
Coords left{xy.x - 1, xy.y + dy};
ret.push_back(left.to_index());
}
// -- Capture to the right
if (xy.x < 7) {
int dy = my_colour == Colour::White ? 1 : -1;
Coords right{xy.x + 1, xy.y + dy};
ret.push_back(right.to_index());
}
return ret;
}

View File

@ -0,0 +1,83 @@
#include "piece.hpp"
#include "../board/board.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
std::vector<Move>
keep_only_blocking(const std::vector<Move> candidates, const Board& board) {
if (candidates.size() == 0)
return {};
std::vector<Move> ret;
for (Move move : candidates) {
Board board_after_move = board.make_move(move, false);
board_after_move = board_after_move.skip_turn();
if (!board_after_move.is_check())
ret.push_back(move);
}
return ret;
}
std::vector<Move>
legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) {
std::vector<Move> ret;
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;
}
if (!looking_for_check)
return keep_only_blocking(ret, b);
return ret;
}
std::optional<Move>
move_for_position(const Board& board, const Coords source, const Coords dest) {
if (!dest.is_within_bounds()
|| board.colour_at(source) == board.colour_at(dest))
return {};
return Move{source.to_index(), dest.to_index()};
}
std::vector<Move>
look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
std::vector<Move> ret;
for (int d = 1; d < 8; d++) {
int dx = mult_dx * d;
int dy = mult_dy * d;
Coords target{xy.x + dx, xy.y + dy};
std::optional<Move> move = move_for_position(board, xy, target);
if (move.has_value()) {
ret.push_back(move.value());
if (board.squares[target.to_index()] != Piece::None)
break;
} else {
break;
}
}
return ret;
}

View File

@ -0,0 +1,78 @@
#pragma once
#include <cstdint>
#include <optional>
#include <ostream>
#include <vector>
enum Piece : int8_t {
None = 0,
Rook = 1,
Knigt = 2,
Bishop = 3,
Queen = 4,
King = 5,
Pawn = 6,
};
enum Colour : int8_t {
White = 8,
Black = 16,
};
inline const char* to_string(Colour c) {
switch (c) {
case White:
return "White";
case Black:
return "Black";
default:
return "[Unknown Colour]";
}
}
inline const char* to_string(Piece c) {
switch (c) {
case Pawn:
return "Pawn";
case Rook:
return "Rook";
case Bishop:
return "Bishop";
case Knigt:
return "Knight";
case Queen:
return "Queen";
case King:
return "King";
default:
return "[Unknown Colour]";
}
}
inline std::ostream& operator<<(std::ostream& os, const Colour& i) {
os << std::to_string(i);
return os;
}
inline std::ostream& operator<<(std::ostream& os, const Piece& 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 = 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);
std::vector<int8_t> pawn_attack_map(const Board&, const Coords);
std::vector<Move> pawn_moves(const Board&, const Coords);
std::vector<Move> rook_moves(const Board&, const Coords);
std::vector<Move> knight_moves(const Board&, const Coords);
std::vector<Move> bishop_moves(const Board&, const Coords);
std::vector<Move> queen_moves(const Board&, const Coords);
std::vector<Move> king_moves(const Board&, const Coords);

View File

@ -0,0 +1,35 @@
#include "../board/board.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
#include "piece.hpp"
#include <vector>
std::vector<Move> queen_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
auto e = look_direction(b, xy, 1, 0);
ret.insert(ret.end(), e.begin(), e.end());
auto s = look_direction(b, xy, 0, -1);
ret.insert(ret.end(), s.begin(), s.end());
auto w = look_direction(b, xy, -1, 0);
ret.insert(ret.end(), w.begin(), w.end());
auto n = look_direction(b, xy, 0, 1);
ret.insert(ret.end(), n.begin(), n.end());
auto ne = look_direction(b, xy, 1, 1);
ret.insert(ret.end(), ne.begin(), ne.end());
auto se = look_direction(b, xy, 1, -1);
ret.insert(ret.end(), se.begin(), se.end());
auto sw = look_direction(b, xy, -1, -1);
ret.insert(ret.end(), sw.begin(), sw.end());
auto nw = look_direction(b, xy, -1, 1);
ret.insert(ret.end(), nw.begin(), nw.end());
return ret;
}

23
src/model/pieces/rook.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "../board/board.hpp"
#include "../utils/coords.hpp"
#include "../utils/move.hpp"
#include "piece.hpp"
#include <vector>
std::vector<Move> rook_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
auto e = look_direction(b, xy, 1, 0);
ret.insert(ret.end(), e.begin(), e.end());
auto s = look_direction(b, xy, 0, -1);
ret.insert(ret.end(), s.begin(), s.end());
auto w = look_direction(b, xy, -1, 0);
ret.insert(ret.end(), w.begin(), w.end());
auto n = look_direction(b, xy, 0, 1);
ret.insert(ret.end(), n.begin(), n.end());
return ret;
}

View File

@ -0,0 +1,71 @@
#pragma once
#include <iostream>
#include <stdexcept>
#include <string>
static std::string _FILES = "abcdefgh";
static std::string _RANKS = "12345678";
struct Coords {
int x, y;
Coords(int x, int y): x(x), y(y) {}
int8_t to_index() const {
return this->y * 8 + this->x;
}
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};
}
static Coords from_algebraic(std::string pos) {
if (pos.size() != 2)
throw std::invalid_argument(
"An algebraic coordinate should only have two characters"
);
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");
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{(int) x, (int) y};
}
std::string to_algebraic() const {
std::string ret;
if (x > 7 || y > 7)
throw std::invalid_argument(
"Can't give the algebraic vesion of an invalid coord"
);
ret += _FILES[x];
ret += _RANKS[y];
return ret;
}
bool is_within_bounds() const {
return 0 <= x && x < 8 && 0 <= y && y < 8;
}
};
inline bool operator==(const Coords& a, const Coords& b) {
return a.x == b.x && a.y == b.y;
}
inline bool operator!=(const Coords& a, const Coords& b) {
return !(a == b);
}
inline std::ostream& operator<<(std::ostream& os, const Coords& coords) {
os << coords.to_algebraic();
return os;
}

26
src/model/utils/move.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "move.hpp"
#include "../board/board.hpp"
#include "utils.hpp"
#include <algorithm>
int Move::score_guess(const Board& b) const {
int ret = 0;
Piece me_piece = b.piece_at(source_square);
Piece captured_piece = b.piece_at(target_square);
if (captured_piece != Piece::None)
ret += 10 * piece_value(captured_piece) - piece_value(me_piece);
if (promoting_to != Piece::None)
ret += piece_value(promoting_to);
std::vector<int8_t> pawn_attack_map = b.opponent_pawn_attack_map();
if (std::find(pawn_attack_map.begin(), pawn_attack_map.end(), target_square)
!= pawn_attack_map.end())
ret -= me_piece;
return ret;
}

51
src/model/utils/move.hpp Normal file
View File

@ -0,0 +1,51 @@
#pragma once
#include "../board/castle_side.hpp"
#include "../pieces/piece.hpp"
#include "coords.hpp"
#include <cstdint>
#include <sstream>
struct Move {
int8_t source_square;
int8_t target_square;
Piece promoting_to = Piece::None;
int score_guess(const Board&) const;
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 bool operator<(const Move& m1, const Move& m2) {
return m1.to_string() < m2.to_string();
}
inline std::ostream& operator<<(std::ostream& os, const Move& m) {
os << m.to_string();
return os;
}

View File

@ -0,0 +1,73 @@
#pragma once
#include <condition_variable>
#include <functional>
#include <future>
#include <iostream>
#include <queue>
#include <thread>
#include <vector>
class ThreadPool {
public:
ThreadPool(size_t numThreads) {
for (size_t i = 0; i < numThreads; ++i) {
workers.emplace_back([this] {
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(queueMutex);
condition.wait(lock, [this] {
return stop || !tasks.empty();
});
if (stop && tasks.empty())
return;
task = std::move(tasks.front());
tasks.pop();
}
task();
}
});
}
}
template <class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::invoke_result<F, Args...>::type> {
using return_type = typename std::invoke_result<F, Args...>::type;
auto task = std::make_shared<std::packaged_task<return_type()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queueMutex);
tasks.emplace([task]() { (*task)(); });
}
condition.notify_one();
return res;
}
void waitAll() {
std::unique_lock<std::mutex> lock(queueMutex);
condition.wait(lock, [this] { return tasks.empty(); });
}
~ThreadPool() {
{
std::unique_lock<std::mutex> lock(queueMutex);
stop = true;
}
condition.notify_all();
for (std::thread& worker : workers)
worker.join();
}
private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex queueMutex;
std::condition_variable condition;
bool stop = false;
};

35
src/model/utils/utils.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "utils.hpp"
#include "../board/board.hpp"
std::vector<int8_t> to_target_square(std::vector<Move> moves) {
std::vector<int8_t> ret;
for (Move move : moves)
ret.push_back(move.target_square);
return ret;
}
int piece_value(Piece p) {
switch (p) {
case Piece::Pawn:
return PawnValue;
case Piece::Knigt:
return KnightValue;
case Piece::Bishop:
return BishopValue;
case Piece::Rook:
return RookValue;
case Piece::Queen:
return QueenValue;
default:
return 0;
}
}
int count_material(const Board& b, int8_t colour) {
int ret = 0;
for (int i = 0; i < 64; i++)
if (b.colour_at(i) == colour)
ret += piece_value(b.piece_at(i));
return ret;
}

19
src/model/utils/utils.hpp Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include "move.hpp"
#include <cstdint>
#include <limits>
#include <vector>
std::vector<int8_t> to_target_square(std::vector<Move>);
int count_material(const Board&, int8_t);
int piece_value(Piece);
const int INFINITY = std::numeric_limits<int>::max();
const int PawnValue = 100;
const int KnightValue = 300;
const int BishopValue = 320;
const int RookValue = 500;
const int QueenValue = 900;

215
src/view/gui.cpp Normal file
View File

@ -0,0 +1,215 @@
#include "gui.hpp"
#include "../model/utils/utils.hpp"
#include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp>
GUI::GUI() {
window.create(sf::VideoMode(WINDOW_SIZE, WINDOW_SIZE), "Chess Board");
load_textures();
font.loadFromFile("res/arial.ttf");
}
void GUI::update_board(
const Board& b, int8_t selected_square, std::vector<int8_t> targets
) {
window.clear();
draw_board(selected_square, targets);
draw_pieces(b);
window.display();
}
void GUI::notify_stalemate(Colour col) {
std::cout << "Stalemate for " << to_string(col) << std::endl;
}
void GUI::notify_checkmate(Colour col) {
std::cout << "Checkmate for " << to_string(col) << std::endl;
}
void GUI::handle_events() {
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::MouseButtonPressed)
handle_click(event.mouseButton.x, event.mouseButton.y);
}
void GUI::load_textures() {
const std::string names[6] =
{"rook", "knight", "bishop", "queen", "king", "pawn"
}; // don't touch the order, it's reflecting the one in the Piece enum
for (int i = 0; i < 6; ++i) {
textures[i][0].loadFromFile("res/pieces/white-" + names[i] + ".png");
textures[i][1].loadFromFile("res/pieces/black-" + names[i] + ".png");
}
}
void GUI::handle_click(int x, int y) {
int file = x / TILE_SIZE;
int rank = 7 - (y / TILE_SIZE);
controller->on_tile_selected(file, rank);
}
void GUI::draw_annotation(int file, int rank) {
if (file == 0) {
sf::Text annotation(std::to_string(rank + 1), font);
annotation.setStyle(sf::Text::Bold);
annotation.setCharacterSize(16);
annotation.setFillColor(rank % 2 == 0 ? colours[1] : colours[0]);
annotation.setPosition(
(file + .05) * TILE_SIZE,
(7 - rank + .05) * TILE_SIZE
);
window.draw(annotation);
}
if (rank == 0) {
sf::Text annotation("abcdefgh"[file], font);
annotation.setCharacterSize(16);
annotation.setOrigin(16, 16);
annotation.setStyle(sf::Text::Bold);
annotation.setFillColor(file % 2 == 0 ? colours[1] : colours[0]);
annotation.setPosition(
(file + 1) * TILE_SIZE,
(7 - rank + .95) * TILE_SIZE
);
window.draw(annotation);
}
}
void GUI::draw_board(int selected_square, std::vector<int8_t> targets) {
sf::RectangleShape square(sf::Vector2f(TILE_SIZE, TILE_SIZE));
for (int rank = 0; rank < 8; ++rank) {
for (int file = 0; file < 8; ++file) {
int8_t index = Coords{file, rank}.to_index();
square.setPosition(file * TILE_SIZE, (7 - rank) * TILE_SIZE);
if (index == selected_square)
square.setFillColor(
(file + rank) % 2 == 0 ? alt_colours[0] : alt_colours[1]
);
else
square.setFillColor(
(file + rank) % 2 == 0 ? colours[0] : colours[1]
);
window.draw(square);
draw_annotation(file, rank);
if (std::find(targets.begin(), targets.end(), index)
!= targets.end()) {
float r = .15 * TILE_SIZE;
sf::CircleShape circle{r};
sf::Color c(0x00000055);
circle.setFillColor(c);
circle.setOrigin(r, r);
circle.setPosition(
(file + .5) * TILE_SIZE,
(7 - rank + .5) * TILE_SIZE
);
window.draw(circle);
}
}
}
}
void GUI::draw_pieces(const Board& board) {
for (int i = 0; i < 64; ++i) {
int piece = board.piece_at(i);
if (piece != Piece::None) {
int colour = board.colour_at(i) == Colour::White ? 0 : 1;
pieces[i].setTexture(textures[piece - 1][colour]);
sf::Vector2 center = textures[piece - 1][colour].getSize() / 2u;
pieces[i].setOrigin(center.x, center.y);
pieces[i].setPosition(
(i % 8 + .5) * TILE_SIZE,
(7 - (int) (i / 8) + .5) * TILE_SIZE
);
window.draw(pieces[i]);
}
}
}
int GUI::show_popup(
const std::string& message, const std::vector<std::string>& options
) {
sf::RenderWindow popup(sf::VideoMode(300, 200), "Choice");
sf::Font font;
if (!font.loadFromFile("res/arial.ttf")) {
std::cerr << "Error: Could not load font!" << std::endl;
return -1;
}
sf::Text text(message, font, 20);
text.setPosition(20, 20);
text.setFillColor(sf::Color::Black);
std::vector<sf::RectangleShape> buttonShapes;
std::vector<sf::Text> buttonTexts;
for (size_t i = 0; i < options.size(); ++i) {
sf::RectangleShape button(sf::Vector2f(200, 30));
button.setPosition(50, 70 + i * 40);
button.setFillColor(sf::Color(150, 150, 150));
buttonShapes.push_back(button);
sf::Text buttonText(options[i], font, 18);
buttonText.setPosition(60, 75 + i * 40);
buttonText.setFillColor(sf::Color::Black);
buttonTexts.push_back(buttonText);
}
while (popup.isOpen()) {
sf::Event event;
while (popup.pollEvent(event)) {
if (event.type == sf::Event::Closed)
popup.close();
else if (event.type == sf::Event::MouseButtonPressed) {
for (size_t i = 0; i < buttonShapes.size(); ++i) {
if (buttonShapes[i].getGlobalBounds().contains(
event.mouseButton.x,
event.mouseButton.y
)) {
popup.close();
return i;
}
}
}
}
popup.clear(sf::Color::White);
popup.draw(text);
for (size_t i = 0; i < buttonShapes.size(); ++i) {
popup.draw(buttonShapes[i]);
popup.draw(buttonTexts[i]);
}
popup.display();
}
return -1;
}
Piece GUI::ask_about_promotion() {
std::vector<std::string> options = {"Queen", "Rook", "Bishop", "Knight"};
int idx = show_popup("Please choose a promotion for your pawn", options);
switch (idx) {
case 0:
return Queen;
case 1:
return Rook;
case 2:
return Bishop;
case 3:
return Knigt;
};
return Piece::None;
}
void GUI::show() {
while (window.isOpen())
handle_events();
}

41
src/view/gui.hpp Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include "../model/board/board.hpp"
#include "view.hpp"
#include <SFML/Graphics.hpp>
const int TILE_SIZE = 80;
const int BOARD_SIZE = 8;
const int WINDOW_SIZE = TILE_SIZE * BOARD_SIZE;
class GUI : public View {
public:
GUI();
void show() override;
Piece ask_about_promotion();
void update_board(const Board&, int8_t, std::vector<int8_t>) override;
void notify_checkmate(Colour) override;
void notify_stalemate(Colour) override;
private:
sf::RenderWindow window;
sf::Texture textures[6][2];
sf::Sprite pieces[64];
sf::Font font;
sf::Color colours[2] = {sf::Color(0xB88762FF), sf::Color(0xEDD6B0FF)};
sf::Color alt_colours[2] = {sf::Color(0xDCC34BFF), sf::Color(0xF6EB72FF)};
int show_popup(
const std::string& message, const std::vector<std::string>& options
);
void load_textures();
void handle_events();
void handle_click(int, int);
void draw_board(int, std::vector<int8_t>);
void draw_pieces(const Board&);
void draw_annotation(int, int);
};

16
src/view/noop.hpp Normal file
View File

@ -0,0 +1,16 @@
#include "../model/board/board.hpp"
#include "view.hpp"
class NoOpView : public View {
public:
NoOpView() {};
void show() override {};
void update_board(const Board&, int8_t, std::vector<int8_t>) override {};
void notify_checkmate(Colour) override{};
void notify_stalemate(Colour) override{};
Piece ask_about_promotion() override {
return Queen;
};
};

22
src/view/view.hpp Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include "../controller/controller.hpp"
#include "../model/board/board.hpp"
#include "../model/pieces/piece.hpp"
#include "../model/utils/move.hpp"
class View {
protected:
Controller* controller;
public:
void set_controller(Controller* c) {
controller = c;
}
virtual void show() = 0;
virtual Piece ask_about_promotion() = 0;
virtual void update_board(const Board&, int8_t, std::vector<int8_t>) = 0;
virtual void notify_checkmate(Colour) = 0;
virtual void notify_stalemate(Colour) = 0;
};