Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Scanner;
- /**
- * Auto-generated code below aims at helping you parse
- * the standard input according to the problem statement.
- **/
- class Solution {
- private static String PLAYER1 = "1";
- private static String PLAYER2 = "2";
- private static HashMap<String, ArrayList<String>> HANDS = null;
- private static HashMap<String, List<String>> STOCK_HANDS = null;
- static {
- HANDS = new HashMap<String, ArrayList<String>>();
- STOCK_HANDS = new HashMap<String, List<String>>();
- STOCK_HANDS.put(PLAYER1, new ArrayList<String>());
- STOCK_HANDS.put(PLAYER2, new ArrayList<String>());
- }
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt(); // the number of cards for player 1
- ArrayList<String> tmpP1 = new ArrayList<String>();
- for (int i = 0; i < n; i++) {
- tmpP1.add(in.next()); // the n cards of player 1
- }
- HANDS.put(PLAYER1, tmpP1);
- int m = in.nextInt(); // the number of cards for player 2
- ArrayList<String> tmpP2 = new ArrayList<String>();
- for (int i = 0; i < m; i++) {
- tmpP2.add(in.next()); // the m cards of player 2
- }
- HANDS.put(PLAYER2, tmpP2);
- // Write an action using System.out.println()
- // To debug: System.err.println("Debug messages...");
- String result = game();
- System.out.println(result);
- }
- // 実際のゲーム
- private static String game() {
- int round = 0;
- boolean isPat = false;
- while (HANDS.get(PLAYER1).size() != 0 &&
- HANDS.get(PLAYER2).size() != 0) {
- System.err.println(HANDS.get(PLAYER1));
- System.err.println(HANDS.get(PLAYER2));
- // player1,2のカードをだす
- String card1 = HANDS.get(PLAYER1).get(0);
- String card2 = HANDS.get(PLAYER2).get(0);
- int card1Number = callNumber(card1);
- int card2Number = callNumber(card2);
- // 出したカードを比較する
- if (card1Number - card2Number > 0) {
- // player1の勝ち
- setStockHands(false);
- List<String> tmpArray = getStockHands();
- HANDS.get(PLAYER1).addAll(tmpArray);
- } else if (card1Number - card2Number == 0) {
- // war
- // 0.手持ちが5枚以上あるかチェック
- // 1.カード保持(出したカードと、上から3枚目までのカード
- // 2.もう一度バトル
- if (HANDS.get(PLAYER1).size() < 4 || HANDS.get(PLAYER2).size() < 4) {
- isPat = true;
- break;
- }
- setStockHands(true);
- continue;
- } else {
- // player2の勝ち
- setStockHands(false);
- List<String> tmpArray = getStockHands();
- HANDS.get(PLAYER2).addAll(tmpArray);
- }
- round++;
- }
- String winner = null;
- if (HANDS.get(PLAYER1).size() > HANDS.get(PLAYER2).size()) {
- winner = PLAYER1;
- } else {
- winner = PLAYER2;
- }
- return isPat ? "PAT" : winner + " " + round;
- }
- private static int callNumber(String hand) {
- String numberString = hand.substring(0, hand.length() - 1);
- switch (numberString) {
- case "J":
- return 11;
- case "Q":
- return 12;
- case "K":
- return 13;
- case "A":
- return 14;
- }
- return Integer.parseInt(numberString);
- }
- private static List<String> getStockHands() {
- List<String> tmpArrayList = new ArrayList<String>(STOCK_HANDS.get(PLAYER1));
- tmpArrayList.addAll(new ArrayList<String>(STOCK_HANDS.get(PLAYER2)));
- STOCK_HANDS.get(PLAYER1).clear();
- STOCK_HANDS.get(PLAYER2).clear();
- return tmpArrayList;
- }
- private static void setStockHands(boolean isWar) {
- int removeCnt = 1;
- if (isWar) {
- removeCnt = 4;
- }
- List<String> subArrayList1 = new ArrayList<String>(HANDS.get(PLAYER1).subList(0, removeCnt));
- List<String> subArrayList2 = new ArrayList<String>(HANDS.get(PLAYER2).subList(0, removeCnt));
- STOCK_HANDS.get(PLAYER1).addAll(subArrayList1);
- STOCK_HANDS.get(PLAYER2).addAll(subArrayList2);
- for (int i = 0; i < removeCnt; i++) {
- HANDS.get(PLAYER1).remove(0);
- HANDS.get(PLAYER2).remove(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement