Ejemplo n.º 1
0
  /**
   * Constructor Initialize instance variables.
   *
   * @param name The name of this Piece
   * @param lightIcon The Icon representing the white version of Piece in the GUI
   * @param darkIcon The Icon representing the black version of Piece in the GUI
   * @param isBlack The color of this Piece
   * @param curSquare The Square this Piece occupies
   * @param board The Board this Piece occupies
   * @param movements Map of legal movements for this Piece
   */
  public Piece(
      String name,
      boolean isBlack,
      Square curSquare,
      Board board,
      Map<Character, Integer> movements) {
    m_name = name;
    m_lightIcon = ImageUtility.getLightImage(name);
    m_darkIcon = ImageUtility.getDarkImage(name);
    setBlack(isBlack);
    m_curSquare = curSquare;
    setOriginalSquare(curSquare);
    // Tell the Square what Piece is on it.
    curSquare.setPiece(this);
    m_board = board;
    m_movements = movements;

    List<Square> legalDests = Lists.newArrayList();
    setLegalDests(legalDests);

    List<Square> guardSquares = Lists.newArrayList();
    setGuardSquares(guardSquares);
    setMoveCount(0);
  }
Ejemplo n.º 2
0
  private Piece classicPromotion(Piece pieceToPromote, String pieceTypeToPromoteFrom) {
    if (pieceToPromote.getPromotesTo() == null || pieceToPromote.getPromotesTo().size() == 0) {
      m_lastPromotedFromPieceName = pieceToPromote.getName();
      mPromotedToClass = pieceToPromote.getName();
      return pieceToPromote;
    } else if (pieceTypeToPromoteFrom != null
        && !pieceTypeToPromoteFrom.equals(pieceToPromote.getName())) {
      // we don't want to promote the objective pieces. That makes things
      // weird...
      if ((pieceToPromote.isBlack()
              && !mGame.getBlackRules().getObjectiveName().equals(pieceToPromote.getName()))
          || (!pieceToPromote.isBlack()
              && !mGame.getWhiteRules().getObjectiveName().equals(pieceToPromote.getName()))) {
        try {
          Piece promoted =
              PieceBuilder.makePiece(
                  pieceTypeToPromoteFrom,
                  pieceToPromote.isBlack(),
                  pieceToPromote.getSquare(),
                  pieceToPromote.getBoard());
          if (promoted.isBlack())
            mGame.getBlackTeam().set(mGame.getBlackTeam().indexOf(pieceToPromote), promoted);
          else mGame.getWhiteTeam().set(mGame.getWhiteTeam().indexOf(pieceToPromote), promoted);
          promoted.getLegalDests().clear();
          promoted.setMoveCount(pieceToPromote.getMoveCount());
          return promoted;
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } else if (pieceTypeToPromoteFrom == null && mGame.isBlackMove() == pieceToPromote.isBlack()) {
      mPromotedToClass = ""; // $NON-NLS-1$
      if ((!pieceToPromote.isBlack() && pieceToPromote.getPromotesTo().size() == 1)
          || pieceToPromote.isBlack() && pieceToPromote.getPromotesTo().size() == 1)
        mPromotedToClass = pieceToPromote.getPromotesTo().get(0);
      while (mPromotedToClass.equals("")) // $NON-NLS-1$
      {
        List<String> promotion =
            pieceToPromote.isBlack()
                ? pieceToPromote.getPromotesTo()
                : pieceToPromote.getPromotesTo();
        String result =
            (String)
                JOptionPane.showInputDialog(
                    null,
                    Messages.getString("selectPromotionType"),
                    Messages.getString("promoChoice"), // $NON-NLS-1$ //$NON-NLS-2$
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    promotion.toArray(),
                    null);

        if (result == null) continue;

        mPromotedToClass = result;
        pieceTypeToPromoteFrom = result;
      }
    } else if (pieceTypeToPromoteFrom != null
        && !pieceToPromote.isBlack()
        && pieceToPromote.getPromotesTo() != null
        && pieceToPromote.getPromotesTo().contains(pieceTypeToPromoteFrom)) {
      mPromotedToClass = pieceTypeToPromoteFrom;
    } else if (pieceTypeToPromoteFrom != null
        && pieceToPromote.isBlack()
        && pieceToPromote.getPromotesTo() != null
        && pieceToPromote.getPromotesTo().contains(pieceTypeToPromoteFrom)) {
      mPromotedToClass = pieceTypeToPromoteFrom;
    }

    try {
      Piece promoted =
          PieceBuilder.makePiece(
              mPromotedToClass,
              pieceToPromote.isBlack(),
              pieceToPromote.getSquare(),
              pieceToPromote.getBoard());
      if (promoted.isBlack())
        mGame.getBlackTeam().set(mGame.getBlackTeam().indexOf(pieceToPromote), promoted);
      else mGame.getWhiteTeam().set(mGame.getWhiteTeam().indexOf(pieceToPromote), promoted);
      promoted.getLegalDests().clear();
      promoted.setMoveCount(pieceToPromote.getMoveCount());
      return promoted;
    } catch (Exception e) {
      e.printStackTrace();
      return pieceToPromote;
    }
  }