rollDice method

int rollDice()

Implementation

int rollDice() {
  if (_state.isFinished) throw StateError('Game already finished.');
  if (_state.phase == LudoTurnPhase.awaitingPieceSelection) {
    throw StateError('Select a piece first.');
  }
  if (_isAnimating) throw StateError('Animation in progress.');

  final value = _diceRoller();
  // _audio.playDiceRoll();
  onDiceRolled?.call(value);

  final result = _engine.roll(_state, value);
  _state = result.state;

  if (result.passed) {
    _state = _state.copyWith(clearLastMovedPiece: true);
    onTurnChanged?.call(_state.currentPlayerIndex);
    notifyListeners();
    return value;
  }

  notifyListeners();

  // Auto-move when only one legal option
  if (_state.legalMoves.length == 1) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (_state.phase == LudoTurnPhase.awaitingPieceSelection &&
          _state.legalMoves.length == 1) {
        selectPiece(_state.legalMoves.first.pieceId);
      }
    });
  }

  return value;
}