From 74e4d596a7bafacd69ae30594c268978d8592c36 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Thu, 6 Feb 2025 20:12:12 +0100 Subject: [PATCH] very easily implemented the human_vs_ai controller (god i love a good design) --- cpp/src/controller/human_vs_ai.cpp | 32 ++++++++++++++++++++++++++++++ cpp/src/controller/human_vs_ai.hpp | 14 +++++++++++++ cpp/src/controller/manual.hpp | 2 +- cpp/src/main.cpp | 7 +++++-- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 cpp/src/controller/human_vs_ai.cpp create mode 100644 cpp/src/controller/human_vs_ai.hpp diff --git a/cpp/src/controller/human_vs_ai.cpp b/cpp/src/controller/human_vs_ai.cpp new file mode 100644 index 0000000..4339a85 --- /dev/null +++ b/cpp/src/controller/human_vs_ai.cpp @@ -0,0 +1,32 @@ +#include "human_vs_ai.hpp" + +#include + +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(); + } +} diff --git a/cpp/src/controller/human_vs_ai.hpp b/cpp/src/controller/human_vs_ai.hpp new file mode 100644 index 0000000..2adfa57 --- /dev/null +++ b/cpp/src/controller/human_vs_ai.hpp @@ -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; +}; diff --git a/cpp/src/controller/manual.hpp b/cpp/src/controller/manual.hpp index 2b349e2..8940ce8 100644 --- a/cpp/src/controller/manual.hpp +++ b/cpp/src/controller/manual.hpp @@ -6,7 +6,7 @@ #include "controller.hpp" class ManualController : public Controller { - private: + protected: View& view; int8_t selected_index; Piece selected_piece; diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index a7e9be3..59bf3eb 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -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;