UPD: Move isBlockedByOwnPiece to LudoRules

This commit is contained in:
sebastian 2026-08-05 18:42:01 +02:00
parent 7426374b00
commit 1c8a6840e6
4 changed files with 13 additions and 14 deletions

View File

@ -79,7 +79,7 @@ void ludo::LudoGameMode::handle(const RollDiceCommand &command) {
if (m_gameState.m_diceValue == 6) {
if (auto homeIndex = findLowestHomeSlotPiece(getColorOf(command.player))) {
if (!isBlockedByOwnPiece(getColorOf(command.player), PieceState::OnTrack, getEntryOffset(getColorOf(command.player)))) {
if (!LudoRules::isBlockedByOwnPiece(m_gameState, getColorOf(command.player), PieceState::OnTrack, getEntryOffset(getColorOf(command.player)))) {
autoReleasePiece(*homeIndex);
finishTurn();
return;
@ -165,7 +165,7 @@ std::vector<int> ludo::LudoGameMode::getMoveablePieceIndices(PlayerColor color,
continue;
}
if (!isBlockedByOwnPiece(color, dest.state, dest.position)) {
if (!LudoRules::isBlockedByOwnPiece(m_gameState, color, dest.state, dest.position)) {
moveablePieceIndices.push_back(i);
}
}
@ -196,15 +196,6 @@ int ludo::LudoGameMode::getEntryOffset(PlayerColor color) {
return static_cast<int>(color) * 10;
}
bool ludo::LudoGameMode::isBlockedByOwnPiece(PlayerColor owner, PieceState state, int position) const {
for (const auto& piece : m_gameState.getPieces()) {
if (piece.color == owner && piece.state == state && piece.position == position) {
return true;
}
}
return false;
}
void ludo::LudoGameMode::publishEvent(LudoEvent event) {
m_pendingEvents.push_back(event);
m_eventBus.publish(event);

View File

@ -60,9 +60,6 @@ namespace ludo {
int rollDice();
std::unique_ptr<IDiceSource> m_diceSource;
[[nodiscard]] bool isBlockedByOwnPiece(PlayerColor owner, PieceState state, int position) const;
void publishEvent(LudoEvent event);
};
}

View File

@ -35,3 +35,12 @@ std::optional<ludo::Destination> ludo::LudoRules::computeDestination(const LudoG
}
return std::nullopt;
}
bool ludo::LudoRules::isBlockedByOwnPiece(const LudoGameState &state, PlayerColor owner, PieceState pieceState, int position) {
for (const auto& piece : state.getPieces()) {
if (piece.color == owner && piece.state == pieceState && piece.position == position) {
return true;
}
}
return false;
}

View File

@ -26,6 +26,8 @@ namespace ludo {
static std::optional<Destination> computeDestination(const LudoGameState& state,
const Piece& piece, int diceValue);
static bool isBlockedByOwnPiece(const LudoGameState& state, PlayerColor owner, PieceState pieceState, int position);
};