lots of exercises in java... from https://github.com/exercism/java

Hand.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. class Hand {
  4. private String input;
  5. private int score;
  6. Hand(String hand) {
  7. this.input = hand;
  8. this.score = scoreHand(parseCards(hand));
  9. }
  10. int getScore() {
  11. return score;
  12. }
  13. String getInput() {
  14. return input;
  15. }
  16. private List<Card> parseCards(String hand) {
  17. ArrayList<Card> parsedCards = new ArrayList<>();
  18. String[] cardsToParse = hand.split(" ");
  19. for (String cardToParse : cardsToParse) {
  20. parsedCards.add(new Card(cardToParse));
  21. }
  22. return parsedCards;
  23. }
  24. private Map<Integer, Integer> getFrequencyMap(List<Card> cards) {
  25. Map<Integer, Integer> frequencyMap = new HashMap<>();
  26. for (Card c : cards) {
  27. if (frequencyMap.containsKey(c.getRank())) {
  28. frequencyMap.put(c.getRank(), frequencyMap.get(c.getRank()) + 1);
  29. } else {
  30. frequencyMap.put(c.getRank(), 1);
  31. }
  32. }
  33. return frequencyMap;
  34. }
  35. private int scoreHand(List<Card> cards) {
  36. List<Card> cardsByRank = cards.stream()
  37. .sorted(Comparator.comparing(Card::getRank))
  38. .unordered()
  39. .collect(Collectors.toList());
  40. Map<Integer, Integer> frequencyMap = getFrequencyMap(cards);
  41. List<Integer> ranks = frequencyMap
  42. .entrySet()
  43. .stream()
  44. .map(Map.Entry::getKey)
  45. .sorted(Comparator.reverseOrder())
  46. .collect(Collectors.toList());
  47. frequencyMap = frequencyMap
  48. .entrySet()
  49. .stream()
  50. .sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
  51. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
  52. List<Integer> rankCounts = frequencyMap
  53. .entrySet()
  54. .stream()
  55. .map(Map.Entry::getValue)
  56. .collect(Collectors.toList());
  57. List<Integer> suits = cards
  58. .stream()
  59. .map(Card::getSuit)
  60. .collect(Collectors.toList());
  61. return calculatedScore(frequencyMap, cardsByRank, ranks, rankCounts, suits);
  62. }
  63. private int calculatedScore(Map<Integer, Integer> frequencyMap, List<Card> cardsByRank, List<Integer> ranks,
  64. List<Integer> rankCounts, List<Integer> suits) {
  65. if (ranks.equals(Arrays.asList(14, 5, 4, 3, 2))) {
  66. ranks = Arrays.asList(5, 4, 3, 2, 1);
  67. }
  68. boolean flush = suits
  69. .stream()
  70. .distinct()
  71. .count() == 1;
  72. boolean straight = ranks.stream().distinct().count() == 5
  73. && ranks.get(0) - ranks.get(4) == 4;
  74. Iterator<Integer> iteratorOverFrequencies = frequencyMap.keySet().iterator();
  75. int highestFrequency = iteratorOverFrequencies.next();
  76. if (straight && flush) {
  77. return 800 + highestFrequency;
  78. }
  79. if (rankCounts.equals(Arrays.asList(4, 1))) {
  80. return 700 + cardsByRank.get(0).getRank();
  81. }
  82. if (rankCounts.equals(Arrays.asList(3, 2))) {
  83. int triplet = 0;
  84. int pair = 0;
  85. for (Integer key : frequencyMap.keySet()) {
  86. if (frequencyMap.get(key) == 2) {
  87. pair = (int) key;
  88. }
  89. if (frequencyMap.get(key) == 3) {
  90. triplet = 3 * (int) key;
  91. }
  92. }
  93. return 600 + 3 * triplet + pair;
  94. }
  95. if (flush) {
  96. return 500 + highestFrequency;
  97. }
  98. if (straight) {
  99. int maxValue = Collections.max(ranks);
  100. return 400 + maxValue;
  101. }
  102. if (rankCounts.equals(Arrays.asList(3, 1, 1))) {
  103. List<Integer> uniqueCards = new ArrayList<Integer>();
  104. int triplet = 0;
  105. for (Integer key : frequencyMap.keySet()) {
  106. if (frequencyMap.get(key) == 1) {
  107. uniqueCards.add((int) key);
  108. }
  109. if (frequencyMap.get(key) == 3) {
  110. triplet = 3 * (int) key;
  111. }
  112. }
  113. return 300 + triplet + Collections.max(uniqueCards);
  114. }
  115. if (rankCounts.equals(Arrays.asList(2, 2, 1))) {
  116. int productsOfFrequencyAndValue = 0;
  117. for (Integer key : frequencyMap.keySet()) {
  118. int frequencyKey = (int) key;
  119. int frequencyValue = frequencyMap.get(key);
  120. productsOfFrequencyAndValue += frequencyKey * frequencyValue;
  121. }
  122. return 200 + productsOfFrequencyAndValue + 2 * Math.max(highestFrequency, iteratorOverFrequencies.next());
  123. }
  124. if (rankCounts.equals(Arrays.asList(2, 1, 1, 1))) {
  125. return 100 + highestFrequency;
  126. }
  127. ranks.sort(Comparator.naturalOrder());
  128. int result = 0;
  129. for (int i = 0; i < ranks.size(); i++) {
  130. result += ranks.get(0) * (i + 1);
  131. }
  132. return result + ranks.get(ranks.size() - 1);
  133. }
  134. }