Exemplo n.º 1
0
  private void addPossiPawnMove(
      Color pawncolor, Position position, State temp_result, ArrayList<Move> temp_position) {
    int row = position.getRow();
    int col = position.getCol();
    Position kingPosition = null;
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        if (temp_result.getPiece(i, j) != null) {
          if (temp_result.getPiece(i, j).equals(new Piece(pawncolor, KING))) {
            kingPosition = new Position(i, j);
            break;
          }
        }
      }
    }
    if (pawncolor == WHITE) {
      if (row < 7) {
        if (row == 1) {
          if ((temp_result.getPiece(row + 2, col) == null)
              && (temp_result.getPiece(row + 1, col) == null)) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(row + 2, col, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              temp_position.add(new Move(position, new Position(row + 2, col), null));
            }
          }
        }
        if (temp_result.getPiece(row + 1, col) == null) {
          State temp = temp_result.copy();
          temp.setPiece(row, col, null);
          temp.setPiece(row + 1, col, temp_result.getPiece(position));
          if (!isUnderCheck(pawncolor, temp, kingPosition)) {
            if ((row + 1) == 7) {
              temp_position.add(new Move(position, new Position(row + 1, col), QUEEN));
              temp_position.add(new Move(position, new Position(row + 1, col), BISHOP));
              temp_position.add(new Move(position, new Position(row + 1, col), KNIGHT));
              temp_position.add(new Move(position, new Position(row + 1, col), ROOK));
            } else temp_position.add(new Move(position, new Position(row + 1, col), null));
          }
        }

        if ((col < 7) && (temp_result.getPiece(row + 1, col + 1) != null)) {
          if (temp_result.getPiece(row + 1, col + 1).getColor().equals(pawncolor.getOpposite())) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(row + 1, col + 1, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              if ((row + 1) == 7) {

                temp_position.add(new Move(position, new Position(row + 1, col + 1), QUEEN));
                temp_position.add(new Move(position, new Position(row + 1, col + 1), BISHOP));
                temp_position.add(new Move(position, new Position(row + 1, col + 1), KNIGHT));
                temp_position.add(new Move(position, new Position(row + 1, col + 1), ROOK));
              } else temp_position.add(new Move(position, new Position(row + 1, col + 1), null));
            }
          }
        } else if ((col < 7) && (temp_result.getPiece(row + 1, col + 1) == null)) {
          if ((temp_result.getEnpassantPosition() != null)
              && (temp_result.getEnpassantPosition().equals(new Position(row, col + 1)))) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(temp_result.getEnpassantPosition(), null);
            temp.setPiece(row + 1, col + 1, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              temp_position.add(new Move(position, new Position(row + 1, col + 1), null));
            }
          }
        }

        if ((col > 0) && (temp_result.getPiece(row + 1, col - 1) != null)) {
          if (temp_result.getPiece(row + 1, col - 1).getColor().equals(pawncolor.getOpposite())) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(row + 1, col - 1, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              if ((row + 1) == 7) {
                temp_position.add(new Move(position, new Position(row + 1, col - 1), QUEEN));
                temp_position.add(new Move(position, new Position(row + 1, col - 1), BISHOP));
                temp_position.add(new Move(position, new Position(row + 1, col - 1), KNIGHT));
                temp_position.add(new Move(position, new Position(row + 1, col - 1), ROOK));
              } else temp_position.add(new Move(position, new Position(row + 1, col - 1), null));
            }
          }
        } else if ((col > 0) && (temp_result.getPiece(row + 1, col - 1) == null)) {
          if ((temp_result.getEnpassantPosition() != null)
              && (temp_result.getEnpassantPosition().equals(new Position(row, col - 1)))) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(temp_result.getEnpassantPosition(), null);
            temp.setPiece(row + 1, col - 1, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              temp_position.add(new Move(position, new Position(row + 1, col - 1), null));
            }
          }
        }
      }

    }
    // black
    else {
      if (row > 0) {
        if (row == 6) {
          if ((temp_result.getPiece(row - 2, col) == null)
              && (temp_result.getPiece(row - 1, col) == null)) {

            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(row - 2, col, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              temp_position.add(new Move(position, new Position(row - 2, col), null));
            }
          }
        }
        if (temp_result.getPiece(row - 1, col) == null) {
          State temp = temp_result.copy();
          temp.setPiece(row, col, null);
          temp.setPiece(row - 1, col, temp_result.getPiece(position));
          if (!isUnderCheck(pawncolor, temp, kingPosition)) {
            if ((row - 1) == 0) {
              temp_position.add(new Move(position, new Position(row - 1, col), QUEEN));
              temp_position.add(new Move(position, new Position(row - 1, col), BISHOP));
              temp_position.add(new Move(position, new Position(row - 1, col), KNIGHT));
              temp_position.add(new Move(position, new Position(row - 1, col), ROOK));
            } else temp_position.add(new Move(position, new Position(row - 1, col), null));
          }
        }

        if ((col < 7) && (temp_result.getPiece(row - 1, col + 1) != null)) {
          if (temp_result.getPiece(row - 1, col + 1).getColor().equals(pawncolor.getOpposite())) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(row - 1, col + 1, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              if ((row - 1) == 0) {
                temp_position.add(new Move(position, new Position(row - 1, col + 1), QUEEN));
                temp_position.add(new Move(position, new Position(row - 1, col + 1), BISHOP));
                temp_position.add(new Move(position, new Position(row - 1, col + 1), KNIGHT));
                temp_position.add(new Move(position, new Position(row - 1, col + 1), ROOK));
              } else temp_position.add(new Move(position, new Position(row - 1, col + 1), null));
            }
          }
        } else if ((col < 7) && (temp_result.getPiece(row - 1, col + 1) == null)) {
          if ((temp_result.getEnpassantPosition() != null)
              && (temp_result.getEnpassantPosition().equals(new Position(row, col + 1)))) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(temp_result.getEnpassantPosition(), null);
            temp.setPiece(row - 1, col + 1, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              temp_position.add(new Move(position, new Position(row - 1, col + 1), null));
            }
          }
        }

        if ((col > 0) && (temp_result.getPiece(row - 1, col - 1) != null)) {
          if (temp_result.getPiece(row - 1, col - 1).getColor().equals(pawncolor.getOpposite())) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(row - 1, col - 1, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {

              if ((row - 1) == 0) {
                temp_position.add(new Move(position, new Position(row - 1, col - 1), QUEEN));
                temp_position.add(new Move(position, new Position(row - 1, col - 1), BISHOP));
                temp_position.add(new Move(position, new Position(row - 1, col - 1), KNIGHT));
                temp_position.add(new Move(position, new Position(row - 1, col - 1), ROOK));
              } else temp_position.add(new Move(position, new Position(row - 1, col - 1), null));
            }
          }
        } else if ((col > 0) && (temp_result.getPiece(row - 1, col - 1) == null)) {
          if ((temp_result.getEnpassantPosition() != null)
              && (temp_result.getEnpassantPosition().equals(new Position(row, col - 1)))) {
            State temp = temp_result.copy();
            temp.setPiece(row, col, null);
            temp.setPiece(temp_result.getEnpassantPosition(), null);
            temp.setPiece(row - 1, col - 1, temp_result.getPiece(position));
            if (!isUnderCheck(pawncolor, temp, kingPosition)) {
              temp_position.add(new Move(position, new Position(row - 1, col - 1), null));
            }
          }
        }
      }
    }
  }