Created
January 5, 2015 12:07
-
-
Save kamekoopa/d2cc9c7d3324542ce7f9 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.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.text.Normalizer; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Random; | |
import static Janken.Utility.*; | |
public class Janken { | |
static enum Hand { | |
Rock("グー"), | |
Paper("パー"), | |
Scissors("チョキ"); | |
private final String desc; | |
Hand(String desc) { | |
this.desc = desc; | |
} | |
@Override | |
public String toString() { | |
return this.desc; | |
} | |
public static Hand of(String code){ | |
switch (code){ | |
case "1": | |
return Rock; | |
case "2": | |
return Scissors; | |
case "3": | |
return Paper; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
} | |
} | |
static enum Result { | |
IWin("わたしの勝ち!"), | |
YouWin("あなたの勝ち!"), | |
Draw("あいこ!"); | |
private final String desc; | |
Result(String desc) { | |
this.desc = desc; | |
} | |
@Override | |
public String toString() { | |
return this.desc; | |
} | |
} | |
static class HandPair { | |
private final Hand myHand; | |
private final Hand yoursHand; | |
public HandPair(Hand myHand, Hand yoursHand) { | |
this.myHand = myHand; | |
this.yoursHand = yoursHand; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
HandPair handPair = (HandPair) o; | |
return myHand == handPair.myHand && yoursHand == handPair.yoursHand; | |
} | |
@Override | |
public int hashCode() { | |
int result = myHand.hashCode(); | |
result = 31 * result + yoursHand.hashCode(); | |
return result; | |
} | |
} | |
static class Judge { | |
private final static Map<HandPair, Result> resultMap = new HashMap<HandPair, Result>(){{ | |
put(new HandPair(Hand.Rock , Hand.Scissors), Result.IWin); | |
put(new HandPair(Hand.Paper , Hand.Rock) , Result.IWin); | |
put(new HandPair(Hand.Scissors, Hand.Paper) , Result.IWin); | |
put(new HandPair(Hand.Rock , Hand.Paper) , Result.YouWin); | |
put(new HandPair(Hand.Paper , Hand.Scissors), Result.YouWin); | |
put(new HandPair(Hand.Scissors, Hand.Rock) , Result.YouWin); | |
}}; | |
public Result judge(Hand myHand, Hand yourHand){ | |
if(myHand == yourHand){ | |
return Result.Draw; | |
}else{ | |
return resultMap.get(new HandPair(myHand, yourHand)); | |
} | |
} | |
} | |
static class Service { | |
private final Judge judge; | |
private final Random random; | |
private int yourWins; | |
private int yourRoses; | |
public Service() { | |
this.judge = new Judge(); | |
this.random = new Random(); | |
this.yourWins = 0; | |
this.yourRoses = 0; | |
} | |
public void battle() throws IOException { | |
println("じゃんけんするよ!何出す??"); | |
HandPair hands = getHands(); | |
Result result; | |
while((result = judge.judge(hands.myHand, hands.yoursHand)) == Result.Draw){ | |
println(String.format("あなたは%s、わたしも%s。%sもう1回!", hands.yoursHand, hands.myHand, result)); | |
hands = getHands(); | |
} | |
println(String.format("あなたは%sで、わたしは%s。%s", hands.yoursHand, hands.myHand, result)); | |
applyResult(result); | |
println(String.format("いま、%d勝%d敗だよ!", yourWins, yourRoses)); | |
} | |
private HandPair getHands() throws IOException { | |
return new HandPair(decideMyHand(), readYourHand()); | |
} | |
private void applyResult(Result result){ | |
if(result == Result.IWin){ | |
yourRoses++; | |
}else{ | |
yourWins++; | |
} | |
} | |
private Hand decideMyHand() { | |
return Hand.values()[random.nextInt(3)]; | |
} | |
private Hand readYourHand() throws IOException { | |
println("1 グー"); | |
println("2 チョキ"); | |
println("3 パー"); | |
print("1〜3の数字を打ち込んでEnter押してね!⇛"); | |
String input = readLineFromConsole(); | |
try { | |
return Hand.of(Normalizer.normalize(input, Normalizer.Form.NFKD)); | |
}catch(IllegalArgumentException e){ | |
throw new IOException(String.format("入力エラー![%s]", input)); | |
} | |
} | |
public static boolean isContinue() throws IOException { | |
println("もう1回?"); | |
println("続けるなら何か入力してEnterを押してね!やめるならEnterのみ押してね!"); | |
String input = readLineFromConsole(); | |
return !input.isEmpty(); | |
} | |
} | |
static class Utility { | |
private Utility(){} | |
public static void print(String str){ | |
System.out.print(str); | |
} | |
public static void println(String str){ | |
System.out.println(str); | |
} | |
public static void printNl(){ | |
System.out.println(); | |
} | |
public static void printlnErr(String str){ | |
System.err.println(str); | |
} | |
public static String readLineFromConsole() throws IOException { | |
return new BufferedReader(new InputStreamReader(System.in)).readLine().trim(); | |
} | |
} | |
public static void main(String... args) { | |
Service service = new Service(); | |
while(true){ | |
try{ | |
service.battle(); | |
printNl(); | |
if(Service.isContinue()){ | |
println("よし、もう1回〜"); | |
}else{ | |
println("おしまい。またね。"); | |
break; | |
} | |
}catch (IOException e){ | |
printNl(); | |
printlnErr(e.getLocalizedMessage()); | |
}catch (Exception e){ | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment