Advertisement
cd62131

qa7230238 rock paper scissors じゃんけん

Oct 18th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.63 KB | None | 0 0
  1. import java.util.*;
  2. public class QA7230238 {
  3.   private enum Hand {
  4.     Rock("グー"), Paper("パー"), Scissors("チョキ");
  5.     private String getLocale() {
  6.       return this.locale;
  7.     }
  8.     private void setLocale(String locale) {
  9.       this.locale = locale;
  10.     }
  11.     private String locale;
  12.     Hand(String locale) {
  13.       this.setLocale(locale);
  14.     }
  15.   }
  16.   private enum Player {
  17.     Human, Computer
  18.   }
  19.   private enum Result {
  20.     Win, Throw, Lose
  21.   }
  22.   private static Hand hand(String s) {
  23.     Hand ret = null;
  24.     switch (s) {
  25.       case "r":
  26.         ret = Hand.Rock;
  27.         break;
  28.       case "p":
  29.         ret = Hand.Paper;
  30.         break;
  31.       case "s":
  32.         ret = Hand.Scissors;
  33.         break;
  34.       default:
  35.         System.exit(1);
  36.     }
  37.     return ret;
  38.   }
  39.   private static Hand hand(int i) {
  40.     Hand ret = null;
  41.     switch (i) {
  42.       case 0:
  43.         ret = Hand.Rock;
  44.         break;
  45.       case 1:
  46.         ret = Hand.Paper;
  47.         break;
  48.       case 2:
  49.         ret = Hand.Scissors;
  50.         break;
  51.       default:
  52.         System.exit(1);
  53.     }
  54.     return ret;
  55.   }
  56.   private static Result result(Hand a, Hand b) {
  57.     if (a == Hand.Rock && b == Hand.Paper) { return Result.Lose; }
  58.     if (a == Hand.Rock && b == Hand.Scissors) { return Result.Win; }
  59.     if (a == Hand.Paper && b == Hand.Rock) { return Result.Win; }
  60.     if (a == Hand.Paper && b == Hand.Scissors) { return Result.Lose; }
  61.     if (a == Hand.Scissors && b == Hand.Rock) { return Result.Lose; }
  62.     if (a == Hand.Scissors && b == Hand.Paper) { return Result.Win; }
  63.     return Result.Throw;
  64.   }
  65.   public static void main(String[] args) {
  66.     int games = 3;
  67.     Scanner in = new Scanner(System.in);
  68.     Map<Player, ArrayList<Hand>> game = new EnumMap<Player, ArrayList<Hand>>(
  69.         Player.class);
  70.     game.put(Player.Human, new ArrayList<Hand>());
  71.     game.put(Player.Computer, new ArrayList<Hand>());
  72.     Random rand = new Random();
  73.     for (int i = 0; i < games; i++) {
  74.       System.out.print("Choose (r)ock (p)aper (s)cissors ? ");
  75.       game.get(Player.Human).add(hand(in.nextLine()));
  76.       game.get(Player.Computer).add(hand(rand.nextInt(3)));
  77.     }
  78.     in.close();
  79.     StringBuilder sb = new StringBuilder();
  80.     sb.append(Player.Human.name() + " vs " + Player.Computer.name() + "\n");
  81.     for (int i = 0; i < games; i++) {
  82.       Hand human = game.get(Player.Human).get(i);
  83.       Hand computer = game.get(Player.Computer).get(i);
  84.       sb.append(human.getLocale() + " vs ");
  85.       sb.append(computer.getLocale() + " --> ");
  86.       sb.append(result(human, computer).name() + "\n");
  87.     }
  88.     System.out.print(sb);
  89.   }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement