Compare commits
No commits in common. "04134f013c5495ada1538002e22fd48fe14e2c7a" and "74e4d596a7bafacd69ae30594c268978d8592c36" have entirely different histories.
04134f013c
...
74e4d596a7
@ -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) {}
|
@ -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;
|
||||
};
|
@ -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);
|
||||
}
|
@ -7,11 +7,8 @@ class View;
|
||||
class Controller {
|
||||
protected:
|
||||
Board board;
|
||||
View& view;
|
||||
virtual void make_move(Move) = 0;
|
||||
|
||||
public:
|
||||
Controller(Board, View&);
|
||||
virtual void start() = 0;
|
||||
// Controller(Board, View);
|
||||
virtual void on_tile_selected(int, int) = 0;
|
||||
};
|
||||
|
@ -4,7 +4,10 @@
|
||||
|
||||
HumanVsAIController::HumanVsAIController(Board b, View& v, ai::AI& ai)
|
||||
: ManualController(b, v),
|
||||
ai(ai) {}
|
||||
ai(ai) {
|
||||
view.set_controller(this);
|
||||
reset_selection();
|
||||
}
|
||||
|
||||
void HumanVsAIController::on_tile_selected(int x, int y) {
|
||||
Coords c{x, y};
|
||||
|
@ -4,14 +4,12 @@
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
void ManualController::start() {
|
||||
view.show();
|
||||
}
|
||||
|
||||
void ManualController::on_tile_selected(int x, int y) {
|
||||
Coords c{x, y};
|
||||
Piece piece = board.piece_at(c);
|
||||
|
@ -7,16 +7,16 @@
|
||||
|
||||
class ManualController : public Controller {
|
||||
protected:
|
||||
View& view;
|
||||
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;
|
||||
void make_move(Move);
|
||||
|
||||
public:
|
||||
ManualController(Board, View&);
|
||||
void on_tile_selected(int, int) override;
|
||||
void start() override;
|
||||
};
|
||||
|
@ -1,4 +1,3 @@
|
||||
#include "controller/ai_vs_ai.hpp"
|
||||
#include "controller/controller.hpp"
|
||||
#include "controller/human_vs_ai.hpp"
|
||||
#include "controller/manual.hpp"
|
||||
@ -6,23 +5,22 @@
|
||||
#include "view/noop.hpp"
|
||||
#include "view/view.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
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);
|
||||
|
||||
ai::v0_random p1(std::chrono::milliseconds(500));
|
||||
ai::v0_random p2(std::chrono::milliseconds(500));
|
||||
ai::v0_random ai;
|
||||
|
||||
GUI gui;
|
||||
AIvsAIController manual(b, gui, p1, p2);
|
||||
HumanVsAIController manual(b, gui, ai);
|
||||
|
||||
|
||||
View& view = gui;
|
||||
Controller& controller = manual;
|
||||
|
||||
controller.start();
|
||||
view.show();
|
||||
|
||||
// perft();
|
||||
// ai::v1_simple ai;
|
||||
|
@ -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;
|
||||
}
|
@ -3,28 +3,16 @@
|
||||
#include "../board/board.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
|
||||
namespace ai {
|
||||
class AI {
|
||||
protected:
|
||||
std::chrono::milliseconds thinking_time;
|
||||
virtual Move _search(const Board&, bool = false) = 0;
|
||||
|
||||
public:
|
||||
AI(std::chrono::milliseconds tt): thinking_time(tt) {}
|
||||
|
||||
struct AI {
|
||||
std::atomic<bool> stop_computation = false;
|
||||
|
||||
Move search(const Board& b, bool am_white = false);
|
||||
|
||||
virtual Move search(const Board&, bool = false) = 0;
|
||||
virtual int eval(const Board&) = 0;
|
||||
};
|
||||
|
||||
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 {
|
||||
return 0;
|
||||
@ -35,9 +23,7 @@ namespace ai {
|
||||
int _search(const Board&, int);
|
||||
|
||||
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;
|
||||
};
|
||||
} // namespace ai
|
||||
|
@ -1,8 +1,6 @@
|
||||
#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();
|
||||
|
||||
return moves[rand() % moves.size()];
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
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());
|
||||
|
||||
std::vector<Move> moves = b.all_legal_moves();
|
||||
|
Loading…
x
Reference in New Issue
Block a user