Compare commits

..

No commits in common. "04134f013c5495ada1538002e22fd48fe14e2c7a" and "74e4d596a7bafacd69ae30594c268978d8592c36" have entirely different histories.

12 changed files with 21 additions and 126 deletions

View File

@ -1,36 +0,0 @@
#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;
Move move = current_player->search(board, board.white_to_play);
make_move(move);
}
view_thread.join();
}
void AIvsAIController::make_move(Move move) {
board = board.make_move(move);
view.update_board(board, -1, {});
Colour current_colour = board.white_to_play ? White : Black;
if (board.is_checkmate_for(current_colour))
view.notify_checkmate(current_colour);
if (board.is_stalemate_for(current_colour))
view.notify_stalemate(current_colour);
}
void AIvsAIController::on_tile_selected(int, int) {}

View File

@ -1,19 +0,0 @@
#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

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

View File

@ -7,11 +7,8 @@ class View;
class Controller { class Controller {
protected: protected:
Board board; Board board;
View& view;
virtual void make_move(Move) = 0;
public: public:
Controller(Board, View&); // Controller(Board, View);
virtual void start() = 0;
virtual void on_tile_selected(int, int) = 0; virtual void on_tile_selected(int, int) = 0;
}; };

View File

@ -4,7 +4,10 @@
HumanVsAIController::HumanVsAIController(Board b, View& v, ai::AI& ai) HumanVsAIController::HumanVsAIController(Board b, View& v, ai::AI& ai)
: ManualController(b, v), : ManualController(b, v),
ai(ai) {} ai(ai) {
view.set_controller(this);
reset_selection();
}
void HumanVsAIController::on_tile_selected(int x, int y) { void HumanVsAIController::on_tile_selected(int x, int y) {
Coords c{x, y}; Coords c{x, y};

View File

@ -4,14 +4,12 @@
#include <algorithm> #include <algorithm>
ManualController::ManualController(Board b, View& v): Controller(b, v) { ManualController::ManualController(Board b, View& view): view(view) {
board = b;
view.set_controller(this);
reset_selection(); reset_selection();
} }
void ManualController::start() {
view.show();
}
void ManualController::on_tile_selected(int x, int y) { void ManualController::on_tile_selected(int x, int y) {
Coords c{x, y}; Coords c{x, y};
Piece piece = board.piece_at(c); Piece piece = board.piece_at(c);

View File

@ -7,16 +7,16 @@
class ManualController : public Controller { class ManualController : public Controller {
protected: protected:
View& view;
int8_t selected_index; int8_t selected_index;
Piece selected_piece; Piece selected_piece;
std::vector<int8_t> targets; std::vector<int8_t> targets;
void reset_selection(); void reset_selection();
void show_legal_moves(Coords); void show_legal_moves(Coords);
void make_move(Move) override; void make_move(Move);
public: public:
ManualController(Board, View&); ManualController(Board, View&);
void on_tile_selected(int, int) override; void on_tile_selected(int, int) override;
void start() override;
}; };

View File

@ -1,4 +1,3 @@
#include "controller/ai_vs_ai.hpp"
#include "controller/controller.hpp" #include "controller/controller.hpp"
#include "controller/human_vs_ai.hpp" #include "controller/human_vs_ai.hpp"
#include "controller/manual.hpp" #include "controller/manual.hpp"
@ -6,23 +5,22 @@
#include "view/noop.hpp" #include "view/noop.hpp"
#include "view/view.hpp" #include "view/view.hpp"
#include <chrono>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::string pos = std::string pos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; "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);
ai::v0_random p1(std::chrono::milliseconds(500)); ai::v0_random ai;
ai::v0_random p2(std::chrono::milliseconds(500));
GUI gui; GUI gui;
AIvsAIController manual(b, gui, p1, p2); HumanVsAIController manual(b, gui, ai);
View& view = gui;
Controller& controller = manual; Controller& controller = manual;
controller.start(); view.show();
// perft(); // perft();
// ai::v1_simple ai; // ai::v1_simple ai;

View File

@ -1,23 +0,0 @@
#include "ai.hpp"
#include <thread>
Move ai::AI::search(const Board& b, bool am_white) {
Move result;
// Start computation in a separate thread
std::thread computation_thread([&]() { result = _search(b, am_white); });
// Start a timer thread to stop computation after given time
std::thread timer_thread([&]() {
std::this_thread::sleep_for(thinking_time);
stop_computation = true;
});
// Wait for computation thread to finish
computation_thread.join();
// Ensure timer thread is also stopped
timer_thread.join();
return result;
}

View File

@ -3,28 +3,16 @@
#include "../board/board.hpp" #include "../board/board.hpp"
#include <atomic> #include <atomic>
#include <chrono>
namespace ai { namespace ai {
class AI { struct AI {
protected:
std::chrono::milliseconds thinking_time;
virtual Move _search(const Board&, bool = false) = 0;
public:
AI(std::chrono::milliseconds tt): thinking_time(tt) {}
std::atomic<bool> stop_computation = false; std::atomic<bool> stop_computation = false;
virtual Move search(const Board&, bool = false) = 0;
Move search(const Board& b, bool am_white = false);
virtual int eval(const Board&) = 0; virtual int eval(const Board&) = 0;
}; };
struct v0_random : public AI { struct v0_random : public AI {
v0_random(std::chrono::milliseconds tt): AI(tt) {} Move search(const Board&, bool) override;
Move _search(const Board&, bool) override;
int eval(const Board&) override { int eval(const Board&) override {
return 0; return 0;
@ -35,9 +23,7 @@ namespace ai {
int _search(const Board&, int); int _search(const Board&, int);
public: public:
v1_simple(std::chrono::milliseconds tt): AI(tt) {} Move search(const Board&, bool) override;
Move _search(const Board&, bool) override;
int eval(const Board&) override; int eval(const Board&) override;
}; };
} // namespace ai } // namespace ai

View File

@ -1,8 +1,6 @@
#include "ai.hpp" #include "ai.hpp"
#include <thread> Move ai::v0_random::search(const Board& b, bool) {
Move ai::v0_random::_search(const Board& b, bool) {
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();
return moves[rand() % moves.size()]; return moves[rand() % moves.size()];

View File

@ -7,7 +7,7 @@
static int INFINITY = std::numeric_limits<int>::max(); static int INFINITY = std::numeric_limits<int>::max();
Move ai::v1_simple::_search(const Board& b, bool am_white) { Move ai::v1_simple::search(const Board& b, bool am_black) {
ThreadPool pool(std::thread::hardware_concurrency()); ThreadPool pool(std::thread::hardware_concurrency());
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();