roll method

LudoEngineRollResult roll(
  1. LudoGameState state,
  2. int diceValue
)

Implementation

LudoEngineRollResult roll(LudoGameState state, int diceValue) {
  if (state.isFinished) {
    throw StateError('Game already finished.');
  }
  if (state.phase != LudoTurnPhase.awaitingRoll) {
    throw StateError('Cannot roll while a piece selection is pending.');
  }
  if (diceValue < 1 || diceValue > 6) {
    throw RangeError.range(diceValue, 1, 6, 'diceValue');
  }

  final isExtraValue = diceRules.grantsExtraTurn(diceValue);
  final streak = isExtraValue ? state.rollStreak + 1 : 0;
  final rolled = state.copyWith(
    lastRoll: diceValue,
    lastRollPlayerIndex: state.currentPlayerIndex,
    rollCount: state.rollCount + 1,
  );

  final limit = diceRules.forfeitStreak;
  if (isExtraValue && limit != null && streak >= limit) {
    return LudoEngineRollResult(
      state: _passTurn(rolled),
      passed: true,
      forfeited: true,
    );
  }

  final moves = computeLegalMoves(state, diceRules, diceValue, teams: teams);

  if (moves.isEmpty) {
    return LudoEngineRollResult(state: _passTurn(rolled), passed: true);
  }

  return LudoEngineRollResult(
    state: rolled.copyWith(
      diceValue: diceValue,
      legalMoves: List.unmodifiable(moves),
      phase: LudoTurnPhase.awaitingPieceSelection,
      rollStreak: streak,
    ),
    passed: false,
  );
}