Created
December 27, 2014 03:47
-
-
Save hishidama/f5af21417471509cc37f to your computer and use it in GitHub Desktop.
Javaじゃんけん1
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
package example; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.Random; | |
/** | |
* http://blog.goo.ne.jp/hishidama/e/ee5bacfceb7179b942d3392bb83e21a6 | |
* <p> | |
* JDK1.8 | |
* </p> | |
* | |
* @author hishidama | |
*/ | |
public class JankenExample1 { | |
public enum Hand { | |
/** グー */ | |
ROCK("グー") { | |
@Override | |
public boolean isWinTo(Hand other) { | |
return other == SCISSORS; | |
} | |
}, | |
/** チョキ */ | |
SCISSORS("チョキ") { | |
@Override | |
public boolean isWinTo(Hand other) { | |
return other == PAPER; | |
} | |
}, | |
/** パー */ | |
PAPER("パー") { | |
@Override | |
public boolean isWinTo(Hand other) { | |
return other == ROCK; | |
} | |
}; | |
private final String label; | |
private Hand(String label) { | |
this.label = label; | |
} | |
public String label() { | |
return label; | |
} | |
public abstract boolean isWinTo(Hand other); | |
} | |
private static final List<Hand> HAND_LIST = Arrays.asList(Hand.values()); | |
public void loop() throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
int winCount = 0; | |
int loseCount = 0; | |
for (;;) { | |
char c = '1'; | |
for (Hand hand : HAND_LIST) { | |
System.out.printf("%c %s\n", c++, hand.label()); | |
} | |
System.out.print("1〜3を入力してEnterを押して下さい。→"); | |
String line = reader.readLine(); | |
if (line == null) { | |
throw new IOException("EOF"); | |
} | |
Optional<Hand> inputOption = parseInput(line); | |
if (!inputOption.isPresent()) { | |
System.out.println("認識できない文字でした。もう一度入力して下さい。"); | |
continue; | |
} | |
Hand input = inputOption.get(); | |
Hand other = getComputerHand(); | |
if (input == other) { | |
System.out.printf("あなたは%s、わたしは%sで、あいこでした。\n", input.label(), other.label()); | |
continue; | |
} | |
if (input.isWinTo(other)) { | |
System.out.printf("あなたは%s、わたしは%sで、あなたの勝ちです。\n", input.label(), other.label()); | |
winCount++; | |
} else { | |
System.out.printf("あなたは%s、わたしは%sで、わたしの勝ちです。\n", input.label(), other.label()); | |
loseCount++; | |
} | |
System.out.printf("現在、%d勝%s敗です。\n", winCount, loseCount); | |
System.out.println(); | |
System.out.println("もう一度やりますか?"); | |
System.out.println("続ける場合は何か入力してEnterを押して下さい。やめる場合はEnterのみを押して下さい。"); | |
line = reader.readLine(); | |
if (line == null || line.isEmpty()) { | |
System.out.println("終了します。"); | |
break; | |
} | |
} | |
} | |
protected Optional<Hand> parseInput(String key) { | |
switch (key) { | |
case "1": | |
return Optional.of(Hand.ROCK); | |
case "2": | |
return Optional.of(Hand.SCISSORS); | |
case "3": | |
return Optional.of(Hand.PAPER); | |
default: | |
return Optional.empty(); | |
} | |
} | |
private final Random random = new Random(); | |
protected Hand getComputerHand() { | |
return HAND_LIST.get(random.nextInt(HAND_LIST.size())); | |
} | |
public static void main(String... args) throws IOException { | |
new JankenExample1().loop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment