/** * 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); } }
/** * 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); } } }
/** * Get the Squares between this piece and the target piece * * @param target The piece in the line of sight * @param inclusive Whether or not to include the target piece square * @return The Squares between this Piece and the target piece */ public Square[] getLineOfSight(Piece target, boolean inclusive) { return getLineOfSight(target.getSquare().getRow(), target.getSquare().getCol(), inclusive); }
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; } }