toICString method

String toICString()

Creates a String representation of a game. This representation contains in each line a pair of moves of white and black player, separated by ' ' character and game metadata at the start.

Implementation

String toICString() {
  String icString = '';

  icString += '$id\n';
  icString += '${whitePlayer.id} ${blackPlayer.id}\n';
  icString +=
      '${timeControl.initialTime.inSeconds} ${timeControl.incrementPerMove.inSeconds}\n';
  icString +=
      '${remainingTimeWhite.inMilliseconds} ${remainingTimeBlack.inMilliseconds}\n';
  if (timesSpentPerMove.isNotEmpty) {
    icString += '${timesSpentPerMove.first.inMilliseconds}';
    for (int i = 1; i < timesSpentPerMove.length; i++) {
      icString += ' ${timesSpentPerMove[i].inMilliseconds}';
    }
  }
  icString += '\n';
  icString += board.getFenRepresentation() + '\n';

  int moveIndex = 0;
  for (; moveIndex + 1 < movesPlayed.length; moveIndex += 2) {
    icString +=
        '${movesPlayed[moveIndex].toICString()} ${movesPlayed[moveIndex + 1].toICString()}\n';
  }

  int futureMoveIndex = movesFromFuture.length - 1;

  if (moveIndex < movesPlayed.length) {
    // moveIndex == game.movesPlayed.length - 1
    icString += '${movesPlayed[moveIndex].toICString()}\t';
    if (futureMoveIndex != -1) {
      icString += '${movesFromFuture[futureMoveIndex].toICString()}\n';
      futureMoveIndex--;
    }
  }

  for (; futureMoveIndex - 1 >= 0; futureMoveIndex -= 2) {
    icString +=
        '${movesPlayed[futureMoveIndex].toICString()} ${movesPlayed[futureMoveIndex - 1].toICString()}\n';
  }
  if (futureMoveIndex == 0) {
    icString += '${movesPlayed[futureMoveIndex].toICString()} ';
  }

  return icString;
}