in_threefold_repetition property

bool get in_threefold_repetition

Is it draw by threefold repetition in current position ?

Implementation

bool get in_threefold_repetition {
  /* TODO: while this function is fine for casual use, a better
   * implementation would use a Zobrist key (instead of FEN). the
   * Zobrist key would be maintained in the make_move/undo_move functions,
   * avoiding the costly that we do below.
   */
  final positions = {};
  var moves = [];
  var repetition = false;

  while (true) {
    var move = undo_move();
    if (move == null) {
      break;
    }
    moves.add(move);
  }

  while (true) {
    /* remove the last two fields in the FEN string, they're not needed
     * when checking for draw by rep */
    var fen = generate_fen().split(' ').sublist(0, 4).join(' ');

    /* has the position occurred three or move times */
    positions[fen] = (positions.containsKey(fen)) ? positions[fen] + 1 : 1;
    if (positions[fen] >= 3) {
      repetition = true;
    }

    if (moves.isEmpty) {
      break;
    }
    make_move(moves.removeLast());
  }

  return repetition;
}