예제 #1
0
파일: Piece.java 프로젝트: routhcr/chess
 public void setIsCaptured(boolean t) {
   // Clear both Lists so the Piece can't move anymore
   getLegalDests().clear();
   getGuardSquares().clear();
   setPinnedBy(null);
   m_captured = t;
 }
예제 #2
0
파일: Piece.java 프로젝트: routhcr/chess
 /**
  * Limit the legal destinations of this piece if it is pinned by another piece
  *
  * @param pinner The piece pinning this Piece
  * @param lineOfSight The legal destinations to retain
  */
 protected void setPinned(Piece pinner, List<Square> lineOfSight) {
   setPinnedBy(pinner);
   getLegalDests().retainAll(lineOfSight);
 }
예제 #3
0
파일: Piece.java 프로젝트: routhcr/chess
  /**
   * Generate the ArrayList of legal destinations for this Piece
   *
   * @param board The Board on which to look for legal destinations
   * @return The number of legal destinations for this Piece
   */
  public int genLegalDests(Board board) {
    // Clear both ArrayLists
    getLegalDests().clear();
    getGuardSquares().clear();
    setPinnedBy(null);
    // Boolean to tell when we are done generating destinations

    /*
     * Special genLegalDests for Pawns, to incorporate enPassant, special
     * initial movement, and diagonal capturing.
     */
    if (m_name.equals("Pawn")) {
      Square dest = null;
      int dir, row, col;
      setPinnedBy(null);
      if (m_captured) return 0;

      dir = (isBlack()) ? -1 : 1;

      // Take one step forward
      if (board.isRowValid(m_curSquare.getRow() + dir)) {
        dest = board.getSquare(m_curSquare.getRow() + dir, m_curSquare.getCol());
        if (!dest.isOccupied() && !getLegalDests().contains(dest)) addLegalDest(dest);
      }

      // Take an opposing piece
      if (board.isRowValid(row = (m_curSquare.getRow() + dir))) {
        col = m_curSquare.getCol();

        // if valid row
        // and the square is occupied (by the other team)
        // or it's not my move (so I'm threatening the square)
        if (board.isColValid((col + 1))
            && ((board.getSquare(row, col + 1).isOccupied()
                && isBlack() != board.getSquare(row, col + 1).getPiece().isBlack()))) {
          addLegalDest(board.getSquare(row, col + 1));
        }
        if (board.isColValid((col - 1))
            && ((board.getSquare(row, col - 1).isOccupied()
                && isBlack() != board.getSquare(row, col - 1).getPiece().isBlack()))) {
          addLegalDest(board.getSquare(row, col - 1));
        }
      }

      // two step
      if (getMoveCount() == 0 && board.isRowValid((m_curSquare.getRow() + (2 * dir)))) {
        dest = board.getSquare((m_curSquare.getRow() + (2 * dir)), m_curSquare.getCol());
        if (!dest.isOccupied()
            && !board.getSquare((m_curSquare.getRow() + dir), m_curSquare.getCol()).isOccupied()
            && !getLegalDests().contains(dest)) {
          addLegalDest(dest);
        }
      }

      if (board.getGame().isClassicChess()) {
        // enPassant
        if (isBlack() == board.isBlackTurn()
            && ((!isBlack() && m_curSquare.getRow() == 5)
                || (isBlack() && m_curSquare.getRow() == 4))) {
          col = m_curSquare.getCol();
          row = isBlack() ? m_curSquare.getRow() - 1 : m_curSquare.getRow() + 1;
          if (board.isColValid(col + 1) && board.getEnpassantCol() == (col + 1))
            addLegalDest(board.getSquare(row, (col + 1)));

          if (board.isColValid(col - 1) && board.getEnpassantCol() == (col - 1))
            addLegalDest(board.getSquare(row, (col - 1)));
        }
      }
      return getLegalDests().size();
    }

    boolean done = false;
    Square dest;
    boolean wraparound = board.isWrapAround();
    /*
     * East
     */
    if (m_movements.containsKey('E')) {
      int northMax = m_movements.get('E') + m_curSquare.getCol();
      if (northMax > board.getMaxCol() || m_movements.get('E') == -1) {
        if (!wraparound) northMax = board.getMaxCol();
      }
      for (int c = m_curSquare.getCol() + 1;
          ((m_movements.get('E') == -1 && wraparound) ? true : c <= northMax) && !done;
          c++) {
        int j = c;
        if (wraparound) {
          if (j > board.getMaxCol()) j = j % board.getMaxCol();
        }

        if (j == 0) break;

        dest = board.getSquare(m_curSquare.getRow(), j);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    done = false;
    /*
     * West
     */
    if (m_movements.containsKey('W')) {
      int southMax = m_curSquare.getCol() - m_movements.get('W');
      if (southMax < 1 || m_movements.get('W') == -1) {
        if (!wraparound) southMax = 1;
      }
      for (int c = m_curSquare.getCol() - 1;
          ((m_movements.get('W') == -1 && wraparound) ? true : c >= southMax) && !done;
          c--) {
        int j = c;
        if (wraparound) {
          if (j < 1) j = board.getMaxCol() + j;
        }

        dest = board.getSquare(m_curSquare.getRow(), j);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    done = false;
    /*
     * North
     */
    if (m_movements.containsKey('N')) {
      int eastMax = m_movements.get('N') + m_curSquare.getRow();

      if (eastMax >= board.getMaxRow() || m_movements.get('N') == -1) eastMax = board.getMaxRow();

      for (int r = m_curSquare.getRow() + 1; (r <= eastMax) && !done; r++) {
        int j = r;
        dest = board.getSquare(j, m_curSquare.getCol());
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    done = false;
    /*
     * South
     */
    if (m_movements.containsKey('S')) {
      int westMax = m_curSquare.getRow() - m_movements.get('S');

      if (westMax < 1 || m_movements.get('S') == -1) westMax = 1;

      for (int r = m_curSquare.getRow() - 1; (r >= westMax) && !done; r--) {
        int j = r;
        dest = board.getSquare(j, m_curSquare.getCol());
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    /*
     * NorthEast
     */
    done = false;
    if (m_movements.containsKey('R')) {
      int neMax =
          ((m_curSquare.getRow() >= m_curSquare.getCol())
                  ? m_curSquare.getRow()
                  : m_curSquare.getCol())
              + m_movements.get('R');

      if (neMax >= board.getMaxCol() || m_movements.get('R') == -1) neMax = board.getMaxCol();
      if (neMax >= board.getMaxRow() || m_movements.get('R') == -1) neMax = board.getMaxRow();

      for (int r = m_curSquare.getRow() + 1, c = m_curSquare.getCol() + 1;
          r <= neMax && c <= neMax && !done;
          r++, c++) {
        dest = board.getSquare(r, c);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }

    /*
     * SouthEast
     */
    done = false;
    if (m_movements.containsKey('r')) {
      int eastMax = m_curSquare.getCol() + m_movements.get('r');

      if (eastMax >= board.getMaxCol() || m_movements.get('r') == -1) eastMax = board.getMaxCol();

      int southMin = m_curSquare.getRow() - m_movements.get('r');

      if (southMin <= 1 || m_movements.get('R') == -1) southMin = 1;

      for (int r = m_curSquare.getRow() - 1, c = m_curSquare.getCol() + 1;
          r >= southMin && c <= eastMax && !done;
          r--, c++) {
        dest = board.getSquare(r, c);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    /*
     * NorthWest
     */
    done = false;
    if (m_movements.containsKey('L')) {
      int westMin = m_curSquare.getCol() - m_movements.get('L');
      if (westMin <= 1 || m_movements.get('L') == -1) westMin = 1;

      int NorthMax = m_curSquare.getRow() + m_movements.get('L');
      if (NorthMax >= board.getMaxRow() || m_movements.get('L') == -1) NorthMax = board.getMaxRow();

      for (int r = m_curSquare.getRow() + 1, c = m_curSquare.getCol() - 1;
          r <= NorthMax && c >= westMin && !done;
          r++, c--) {
        dest = board.getSquare(r, c);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    /*
     * SouthWest
     */
    done = false;
    if (m_movements.containsKey('l')) {
      int westMin = m_curSquare.getCol() - m_movements.get('l');
      if (westMin <= 1 || m_movements.get('l') == -1) westMin = 1;

      int southMin = m_curSquare.getRow() - m_movements.get('l');
      if (southMin <= 1 || m_movements.get('l') == -1) southMin = 1;

      for (int r = m_curSquare.getRow() - 1, c = m_curSquare.getCol() - 1;
          r >= southMin && c >= westMin && !done;
          r--, c--) {
        dest = board.getSquare(r, c);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }

    /*
     * Knight / Leaper Movements
     *
     *
     * Store of Knight Movements are as followed:
     *
     * A Piece can move x File by y Rank squares at a time.
     *
     * IE: A knight can move 1 by 2 or 2 by 1, but not 1 by 1 or 2 by 2
     */
    if (m_movements.containsKey('x')) {
      int f, r;
      int Rank = m_movements.get('x');
      int File = m_movements.get('y');
      f = (m_curSquare.getRow() + File);
      r = (m_curSquare.getCol() + Rank);
      if (wraparound) {
        if (r > board.getMaxCol() + 1) r = r % board.getMaxCol();
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // two o'clock
      f = (m_curSquare.getRow() + Rank);
      r = (m_curSquare.getCol() + File);
      if (wraparound) {
        if (r > board.getMaxCol() + 1) r = r % board.getMaxCol();
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // four o'clock
      f = (m_curSquare.getRow() + File);
      r = (m_curSquare.getCol() - Rank);
      if (wraparound) {
        if (r < 1) r = board.getMaxCol() + r;
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // five o'clock
      f = (m_curSquare.getRow() + Rank);
      r = (m_curSquare.getCol() - File);
      if (wraparound) {
        if (r < 1) r = board.getMaxCol() + r;
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // seven o'clock
      f = (m_curSquare.getRow() - File);
      r = (m_curSquare.getCol() - Rank);
      if (wraparound) {
        if (r < 1) r = board.getMaxCol() + r;
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // eight o'clock
      f = (m_curSquare.getRow() - Rank);
      r = (m_curSquare.getCol() - File);
      if (wraparound) {
        if (r < 1) r = board.getMaxCol() + r;
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // ten o'clock
      f = (m_curSquare.getRow() - File);
      r = (m_curSquare.getCol() + Rank);
      if (wraparound) {
        if (r > board.getMaxCol() + 1) r = r % board.getMaxCol();
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // eleven o'clock
      f = (m_curSquare.getRow() - Rank);
      r = (m_curSquare.getCol() + File);
      if (wraparound) {
        if (r > board.getMaxCol() + 1) r = r % board.getMaxCol();
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));
    }
    return getLegalDests().size();
  }