| 3 | import java.util.ArrayList; |
| 4 | |
| 5 | public class BlackJackHand extends Hand<BlackJackCard> { |
| 6 | public BlackJackHand() { |
| 7 | |
| 8 | } |
| 9 | |
| 10 | public int score() { |
| 11 | ArrayList<Integer> scores = possibleScores(); |
| 12 | int maxUnder = Integer.MIN_VALUE; |
| 13 | int minOver = Integer.MAX_VALUE; |
| 14 | for (int score : scores) { |
| 15 | if (score > 21 && score < minOver) { |
| 16 | minOver = score; |
| 17 | } else if (score <= 21 && score > maxUnder) { |
| 18 | maxUnder = score; |
| 19 | } |
| 20 | } |
| 21 | return maxUnder == Integer.MIN_VALUE ? minOver : maxUnder; |
| 22 | } |
| 23 | |
| 24 | private ArrayList<Integer> possibleScores() { |
| 25 | ArrayList<Integer> scores = new ArrayList<Integer>(); |
| 26 | if (cards.size() == 0) { |
| 27 | return scores; |
| 28 | } |
| 29 | for (BlackJackCard card : cards) { |
| 30 | addCardToScoreList(card, scores); |
| 31 | } |
| 32 | return scores; |
| 33 | } |
| 34 | |
| 35 | private void addCardToScoreList(BlackJackCard card, ArrayList<Integer> scores) { |
| 36 | if (scores.size() == 0) { |
| 37 | scores.add(0); |
| 38 | } |
| 39 | int length = scores.size(); |
| 40 | for (int i = 0; i < length; i++) { |
| 41 | int score = scores.get(i); |
| 42 | scores.set(i, score + card.minValue()); |
| 43 | if (card.minValue() != card.maxValue()) { |
| 44 | scores.add(score + card.maxValue()); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public boolean busted() { |
| 50 | return score() > 21; |
| 51 | } |
| 52 | |
| 53 | public boolean is21() { |
| 54 | return score() == 21; |
| 55 | } |
| 56 | |
| 57 | public boolean isBlackJack() { |
| 58 | if (cards.size() != 2) { |
| 59 | return false; |
| 60 | } |
| 61 | BlackJackCard first = cards.get(0); |
| 62 | BlackJackCard second = cards.get(1); |
nothing calls this directly
no outgoing calls
no test coverage detected