UPD: Move getMoveablePieceIndices to LudoRules

This commit is contained in:
sebastian 2026-08-05 18:46:38 +02:00
parent 72c7db8439
commit f86e1b872a
5 changed files with 29 additions and 27 deletions

View File

@ -39,7 +39,7 @@ void ludo::LudoAiController::update(float deltaTime) {
std::optional<int> 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();

View File

@ -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<int> ludo::LudoGameMode::getMoveablePieceIndices(PlayerColor color, int diceValue) const {
std::vector<int> 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();

View File

@ -27,7 +27,6 @@ namespace ludo {
[[nodiscard]] const LudoGameState& getState() const override { return m_gameState; }
[[nodiscard]] PlayerColor getColorOf(engine::PlayerID playerID) const;
std::vector<int> getMoveablePieceIndices(PlayerColor color, int diceValue) const;
[[nodiscard]] static int getEntryOffset(PlayerColor color);

View File

@ -59,3 +59,26 @@ std::optional<int> ludo::LudoRules::wouldCapture(const LudoGameState &state, int
}
return std::nullopt;
}
std::vector<int> ludo::LudoRules::getMoveablePieceIndices(const LudoGameState &state, PlayerColor color, int diceValue) {
std::vector<int> 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;
}

View File

@ -30,6 +30,9 @@ namespace ludo {
static std::optional<int> wouldCapture(const LudoGameState& state, int pieceIndex, int diceValue);
static std::vector<int> getMoveablePieceIndices(const LudoGameState& state, PlayerColor color, int diceValue);
};