UPD: Move wouldCapture to LudoRules
This commit is contained in:
parent
1c8a6840e6
commit
72c7db8439
@ -8,6 +8,8 @@
|
||||
#include <ostream>
|
||||
#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){
|
||||
}
|
||||
|
||||
@ -44,7 +46,7 @@ std::optional<int> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,21 +172,6 @@ std::vector<int> ludo::LudoGameMode::getMoveablePieceIndices(PlayerColor color,
|
||||
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() {
|
||||
return m_diceSource->roll();
|
||||
|
||||
@ -28,7 +28,6 @@ namespace ludo {
|
||||
[[nodiscard]] PlayerColor getColorOf(engine::PlayerID playerID) 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);
|
||||
|
||||
|
||||
|
||||
@ -44,3 +44,18 @@ bool ludo::LudoRules::isBlockedByOwnPiece(const LudoGameState &state, PlayerColo
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@ -28,6 +28,8 @@ namespace ludo {
|
||||
|
||||
static bool isBlockedByOwnPiece(const LudoGameState& state, PlayerColor owner, PieceState pieceState, int position);
|
||||
|
||||
static std::optional<int> wouldCapture(const LudoGameState& state, int pieceIndex, int diceValue);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user