示例#1
0
文件: Piece.java 项目: routhcr/chess
  /**
   * 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);
      }
    }
  }