getCheckMate method

List<String> getCheckMate(
  1. int team, [
  2. int depth = 10
])

TODO 获取杀招 ,连将,连将过程中局面不会复原,不会被解将

Implementation

List<String> getCheckMate(int team, [int depth = 10]) {
  List<String> moves = [];

  List<ChessItem> pieces = fen.getAll();
  ChessPos kPos = fen.find(team == 0 ? 'k' : 'K')!;

  // 正在将军,直接查出吃老将的招法
  int enemyTeam = team == 0 ? 1 : 0;
  if (isCheck(enemyTeam)) {
    for (final item in pieces) {
      if (item.team == team) {
        List<String> moves = movePoints(item.position, kPos);
        if (moves.contains(kPos.toCode())) {
          moves.add(item.position.toCode() + kPos.toCode());
          return moves;
        }
      }
    }
  }

  final currentFen = fen.copy();
  ChessFen startFen = currentFen;

  int curDepth = 0;

  // 获取连将的杀着
  while (true) {
    moves.add('');
    final checkMoves = ChessRule(startFen).getCheckMoves(team);
    for (final move in checkMoves) {
      moves[curDepth] = move;
      ChessFen fend = currentFen.copy();
      fend.move(move);
      if (!ChessRule(fend).canParryKill(team == 0 ? 1 : 0)) {
        return moves;
      }
      currentFen.deductions.add(fend);
      startFen = fend;
    }

    curDepth++;
    if (curDepth >= depth) {
      break;
    }
  }

  return moves;
}