From f86e1b872add9a7c4d27923fc440bd753147671f Mon Sep 17 00:00:00 2001 From: sebastian Date: Wed, 5 Aug 2026 18:46:38 +0200 Subject: [PATCH] UPD: Move getMoveablePieceIndices to LudoRules --- game/src/ludo/LudoAiController.cpp | 2 +- game/src/ludo/LudoGameMode.cpp | 27 ++------------------------- game/src/ludo/LudoGameMode.h | 1 - game/src/ludo/gameMode/LudoRules.cpp | 23 +++++++++++++++++++++++ game/src/ludo/gameMode/LudoRules.h | 3 +++ 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/game/src/ludo/LudoAiController.cpp b/game/src/ludo/LudoAiController.cpp index 69ccbd6..d900c83 100644 --- a/game/src/ludo/LudoAiController.cpp +++ b/game/src/ludo/LudoAiController.cpp @@ -39,7 +39,7 @@ void ludo::LudoAiController::update(float deltaTime) { std::optional ludo::LudoAiController::chooseMove() const { auto color = m_gameMode->getColorOf(m_player); - auto moveable = m_gameMode->getMoveablePieceIndices(color, m_gameMode->getState().getDiceValue()); + auto moveable = LudoRules::getMoveablePieceIndices(m_gameMode->getState(), color, m_gameMode->getState().getDiceValue()); if (moveable.empty()) return std::nullopt; int diceValue = m_gameMode->getState().getDiceValue(); diff --git a/game/src/ludo/LudoGameMode.cpp b/game/src/ludo/LudoGameMode.cpp index 2b0b931..86b69a8 100644 --- a/game/src/ludo/LudoGameMode.cpp +++ b/game/src/ludo/LudoGameMode.cpp @@ -87,7 +87,7 @@ void ludo::LudoGameMode::handle(const RollDiceCommand &command) { } } - if (getMoveablePieceIndices(getColorOf(command.player), m_gameState.m_diceValue).empty()) { + if (LudoRules::getMoveablePieceIndices(m_gameState, getColorOf(command.player), m_gameState.m_diceValue).empty()) { finishTurn(); // kein gültiger zug, sofort weitergeben } else { m_gameState.m_phase = TurnPhase::AwaitingPieceSelection; @@ -111,7 +111,7 @@ void ludo::LudoGameMode::handle(const MovePieceCommand &command) { return; } - auto movable = getMoveablePieceIndices(getColorOf(command.player), m_gameState.m_diceValue); + auto movable = LudoRules::getMoveablePieceIndices(m_gameState, getColorOf(command.player), m_gameState.m_diceValue); if (std::find(movable.begin(), movable.end(), command.pieceIndex) == movable.end()) { spdlog::warn("MovePieceCommand abgewiesen: pieceIndex={} nicht in movable-Liste", command.pieceIndex); return; @@ -149,29 +149,6 @@ void ludo::LudoGameMode::finishTurn() { publishEvent(TurnChangedEvent{getCurrentPlayer()}); } -std::vector ludo::LudoGameMode::getMoveablePieceIndices(PlayerColor color, int diceValue) const { - std::vector moveablePieceIndices; - for (int i = 0; i < m_gameState.getPieces().size(); ++i) { - const Piece& piece = m_gameState.getPieces()[i]; - if (piece.color != color) continue; - - Destination dest = LudoRules::computeDestination(m_gameState, piece, diceValue).value(); - - // Überschuss in der Zielgeraden abfangen: computeDestination liefert bei Überschuss - // aktuell denselben Zustand zurück, wie unten in computeDestination definiert - if (piece.state == PieceState::InHomeStretch && dest.position > kHomeStretchLength - 1) continue; - - if (piece.state == PieceState::AtHome && diceValue != 6) { - continue; - } - - if (!LudoRules::isBlockedByOwnPiece(m_gameState, color, dest.state, dest.position)) { - moveablePieceIndices.push_back(i); - } - } - return moveablePieceIndices; -} - int ludo::LudoGameMode::rollDice() { return m_diceSource->roll(); diff --git a/game/src/ludo/LudoGameMode.h b/game/src/ludo/LudoGameMode.h index b301432..048caf4 100644 --- a/game/src/ludo/LudoGameMode.h +++ b/game/src/ludo/LudoGameMode.h @@ -27,7 +27,6 @@ namespace ludo { [[nodiscard]] const LudoGameState& getState() const override { return m_gameState; } [[nodiscard]] PlayerColor getColorOf(engine::PlayerID playerID) const; - std::vector getMoveablePieceIndices(PlayerColor color, 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 9d85647..0189018 100644 --- a/game/src/ludo/gameMode/LudoRules.cpp +++ b/game/src/ludo/gameMode/LudoRules.cpp @@ -59,3 +59,26 @@ std::optional ludo::LudoRules::wouldCapture(const LudoGameState &state, int } return std::nullopt; } + +std::vector ludo::LudoRules::getMoveablePieceIndices(const LudoGameState &state, PlayerColor color, int diceValue) { + std::vector moveablePieceIndices; + for (int i = 0; i < state.getPieces().size(); ++i) { + const Piece& piece = state.getPieces()[i]; + if (piece.color != color) continue; + + Destination dest = LudoRules::computeDestination(state, piece, diceValue).value(); + + // Überschuss in der Zielgeraden abfangen: computeDestination liefert bei Überschuss + // aktuell denselben Zustand zurück, wie unten in computeDestination definiert + if (piece.state == PieceState::InHomeStretch && dest.position > kHomeStretchLength - 1) continue; + + if (piece.state == PieceState::AtHome && diceValue != 6) { + continue; + } + + if (!LudoRules::isBlockedByOwnPiece(state, color, dest.state, dest.position)) { + moveablePieceIndices.push_back(i); + } + } + return moveablePieceIndices; +} diff --git a/game/src/ludo/gameMode/LudoRules.h b/game/src/ludo/gameMode/LudoRules.h index 22f167e..3071fe6 100644 --- a/game/src/ludo/gameMode/LudoRules.h +++ b/game/src/ludo/gameMode/LudoRules.h @@ -30,6 +30,9 @@ namespace ludo { static std::optional wouldCapture(const LudoGameState& state, int pieceIndex, int diceValue); + + static std::vector getMoveablePieceIndices(const LudoGameState& state, PlayerColor color, int diceValue); + };