|
| 1 | +package blackjack.blackjack.card; |
| 2 | + |
| 3 | +import blackjack.util.ExceptionMessage; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Objects; |
| 8 | + |
| 9 | +public final class Hand { |
| 10 | + |
| 11 | + private static final int BLACKJACK_SIZE = 2; |
| 12 | + public static final int BURST_THRESHOLD = 21; |
| 13 | + private static final int ACE_SUBTRACT = 10; |
| 14 | + |
| 15 | + private final List<Card> cards; |
| 16 | + |
| 17 | + public Hand(final List<Card> cards) { |
| 18 | + this.cards = new ArrayList<>(cards); |
| 19 | + } |
| 20 | + |
| 21 | + public Hand(final Card card) { |
| 22 | + this(List.of(card)); |
| 23 | + } |
| 24 | + |
| 25 | + public int calculateScore() { |
| 26 | + int maxScore = calculateScore(cards); |
| 27 | + if (isNotBurst(maxScore)) { |
| 28 | + return maxScore; |
| 29 | + } |
| 30 | + return subtractAce(maxScore); |
| 31 | + } |
| 32 | + |
| 33 | + public int calculateWithHardHand() { |
| 34 | + return cards.stream() |
| 35 | + .mapToInt(Card::getCardMinNumber) |
| 36 | + .sum(); |
| 37 | + } |
| 38 | + |
| 39 | + public void add(final Card card) { |
| 40 | + cards.add(card); |
| 41 | + } |
| 42 | + |
| 43 | + public void addAll(final Hand givenHand) { |
| 44 | + cards.addAll(givenHand.getCards()); |
| 45 | + } |
| 46 | + |
| 47 | + public boolean isBlackjack() { |
| 48 | + return cards.size() == BLACKJACK_SIZE && calculateScore() == BURST_THRESHOLD; |
| 49 | + } |
| 50 | + |
| 51 | + public boolean isBust() { |
| 52 | + return calculateScore() > BURST_THRESHOLD; |
| 53 | + } |
| 54 | + |
| 55 | + private int calculateScore(final List<Card> cards) { |
| 56 | + return cards.stream() |
| 57 | + .mapToInt(Card::getCardMaxNumber) |
| 58 | + .sum(); |
| 59 | + } |
| 60 | + |
| 61 | + private boolean isNotBurst(final int score) { |
| 62 | + return score <= BURST_THRESHOLD; |
| 63 | + } |
| 64 | + |
| 65 | + private int subtractAce(int score) { |
| 66 | + int aceCount = countAce(cards); |
| 67 | + while (!isNotBurst(score) && aceCount-- > 0) { |
| 68 | + score -= ACE_SUBTRACT; |
| 69 | + } |
| 70 | + return score; |
| 71 | + } |
| 72 | + |
| 73 | + private int countAce(final List<Card> cards) { |
| 74 | + return (int) cards.stream() |
| 75 | + .filter(Card::isAce) |
| 76 | + .count(); |
| 77 | + } |
| 78 | + |
| 79 | + private void validateIndex(final int start, final int end) { |
| 80 | + final int size = cards.size(); |
| 81 | + if (start < 0 || end < 0 || start >= size || end > size) { |
| 82 | + throw new IllegalArgumentException(ExceptionMessage.makeMessage("인덱스는 0 이상 hand 크기 이하여야 합니다")); |
| 83 | + } |
| 84 | + if (start > end) { |
| 85 | + throw new IllegalArgumentException(ExceptionMessage.makeMessage("끝 인덱스는 시작 인덱스보다 커야합니다")); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean equals(final Object o) { |
| 91 | + if (!(o instanceof final Hand hand1)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + return Objects.equals(cards, hand1.cards); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public int hashCode() { |
| 99 | + return Objects.hashCode(cards); |
| 100 | + } |
| 101 | + |
| 102 | + public int getSize() { |
| 103 | + return cards.size(); |
| 104 | + } |
| 105 | + |
| 106 | + public Card getFirstCard() { |
| 107 | + return cards.getFirst(); |
| 108 | + } |
| 109 | + |
| 110 | + public Hand getPartialHand(final int startInclusive, final int endExclusive) { |
| 111 | + validateIndex(startInclusive, endExclusive); |
| 112 | + return new Hand(cards.subList(startInclusive, endExclusive)); |
| 113 | + } |
| 114 | + |
| 115 | + public List<Card> getCards() { |
| 116 | + return Collections.unmodifiableList(cards); |
| 117 | + } |
| 118 | +} |
0 commit comments