Exemple #1
0
  /**
   * Generate legal destinations that will save the King piece from check
   *
   * @param king The King piece
   * @param threat The Piece threatening the King piece
   */
  protected void genLegalDestsSaveKing(Piece king, Piece threat) {
    if ((isBlack() ? m_board.getGame().getBlackRules() : m_board.getGame().getWhiteRules())
            .objectivePiece(isBlack())
        == this) return;
    if (king == null) return;
    Iterator<Square> oldLegalDests = getLegalDests().iterator();
    Square sq = null;

    if (m_captured) return;

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

    while (oldLegalDests.hasNext()) {
      sq = oldLegalDests.next();

      if (threat.isBlockable(sq, king)) getLegalDests().add(sq);
      else if (sq.equals(threat.getSquare())) getLegalDests().add(sq);
    }
  }
Exemple #2
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);
  }
Exemple #3
0
  /**
   * Adjust the legal destinations of this piece if they are forced to continue protecting the
   * objective piece from a member of the enemy team
   *
   * @param objectivePiece The piece to protect
   * @param enemyTeam The enemy team
   */
  public void adjustPinsLegalDests(Piece objectivePiece, List<Piece> enemyTeam) {

    if (((isBlack() ? m_board.getGame().getBlackRules() : m_board.getGame().getWhiteRules())
                .objectivePiece(isBlack())
            == this)
        && (m_board.getGame().isBlackMove() == isBlack())) {
      List<Square> tmpLegalDests = getLegalDests();
      Iterator<Square> perlimMoves = tmpLegalDests.iterator();

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

      // Make sure the you don't move into check
      Square dest;
      while (perlimMoves.hasNext()) {
        dest = perlimMoves.next();

        if (!m_board.getGame().isThreatened(dest, !isBlack())
            && !m_board.getGame().isGuarded(dest, !isBlack())) addLegalDest(dest);
      }

      if (m_board.getGame().isClassicChess()) {
        // Castling
        if (getMoveCount() == 0) {
          boolean blocked = false;
          // Castle Queen side
          Piece rook = m_board.getSquare(m_curSquare.getRow(), 1).getPiece();
          if (rook != null && rook.getMoveCount() == 0) {
            blocked = false;

            for (int c = (rook.getSquare().getCol() + 1);
                c <= m_curSquare.getCol() && !blocked;
                c++) {
              if (c < m_curSquare.getCol())
                blocked = m_board.getSquare(m_curSquare.getRow(), c).isOccupied();

              if (!blocked)
                blocked =
                    m_board
                        .getGame()
                        .isThreatened(m_board.getSquare(m_curSquare.getRow(), c), !isBlack());
            }

            if (!blocked) addLegalDest(m_board.getSquare(((isBlack()) ? 8 : 1), 3));
          }

          // Castle King side
          rook = m_board.getSquare(m_curSquare.getRow(), m_board.getMaxCol()).getPiece();
          if (rook != null && rook.getMoveCount() == 0) {
            blocked = false;

            for (int c = (rook.getSquare().getCol() - 1);
                c >= m_curSquare.getCol() && !blocked;
                c--) {
              if (c > m_curSquare.getCol())
                blocked = m_board.getSquare(m_curSquare.getRow(), c).isOccupied();

              if (!blocked)
                blocked =
                    m_board
                        .getGame()
                        .isThreatened(m_board.getSquare(m_curSquare.getRow(), c), !isBlack());
            }

            if (!blocked) addLegalDest(m_board.getSquare(((isBlack()) ? 8 : 1), 7));
          }
        }
      }
      return;
    }

    Square[] line = getLineOfSight(objectivePiece, false);
    Piece pin = null;
    Piece tmp;
    boolean done = false;

    if (m_captured) return;

    if (line != null) {
      List<Square> temp = Lists.newArrayList();
      for (Square sq : line) {
        if (m_legalDests.contains(sq) || sq.equals(m_curSquare)) temp.add(sq);
      }
      line = new Square[temp.size()];
      temp.toArray(line);

      if (m_board.getGame().isStaleLegalDests()) m_board.getGame().genLegalDests();

      // Start i at 1 since 0 is this Piece
      for (int i = 1; i < line.length && !done; i++) {
        tmp = line[i].getPiece();

        if (tmp != null) {
          // two pieces blocking the attack is not a pin
          if (pin != null) {
            pin = null;
            done = true;
          }
          // friend in the way
          else if (tmp.isBlack() == isBlack()) {
            done = true;
          } else {
            pin = tmp;
          }
        }
      }

      if (pin != null) {
        // need to AND moves with line (includes this square)
        List<Square> maintainPins = Arrays.asList(line);
        pin.setPinned(this, maintainPins);
      }
    }
  }