Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import lsm.helpers.Note;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6.  
  7. public class Cabbage {
  8.  
  9. public static void main(String... args) {
  10. Note.writenl("Final score: " + score("5D,QS,JC,KH,AC"));
  11. Note.nl();
  12. Note.writenl("Final score: " + score("8C,AD,10C,6H,7S"));
  13. Note.nl();
  14. Note.writenl("Final score: " + score("AC,6D,5C,10C,8C"));
  15. }
  16.  
  17. private static int score(String input) {
  18. Card[] cards = Arrays.stream(input.split(",")).map(Card::new).toArray(Card[]::new);
  19. int score = 0;
  20. Note.writenl(Arrays.stream(cards).mapToInt(i -> i.n).toArray());
  21. score += flushes(cards) + nobs(cards);
  22.  
  23. Note.writenl("flushes: " + flushes(cards));
  24. Note.writenl("nobs: " + nobs(cards));
  25.  
  26. Arrays.sort(cards, Comparator.comparingInt(o -> o.n));
  27.  
  28. score += pairs(cards) + runs(cards) + count(cards);
  29. Note.writenl("pairs: " + pairs(cards));
  30. Note.writenl("runs: " + runs(cards));
  31. Note.writenl("counts: " + count(cards));
  32.  
  33. return score;
  34. }
  35.  
  36. private static int count(Card[] cards) {
  37. return count(cards, 0, 0);
  38. }
  39.  
  40. private static int count(Card[] cards, int i, int val) {
  41. if(i >= cards.length) return 0;
  42. int score = 0;
  43. score += count(cards, i + 1, val);
  44. val += cards[i].val;
  45. if(val == 15) score += 2;
  46. score += count(cards, i + 1, val);
  47. return score;
  48. }
  49.  
  50. private static int runs(Card[] cards) {
  51. int streak = 1;
  52. int best = 1;
  53. for(int i = 1; i < cards.length; i++)
  54. if(cards[i].n == cards[i - 1].n + 1) {
  55. streak++;
  56. best = streak > best ? streak : best;
  57. }
  58. else streak = 1;
  59.  
  60. return best > 2 ? best : 0;
  61. }
  62.  
  63. private static int pairs(Card[] cards) {
  64. int score = 0;
  65. HashMap<Integer, Integer> map = new HashMap<>();
  66. for(Card card : cards)
  67. map.put(card.n, map.getOrDefault(card.n, 0) + 1);
  68. for(int key : map.keySet())
  69. switch(map.get(key)){
  70. case 2: score += 2; break;
  71. case 3: score += 6; break;
  72. case 4: score += 12; break;
  73. }
  74. return score;
  75. }
  76.  
  77. private static int nobs(Card[] cards) {
  78. int i = 0;
  79. for(int j = 0; j < cards.length - 1; j++)
  80. if(cards[j].type == cards[4].type && cards[j].n == 11)
  81. i++;
  82. return i;
  83. }
  84.  
  85.  
  86. private static int flushes(Card[] cards) {
  87. char type = cards[0].type;
  88. for(int i = 1; i < 4; i++)
  89. if(cards[i].type != type) return 0;
  90. return cards[4].type == type ? 5 : 4;
  91. }
  92. }
  93.  
  94. class Card{
  95. int n;
  96. int val;
  97. char type;
  98. Card(String card) {
  99. String val = card.substring(0, card.length() - 1);
  100. type = card.charAt(card.length() - 1);
  101. try{
  102. n = Integer.parseInt(val);
  103. this.val = n;
  104. } catch(Exception ignored) {
  105. switch(val) {
  106. case "A": n = 1; this.val = 1; break;
  107. case "J": n = 11; this.val = 10; break;
  108. case "Q": n = 12; this.val = 10; break;
  109. case "K": n = 13; this.val = 10; break;
  110. }
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement