Last active
August 29, 2015 14:12
-
-
Save skrb/f2d3882ee653ec68249a to your computer and use it in GitHub Desktop.
じゃんけん
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.EnumMap; | |
import java.util.Map; | |
import java.util.Random; | |
import java.util.function.Supplier; | |
public class RockPaperScissors { | |
class FormatException extends Exception { | |
public FormatException(String message) { | |
super(message); | |
} | |
public FormatException(Throwable cause) { | |
super(cause); | |
} | |
} | |
enum Result { | |
WIN { | |
@Override | |
public String toString() { | |
return "I Win!"; | |
} | |
}, | |
LOSE { | |
@Override | |
public String toString() { | |
return "I Lose!"; | |
} | |
}, | |
DRAW { | |
@Override | |
public String toString() { | |
return "Draw\nOnce Again!"; | |
} | |
} | |
} | |
enum Hand { | |
ROCK, PAPER, SCISSORS | |
} | |
class Judge { | |
private final Map<Hand, Result> judgeMap; | |
public Judge(Supplier<Map<Hand, Result>> supplier) { | |
judgeMap = supplier.get(); | |
} | |
public Result judge(Hand other) { | |
return judgeMap.get(other); | |
} | |
} | |
private final static int HAND_SIZE = 3; | |
private final Random random = new Random(); | |
private final Map<Hand, Judge> judges; | |
private int winCount; | |
private int loseCount; | |
{ | |
judges = new EnumMap<>(Hand.class); | |
judges.put(Hand.ROCK, new Judge(() -> { | |
Map<Hand, Result> map = new EnumMap<>(Hand.class); | |
map.put(Hand.PAPER, Result.LOSE); | |
map.put(Hand.SCISSORS, Result.WIN); | |
map.put(Hand.ROCK, Result.DRAW); | |
return map; | |
})); | |
judges.put(Hand.PAPER, new Judge(() -> { | |
Map<Hand, Result> map = new EnumMap<>(Hand.class); | |
map.put(Hand.SCISSORS, Result.LOSE); | |
map.put(Hand.ROCK, Result.WIN); | |
map.put(Hand.PAPER, Result.DRAW); | |
return map; | |
})); | |
judges.put(Hand.SCISSORS, new Judge(() -> { | |
Map<Hand, Result> map = new EnumMap<>(Hand.class); | |
map.put(Hand.ROCK, Result.LOSE); | |
map.put(Hand.PAPER, Result.WIN); | |
map.put(Hand.SCISSORS, Result.DRAW); | |
return map; | |
})); | |
} | |
public RockPaperScissors() { | |
for(;;) { | |
System.out.println("\nRock-Paper-Scissors GO!"); | |
System.out.println("1: Rock, 2: Paper, 3: Scissors"); | |
String otherHand = System.console().readLine(); | |
try { | |
if (match(otherHand)) { | |
continue; | |
} | |
} catch (FormatException ex) { | |
System.out.println("It's not llegal Hand!\nTry Again!"); | |
continue; | |
} | |
System.out.println("Continue?: Y or N"); | |
String answer = System.console().readLine(); | |
if (!answer.trim().equalsIgnoreCase("y")) { | |
System.out.println("Total Win: " + winCount); | |
System.out.println("Total Lose: " + loseCount); | |
break; | |
} | |
} | |
} | |
private boolean match(String otherHand) throws FormatException { | |
Hand mine = select(); | |
Hand other = parse(otherHand); | |
System.out.println("My hand is: " + mine); | |
System.out.println("Your hand is: " + other); | |
Result result = judges.get(mine).judge(other); | |
System.out.println(result); | |
if (result == Result.WIN) { | |
winCount++; | |
} else if (result == Result.LOSE) { | |
loseCount++; | |
} | |
return result == Result.DRAW; | |
} | |
private Hand parse(String hand) throws FormatException { | |
try { | |
int index = Integer.parseInt(hand); | |
if (index > 0 && index < 4) { | |
return Hand.values()[index - 1]; | |
} else { | |
throw new FormatException("Illegal Number: " + index); | |
} | |
} catch (NumberFormatException ex) { | |
throw new FormatException(ex); | |
} | |
} | |
private Hand select() { | |
return Hand.values()[random.nextInt(HAND_SIZE)]; | |
} | |
public static void main(String... args) { | |
new RockPaperScissors(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こちらに移行しました
https://github.com/skrb/RockPaperScissors