Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class QA7230238 {
- private enum Hand {
- Rock("グー"), Paper("パー"), Scissors("チョキ");
- private String getLocale() {
- return this.locale;
- }
- private void setLocale(String locale) {
- this.locale = locale;
- }
- private String locale;
- Hand(String locale) {
- this.setLocale(locale);
- }
- }
- private enum Player {
- Human, Computer
- }
- private enum Result {
- Win, Throw, Lose
- }
- private static Hand hand(String s) {
- Hand ret = null;
- switch (s) {
- case "r":
- ret = Hand.Rock;
- break;
- case "p":
- ret = Hand.Paper;
- break;
- case "s":
- ret = Hand.Scissors;
- break;
- default:
- System.exit(1);
- }
- return ret;
- }
- private static Hand hand(int i) {
- Hand ret = null;
- switch (i) {
- case 0:
- ret = Hand.Rock;
- break;
- case 1:
- ret = Hand.Paper;
- break;
- case 2:
- ret = Hand.Scissors;
- break;
- default:
- System.exit(1);
- }
- return ret;
- }
- private static Result result(Hand a, Hand b) {
- if (a == Hand.Rock && b == Hand.Paper) { return Result.Lose; }
- if (a == Hand.Rock && b == Hand.Scissors) { return Result.Win; }
- if (a == Hand.Paper && b == Hand.Rock) { return Result.Win; }
- if (a == Hand.Paper && b == Hand.Scissors) { return Result.Lose; }
- if (a == Hand.Scissors && b == Hand.Rock) { return Result.Lose; }
- if (a == Hand.Scissors && b == Hand.Paper) { return Result.Win; }
- return Result.Throw;
- }
- public static void main(String[] args) {
- int games = 3;
- Scanner in = new Scanner(System.in);
- Map<Player, ArrayList<Hand>> game = new EnumMap<Player, ArrayList<Hand>>(
- Player.class);
- game.put(Player.Human, new ArrayList<Hand>());
- game.put(Player.Computer, new ArrayList<Hand>());
- Random rand = new Random();
- for (int i = 0; i < games; i++) {
- System.out.print("Choose (r)ock (p)aper (s)cissors ? ");
- game.get(Player.Human).add(hand(in.nextLine()));
- game.get(Player.Computer).add(hand(rand.nextInt(3)));
- }
- in.close();
- StringBuilder sb = new StringBuilder();
- sb.append(Player.Human.name() + " vs " + Player.Computer.name() + "\n");
- for (int i = 0; i < games; i++) {
- Hand human = game.get(Player.Human).get(i);
- Hand computer = game.get(Player.Computer).get(i);
- sb.append(human.getLocale() + " vs ");
- sb.append(computer.getLocale() + " --> ");
- sb.append(result(human, computer).name() + "\n");
- }
- System.out.print(sb);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement