private void playCard(@NotNull SevensPlayer inputter, Card card, Card adjacentExtremeCard) { if (adjacentExtremeCard.getRank() != SEVEN) { extremeCards.remove(adjacentExtremeCard); } extremeCards.add(card); playedCards.get(card.getSuit()).add(card); inputter.playCard(card); }
@Nullable private Card getAdjacentExtremeCard(@NotNull Card card) { if (card.getRank() == SEVEN) { if (extremeCards.isEmpty()) { // You can only start the game with the seven of diamonds. return card == SEVEN_OF_DIAMONDS ? SEVEN_OF_DIAMONDS : null; } else { return card; } } return extremeCards .stream() .filter(extreme -> isAdjacent(card, extreme)) .findAny() .orElse(null); }
private boolean isAdjacent(Card card, Card extreme) { return card.getSuit() == extreme.getSuit() && (card.getRank().getValue() == extreme.getRank().getValue() - 1 || card.getRank().getValue() == extreme.getRank().getValue() + 1); }