UPD: Move wouldCapture to LudoRules

This commit is contained in:
sebastian 2026-08-05 18:44:14 +02:00
parent 1c8a6840e6
commit 72c7db8439
5 changed files with 20 additions and 17 deletions

View File

@ -8,6 +8,8 @@
#include <ostream> #include <ostream>
#include <utility> #include <utility>
#include "gameMode/LudoRules.h"
ludo::LudoAiController::LudoAiController(std::shared_ptr<LudoGameMode> gameMode, engine::PlayerID player) : m_gameMode(std::move(gameMode)), m_player(player){ ludo::LudoAiController::LudoAiController(std::shared_ptr<LudoGameMode> gameMode, engine::PlayerID player) : m_gameMode(std::move(gameMode)), m_player(player){
} }
@ -44,7 +46,7 @@ std::optional<int> ludo::LudoAiController::chooseMove() const {
//Priority 1: Schmeißen //Priority 1: Schmeißen
for (int idx : moveable) { for (int idx : moveable) {
if (m_gameMode->wouldCapture(idx, diceValue).has_value()) { if (LudoRules::wouldCapture(m_gameMode->getState(), idx, diceValue).has_value()) {
return idx; return idx;
} }
} }

View File

@ -172,21 +172,6 @@ std::vector<int> ludo::LudoGameMode::getMoveablePieceIndices(PlayerColor color,
return moveablePieceIndices; return moveablePieceIndices;
} }
std::optional<int> 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<int>(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() { int ludo::LudoGameMode::rollDice() {
return m_diceSource->roll(); return m_diceSource->roll();

View File

@ -28,7 +28,6 @@ namespace ludo {
[[nodiscard]] PlayerColor getColorOf(engine::PlayerID playerID) const; [[nodiscard]] PlayerColor getColorOf(engine::PlayerID playerID) const;
std::vector<int> getMoveablePieceIndices(PlayerColor color, int diceValue) const; std::vector<int> getMoveablePieceIndices(PlayerColor color, int diceValue) const;
std::optional<int> wouldCapture(int pieceIndex, int diceValue) const;
[[nodiscard]] static int getEntryOffset(PlayerColor color); [[nodiscard]] static int getEntryOffset(PlayerColor color);

View File

@ -44,3 +44,18 @@ bool ludo::LudoRules::isBlockedByOwnPiece(const LudoGameState &state, PlayerColo
} }
return false; return false;
} }
std::optional<int> 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<int>(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;
}

View File

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