Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * Auto-generated code below aims at helping you parse
  8. * the standard input according to the problem statement.
  9. **/
  10. class Solution {
  11.  
  12. private static String PLAYER1 = "1";
  13. private static String PLAYER2 = "2";
  14.  
  15. private static HashMap<String, ArrayList<String>> HANDS = null;
  16. private static HashMap<String, List<String>> STOCK_HANDS = null;
  17.  
  18. static {
  19. HANDS = new HashMap<String, ArrayList<String>>();
  20. STOCK_HANDS = new HashMap<String, List<String>>();
  21. STOCK_HANDS.put(PLAYER1, new ArrayList<String>());
  22. STOCK_HANDS.put(PLAYER2, new ArrayList<String>());
  23. }
  24.  
  25. public static void main(String args[]) {
  26. Scanner in = new Scanner(System.in);
  27. int n = in.nextInt(); // the number of cards for player 1
  28. ArrayList<String> tmpP1 = new ArrayList<String>();
  29. for (int i = 0; i < n; i++) {
  30. tmpP1.add(in.next()); // the n cards of player 1
  31. }
  32. HANDS.put(PLAYER1, tmpP1);
  33.  
  34. int m = in.nextInt(); // the number of cards for player 2
  35. ArrayList<String> tmpP2 = new ArrayList<String>();
  36. for (int i = 0; i < m; i++) {
  37. tmpP2.add(in.next()); // the m cards of player 2
  38. }
  39. HANDS.put(PLAYER2, tmpP2);
  40. // Write an action using System.out.println()
  41. // To debug: System.err.println("Debug messages...");
  42.  
  43. String result = game();
  44.  
  45. System.out.println(result);
  46. }
  47.  
  48. // 実際のゲーム
  49. private static String game() {
  50.  
  51. int round = 0;
  52. boolean isPat = false;
  53.  
  54. while (HANDS.get(PLAYER1).size() != 0 &&
  55. HANDS.get(PLAYER2).size() != 0) {
  56.  
  57. System.err.println(HANDS.get(PLAYER1));
  58. System.err.println(HANDS.get(PLAYER2));
  59.  
  60. // player1,2のカードをだす
  61. String card1 = HANDS.get(PLAYER1).get(0);
  62.  
  63. String card2 = HANDS.get(PLAYER2).get(0);
  64.  
  65. int card1Number = callNumber(card1);
  66. int card2Number = callNumber(card2);
  67.  
  68. // 出したカードを比較する
  69. if (card1Number - card2Number > 0) {
  70. // player1の勝ち
  71. setStockHands(false);
  72. List<String> tmpArray = getStockHands();
  73. HANDS.get(PLAYER1).addAll(tmpArray);
  74.  
  75. } else if (card1Number - card2Number == 0) {
  76. // war
  77. // 0.手持ちが5枚以上あるかチェック
  78. // 1.カード保持(出したカードと、上から3枚目までのカード
  79. // 2.もう一度バトル
  80.  
  81. if (HANDS.get(PLAYER1).size() < 4 || HANDS.get(PLAYER2).size() < 4) {
  82. isPat = true;
  83. break;
  84. }
  85.  
  86. setStockHands(true);
  87. continue;
  88.  
  89. } else {
  90. // player2の勝ち
  91. setStockHands(false);
  92. List<String> tmpArray = getStockHands();
  93. HANDS.get(PLAYER2).addAll(tmpArray);
  94. }
  95.  
  96. round++;
  97. }
  98.  
  99. String winner = null;
  100. if (HANDS.get(PLAYER1).size() > HANDS.get(PLAYER2).size()) {
  101.  
  102. winner = PLAYER1;
  103.  
  104. } else {
  105.  
  106. winner = PLAYER2;
  107. }
  108.  
  109. return isPat ? "PAT" : winner + " " + round;
  110. }
  111.  
  112. private static int callNumber(String hand) {
  113.  
  114. String numberString = hand.substring(0, hand.length() - 1);
  115.  
  116. switch (numberString) {
  117.  
  118. case "J":
  119. return 11;
  120. case "Q":
  121. return 12;
  122. case "K":
  123. return 13;
  124. case "A":
  125. return 14;
  126. }
  127.  
  128. return Integer.parseInt(numberString);
  129. }
  130.  
  131. private static List<String> getStockHands() {
  132.  
  133. List<String> tmpArrayList = new ArrayList<String>(STOCK_HANDS.get(PLAYER1));
  134. tmpArrayList.addAll(new ArrayList<String>(STOCK_HANDS.get(PLAYER2)));
  135.  
  136. STOCK_HANDS.get(PLAYER1).clear();
  137. STOCK_HANDS.get(PLAYER2).clear();
  138.  
  139. return tmpArrayList;
  140. }
  141.  
  142. private static void setStockHands(boolean isWar) {
  143.  
  144. int removeCnt = 1;
  145. if (isWar) {
  146. removeCnt = 4;
  147. }
  148.  
  149. List<String> subArrayList1 = new ArrayList<String>(HANDS.get(PLAYER1).subList(0, removeCnt));
  150. List<String> subArrayList2 = new ArrayList<String>(HANDS.get(PLAYER2).subList(0, removeCnt));
  151. STOCK_HANDS.get(PLAYER1).addAll(subArrayList1);
  152. STOCK_HANDS.get(PLAYER2).addAll(subArrayList2);
  153.  
  154. for (int i = 0; i < removeCnt; i++) {
  155. HANDS.get(PLAYER1).remove(0);
  156. HANDS.get(PLAYER2).remove(0);
  157. }
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement