Removed the python version and kept just the c++ one
This commit is contained in:
38
src/controller/ai_vs_ai.cpp
Normal file
38
src/controller/ai_vs_ai.cpp
Normal 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) {}
|
19
src/controller/ai_vs_ai.hpp
Normal file
19
src/controller/ai_vs_ai.hpp
Normal 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;
|
||||
};
|
7
src/controller/controller.cpp
Normal file
7
src/controller/controller.cpp
Normal 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);
|
||||
}
|
17
src/controller/controller.hpp
Normal file
17
src/controller/controller.hpp
Normal 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;
|
||||
};
|
29
src/controller/human_vs_ai.cpp
Normal file
29
src/controller/human_vs_ai.cpp
Normal 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();
|
||||
}
|
||||
}
|
14
src/controller/human_vs_ai.hpp
Normal file
14
src/controller/human_vs_ai.hpp
Normal 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
74
src/controller/manual.cpp
Normal 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
22
src/controller/manual.hpp
Normal 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;
|
||||
};
|
Reference in New Issue
Block a user