very easily implemented the human_vs_ai controller
(god i love a good design)
This commit is contained in:
parent
60e1a77fcb
commit
74e4d596a7
32
cpp/src/controller/human_vs_ai.cpp
Normal file
32
cpp/src/controller/human_vs_ai.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include "human_vs_ai.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
HumanVsAIController::HumanVsAIController(Board b, View& v, ai::AI& ai)
|
||||
: ManualController(b, v),
|
||||
ai(ai) {
|
||||
view.set_controller(this);
|
||||
reset_selection();
|
||||
}
|
||||
|
||||
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
cpp/src/controller/human_vs_ai.hpp
Normal file
14
cpp/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;
|
||||
};
|
@ -6,7 +6,7 @@
|
||||
#include "controller.hpp"
|
||||
|
||||
class ManualController : public Controller {
|
||||
private:
|
||||
protected:
|
||||
View& view;
|
||||
int8_t selected_index;
|
||||
Piece selected_piece;
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "controller/controller.hpp"
|
||||
#include "controller/human_vs_ai.hpp"
|
||||
#include "controller/manual.hpp"
|
||||
#include "view/gui.hpp"
|
||||
#include "view/noop.hpp"
|
||||
@ -10,8 +11,10 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
Board b = Board::setup_fen_position(pos);
|
||||
|
||||
NoOpView gui;
|
||||
ManualController manual(b, gui);
|
||||
ai::v0_random ai;
|
||||
|
||||
GUI gui;
|
||||
HumanVsAIController manual(b, gui, ai);
|
||||
|
||||
|
||||
View& view = gui;
|
||||
|
Loading…
x
Reference in New Issue
Block a user