undo method

PlayedMove? undo()

Undoes the last move.

Returns the last move if undo is successful, otherwise null.

Implementation

PlayedMove? undo() {
  if (!canUndo) return null;

  while (canGoForward) {
    if (_board.safeMove(_gameHistory.forward()) == null) return null;
  }
  final PlayedMove lastMove = _gameHistory.undo();
  if (_board.safeUndoMove(lastMove)) {
    _calculateLegalMoves();
    return lastMove;
  }

  _gameHistory.add(lastMove);
  return null;
}