@Test public void canRecognizeHighestCard(){ CardSet cardSet = new CardSet( aCard(HEART, ACE), aCard(HEART, QUEEN), aCard(DIAMOND, TEN), aCard(SPADE, FIVE), aCard(CLUB, FOUR)); Hand hand = handResolver.hand(cardSet); assertThat(hand) .hasRanking(RANKING.HIGH_CARD); }
public Hand hand(CardSet cardSet) { List<Card> handCards = cardSet.getSortedCards(); SUIT colorCandidate = handCards.get(0).getSuit(); boolean allSameColor = handCards.stream() .allMatch(card -> card.getSuit().equals(colorCandidate)); if (allSameColor){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT_FLUSH, handCards); else return new Hand(FLUSH, handCards); } else{ Map<RANK, List<Card>> cardsByRank = handCards.stream().collect(groupingBy(Card::getRank)); List<RANK> ranks = cardsByRank.keySet() .stream() .collect(Collectors.toList()); if (ranks.size() == 5){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT, handCards); } if (ranks.size() == 2){ if (cardsByRank.get(ranks.get(0)).size() == 4 || cardsByRank.get(ranks.get(1)).size() == 4) return new Hand(FOUR_OF_A_KIND, handCards); else { return new Hand(FULL_HOUSE, handCards); } } else if (ranks.size() == 3){ if (cardsByRank.get(ranks.get(0)).size() == 3 || cardsByRank.get(ranks.get(1)).size() == 3 || cardsByRank.get(ranks.get(2)).size() == 3) return new Hand(THREE_OF_A_KIND, handCards); if (cardsByRank.get(ranks.get(0)).size() == 1 || cardsByRank.get(ranks.get(1)).size() == 1 || cardsByRank.get(ranks.get(2)).size() == 1) return new Hand(TWO_PAIRS, handCards); } else if (ranks.size() == 4){ return new Hand(ONE_PAIR, handCards); } else { return new Hand(HIGH_CARD, handCards); } } return new Hand(HIGH_CARD, handCards); }
HandResolver { public Hand hand(CardSet cardSet) { List<Card> handCards = cardSet.getSortedCards(); SUIT colorCandidate = handCards.get(0).getSuit(); boolean allSameColor = handCards.stream() .allMatch(card -> card.getSuit().equals(colorCandidate)); if (allSameColor){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT_FLUSH, handCards); else return new Hand(FLUSH, handCards); } else{ Map<RANK, List<Card>> cardsByRank = handCards.stream().collect(groupingBy(Card::getRank)); List<RANK> ranks = cardsByRank.keySet() .stream() .collect(Collectors.toList()); if (ranks.size() == 5){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT, handCards); } if (ranks.size() == 2){ if (cardsByRank.get(ranks.get(0)).size() == 4 || cardsByRank.get(ranks.get(1)).size() == 4) return new Hand(FOUR_OF_A_KIND, handCards); else { return new Hand(FULL_HOUSE, handCards); } } else if (ranks.size() == 3){ if (cardsByRank.get(ranks.get(0)).size() == 3 || cardsByRank.get(ranks.get(1)).size() == 3 || cardsByRank.get(ranks.get(2)).size() == 3) return new Hand(THREE_OF_A_KIND, handCards); if (cardsByRank.get(ranks.get(0)).size() == 1 || cardsByRank.get(ranks.get(1)).size() == 1 || cardsByRank.get(ranks.get(2)).size() == 1) return new Hand(TWO_PAIRS, handCards); } else if (ranks.size() == 4){ return new Hand(ONE_PAIR, handCards); } else { return new Hand(HIGH_CARD, handCards); } } return new Hand(HIGH_CARD, handCards); } }
HandResolver { public Hand hand(CardSet cardSet) { List<Card> handCards = cardSet.getSortedCards(); SUIT colorCandidate = handCards.get(0).getSuit(); boolean allSameColor = handCards.stream() .allMatch(card -> card.getSuit().equals(colorCandidate)); if (allSameColor){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT_FLUSH, handCards); else return new Hand(FLUSH, handCards); } else{ Map<RANK, List<Card>> cardsByRank = handCards.stream().collect(groupingBy(Card::getRank)); List<RANK> ranks = cardsByRank.keySet() .stream() .collect(Collectors.toList()); if (ranks.size() == 5){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT, handCards); } if (ranks.size() == 2){ if (cardsByRank.get(ranks.get(0)).size() == 4 || cardsByRank.get(ranks.get(1)).size() == 4) return new Hand(FOUR_OF_A_KIND, handCards); else { return new Hand(FULL_HOUSE, handCards); } } else if (ranks.size() == 3){ if (cardsByRank.get(ranks.get(0)).size() == 3 || cardsByRank.get(ranks.get(1)).size() == 3 || cardsByRank.get(ranks.get(2)).size() == 3) return new Hand(THREE_OF_A_KIND, handCards); if (cardsByRank.get(ranks.get(0)).size() == 1 || cardsByRank.get(ranks.get(1)).size() == 1 || cardsByRank.get(ranks.get(2)).size() == 1) return new Hand(TWO_PAIRS, handCards); } else if (ranks.size() == 4){ return new Hand(ONE_PAIR, handCards); } else { return new Hand(HIGH_CARD, handCards); } } return new Hand(HIGH_CARD, handCards); }  }
HandResolver { public Hand hand(CardSet cardSet) { List<Card> handCards = cardSet.getSortedCards(); SUIT colorCandidate = handCards.get(0).getSuit(); boolean allSameColor = handCards.stream() .allMatch(card -> card.getSuit().equals(colorCandidate)); if (allSameColor){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT_FLUSH, handCards); else return new Hand(FLUSH, handCards); } else{ Map<RANK, List<Card>> cardsByRank = handCards.stream().collect(groupingBy(Card::getRank)); List<RANK> ranks = cardsByRank.keySet() .stream() .collect(Collectors.toList()); if (ranks.size() == 5){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT, handCards); } if (ranks.size() == 2){ if (cardsByRank.get(ranks.get(0)).size() == 4 || cardsByRank.get(ranks.get(1)).size() == 4) return new Hand(FOUR_OF_A_KIND, handCards); else { return new Hand(FULL_HOUSE, handCards); } } else if (ranks.size() == 3){ if (cardsByRank.get(ranks.get(0)).size() == 3 || cardsByRank.get(ranks.get(1)).size() == 3 || cardsByRank.get(ranks.get(2)).size() == 3) return new Hand(THREE_OF_A_KIND, handCards); if (cardsByRank.get(ranks.get(0)).size() == 1 || cardsByRank.get(ranks.get(1)).size() == 1 || cardsByRank.get(ranks.get(2)).size() == 1) return new Hand(TWO_PAIRS, handCards); } else if (ranks.size() == 4){ return new Hand(ONE_PAIR, handCards); } else { return new Hand(HIGH_CARD, handCards); } } return new Hand(HIGH_CARD, handCards); }  Hand hand(CardSet cardSet); }
HandResolver { public Hand hand(CardSet cardSet) { List<Card> handCards = cardSet.getSortedCards(); SUIT colorCandidate = handCards.get(0).getSuit(); boolean allSameColor = handCards.stream() .allMatch(card -> card.getSuit().equals(colorCandidate)); if (allSameColor){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT_FLUSH, handCards); else return new Hand(FLUSH, handCards); } else{ Map<RANK, List<Card>> cardsByRank = handCards.stream().collect(groupingBy(Card::getRank)); List<RANK> ranks = cardsByRank.keySet() .stream() .collect(Collectors.toList()); if (ranks.size() == 5){ int firstOrdinal = handCards.get(0).getRank().ordinal(); int secondOrdinal = handCards.get(1).getRank().ordinal(); int thirdOrdinal = handCards.get(2).getRank().ordinal(); int fourthOrdinal = handCards.get(3).getRank().ordinal(); int fifthOrdinal = handCards.get(4).getRank().ordinal(); if (firstOrdinal + 1 == secondOrdinal && secondOrdinal + 1 == thirdOrdinal && thirdOrdinal + 1 == fourthOrdinal && fourthOrdinal + 1 == fifthOrdinal) return new Hand(STRAIGHT, handCards); } if (ranks.size() == 2){ if (cardsByRank.get(ranks.get(0)).size() == 4 || cardsByRank.get(ranks.get(1)).size() == 4) return new Hand(FOUR_OF_A_KIND, handCards); else { return new Hand(FULL_HOUSE, handCards); } } else if (ranks.size() == 3){ if (cardsByRank.get(ranks.get(0)).size() == 3 || cardsByRank.get(ranks.get(1)).size() == 3 || cardsByRank.get(ranks.get(2)).size() == 3) return new Hand(THREE_OF_A_KIND, handCards); if (cardsByRank.get(ranks.get(0)).size() == 1 || cardsByRank.get(ranks.get(1)).size() == 1 || cardsByRank.get(ranks.get(2)).size() == 1) return new Hand(TWO_PAIRS, handCards); } else if (ranks.size() == 4){ return new Hand(ONE_PAIR, handCards); } else { return new Hand(HIGH_CARD, handCards); } } return new Hand(HIGH_CARD, handCards); }  Hand hand(CardSet cardSet);  }