Exemplo n.º 1
0
  private void addPossiKingMove(
      Color kingcolor, Position position, State temp_result, ArrayList<Move> temp_position) {

    int row = position.getRow();
    int col = position.getCol();
    Position[] possiKingPosition =
        new Position[] {
          new Position(row + 1, col), new Position(row - 1, col),
          new Position(row, col + 1), new Position(row, col - 1),
          new Position(row + 1, col + 1), new Position(row - 1, col - 1),
          new Position(row - 1, col + 1), new Position(row + 1, col - 1)
        };

    for (int l = 0; l < possiKingPosition.length; l++) {
      int i = possiKingPosition[l].getRow();
      int j = possiKingPosition[l].getCol();

      if ((i < 0) || (i > 7) || (j < 0) || (j > 7)) {
        continue;
      } else {
        if ((temp_result.getPiece(possiKingPosition[l]) == null)
            || ((temp_result.getPiece(possiKingPosition[l]) != null)
                && (temp_result
                    .getPiece(possiKingPosition[l])
                    .getColor()
                    .equals(kingcolor.getOpposite())))) {
          State temp = temp_result.copy();
          temp.setPiece(position, null);
          temp.setPiece(possiKingPosition[l], temp_result.getPiece(position));
          if (!isUnderCheck(kingcolor, temp, possiKingPosition[l])) {
            temp_position.add(new Move(position, possiKingPosition[l], null));
          }
        }
      }
    }

    if ((kingcolor.equals(WHITE)) && (temp_result.isCanCastleKingSide(kingcolor))) {
      boolean isOccupied = false;
      Position[] positionNeedCheck = new Position[] {new Position(0, 5), new Position(0, 6)};
      for (int i = 0; i < positionNeedCheck.length; i++) {
        if (temp_result.getPiece(positionNeedCheck[i]) != null) {
          isOccupied = true;
          break;
        }
      }
      boolean isChecked = false;
      if (!isOccupied) {
        Position[] positionNeedCheck1 =
            new Position[] {new Position(0, 4), new Position(0, 5), new Position(0, 6)};
        for (int i = 0; i < positionNeedCheck1.length; i++) {
          State temp = temp_result.copy();
          temp.setPiece(position, null);
          temp.setPiece(positionNeedCheck1[i], new Piece(kingcolor, KING));
          isChecked = isUnderCheck(kingcolor, temp, positionNeedCheck1[i]);
          if (isChecked) {
            break;
          }
        }
      }

      if ((!isOccupied) && (!isChecked)) {
        temp_position.add(new Move(position, new Position(0, 6), null));
      }
    }

    if ((kingcolor.equals(WHITE)) && (temp_result.isCanCastleQueenSide(kingcolor))) {
      boolean isOccupied = false;
      Position[] positionNeedCheck =
          new Position[] {new Position(0, 3), new Position(0, 2), new Position(0, 1)};
      for (int i = 0; i < positionNeedCheck.length; i++) {
        if (temp_result.getPiece(positionNeedCheck[i]) != null) {
          isOccupied = true;
          break;
        }
      }
      boolean isChecked = false;
      if (!isOccupied) {
        Position[] positionNeedCheck1 =
            new Position[] {new Position(0, 4), new Position(0, 3), new Position(0, 2)};
        for (int i = 0; i < positionNeedCheck1.length; i++) {
          State temp = temp_result.copy();
          temp.setPiece(position, null);
          temp.setPiece(positionNeedCheck1[i], new Piece(kingcolor, KING));
          isChecked = isUnderCheck(kingcolor, temp, positionNeedCheck1[i]);
          if (isChecked) {
            break;
          }
        }
      }

      if ((!isOccupied) && (!isChecked)) {
        temp_position.add(new Move(position, new Position(0, 2), null));
      }
    }

    if ((kingcolor.equals(BLACK)) && (temp_result.isCanCastleKingSide(kingcolor))) {
      boolean isOccupied = false;
      Position[] positionNeedCheck = new Position[] {new Position(7, 5), new Position(7, 6)};
      for (int i = 0; i < positionNeedCheck.length; i++) {
        if (temp_result.getPiece(positionNeedCheck[i]) != null) {
          isOccupied = true;
          break;
        }
      }
      boolean isChecked = false;
      if (!isOccupied) {
        Position[] positionNeedCheck1 =
            new Position[] {new Position(7, 4), new Position(7, 5), new Position(7, 6)};
        for (int i = 0; i < positionNeedCheck1.length; i++) {
          State temp = temp_result.copy();
          temp.setPiece(position, null);
          temp.setPiece(positionNeedCheck1[i], new Piece(kingcolor, KING));
          isChecked = isUnderCheck(kingcolor, temp, positionNeedCheck1[i]);
          if (isChecked) {
            break;
          }
        }
      }

      if ((!isOccupied) && (!isChecked)) {
        temp_position.add(new Move(position, new Position(7, 6), null));
      }
    }
    if ((kingcolor.equals(BLACK)) && (temp_result.isCanCastleQueenSide(kingcolor))) {
      boolean isOccupied = false;
      Position[] positionNeedCheck =
          new Position[] {new Position(7, 3), new Position(7, 2), new Position(7, 1)};
      for (int i = 0; i < positionNeedCheck.length; i++) {
        if (temp_result.getPiece(positionNeedCheck[i]) != null) {
          isOccupied = true;
          break;
        }
      }
      boolean isChecked = false;
      if (!isOccupied) {
        Position[] positionNeedCheck1 =
            new Position[] {new Position(7, 4), new Position(7, 3), new Position(7, 2)};
        for (int i = 0; i < positionNeedCheck1.length; i++) {
          State temp = temp_result.copy();
          temp.setPiece(position, null);
          temp.setPiece(positionNeedCheck1[i], new Piece(kingcolor, KING));
          isChecked = isUnderCheck(kingcolor, temp, positionNeedCheck1[i]);
          if (isChecked) {
            break;
          }
        }
      }

      if ((!isOccupied) && (!isChecked)) {
        temp_position.add(new Move(position, new Position(7, 2), null));
      }
    }
  }