diff --git a/game/src/ludo/LudoAiController.cpp b/game/src/ludo/LudoAiController.cpp index 0b0f0de..69ccbd6 100644 --- a/game/src/ludo/LudoAiController.cpp +++ b/game/src/ludo/LudoAiController.cpp @@ -8,6 +8,8 @@ #include #include +#include "gameMode/LudoRules.h" + ludo::LudoAiController::LudoAiController(std::shared_ptr gameMode, engine::PlayerID player) : m_gameMode(std::move(gameMode)), m_player(player){ } @@ -44,7 +46,7 @@ std::optional ludo::LudoAiController::chooseMove() const { //Priority 1: Schmeißen for (int idx : moveable) { - if (m_gameMode->wouldCapture(idx, diceValue).has_value()) { + if (LudoRules::wouldCapture(m_gameMode->getState(), idx, diceValue).has_value()) { return idx; } } diff --git a/game/src/ludo/LudoGameMode.cpp b/game/src/ludo/LudoGameMode.cpp index 01b2b84..2b0b931 100644 --- a/game/src/ludo/LudoGameMode.cpp +++ b/game/src/ludo/LudoGameMode.cpp @@ -172,21 +172,6 @@ std::vector ludo::LudoGameMode::getMoveablePieceIndices(PlayerColor color, return moveablePieceIndices; } -std::optional ludo::LudoGameMode::wouldCapture(int pieceIndex, int diceValue) const { - const auto& piece = m_gameState.m_pieces[pieceIndex]; - auto dest = LudoRules::computeDestination(m_gameState, piece, diceValue); - if (dest.value().state != PieceState::OnTrack) return std::nullopt; - - for (int i = 0; i < static_cast(m_gameState.m_pieces.size()); ++i) { - if (i == pieceIndex) continue; - const auto& other = m_gameState.m_pieces[i]; - if (other.color != piece.color && other.state == PieceState::OnTrack && other.position == dest.value().position) { - return i; - } - } - return std::nullopt; -} - int ludo::LudoGameMode::rollDice() { return m_diceSource->roll(); diff --git a/game/src/ludo/LudoGameMode.h b/game/src/ludo/LudoGameMode.h index b358223..b301432 100644 --- a/game/src/ludo/LudoGameMode.h +++ b/game/src/ludo/LudoGameMode.h @@ -28,7 +28,6 @@ namespace ludo { [[nodiscard]] PlayerColor getColorOf(engine::PlayerID playerID) const; std::vector getMoveablePieceIndices(PlayerColor color, int diceValue) const; - std::optional wouldCapture(int pieceIndex, int diceValue) const; [[nodiscard]] static int getEntryOffset(PlayerColor color); diff --git a/game/src/ludo/gameMode/LudoRules.cpp b/game/src/ludo/gameMode/LudoRules.cpp index 80ad78f..9d85647 100644 --- a/game/src/ludo/gameMode/LudoRules.cpp +++ b/game/src/ludo/gameMode/LudoRules.cpp @@ -44,3 +44,18 @@ bool ludo::LudoRules::isBlockedByOwnPiece(const LudoGameState &state, PlayerColo } return false; } + +std::optional ludo::LudoRules::wouldCapture(const LudoGameState &state, int pieceIndex, int diceValue) { + const auto& piece = state.getPieces()[pieceIndex]; + auto dest = LudoRules::computeDestination(state, piece, diceValue); + if (dest.value().state != PieceState::OnTrack) return std::nullopt; + + for (int i = 0; i < static_cast(state.getPieces().size()); ++i) { + if (i == pieceIndex) continue; + const auto& other = state.getPieces()[i]; + if (other.color != piece.color && other.state == PieceState::OnTrack && other.position == dest.value().position) { + return i; + } + } + return std::nullopt; +} diff --git a/game/src/ludo/gameMode/LudoRules.h b/game/src/ludo/gameMode/LudoRules.h index 1d1cd6c..22f167e 100644 --- a/game/src/ludo/gameMode/LudoRules.h +++ b/game/src/ludo/gameMode/LudoRules.h @@ -28,6 +28,8 @@ namespace ludo { static bool isBlockedByOwnPiece(const LudoGameState& state, PlayerColor owner, PieceState pieceState, int position); + static std::optional wouldCapture(const LudoGameState& state, int pieceIndex, int diceValue); + };