Created
December 27, 2014 03:49
-
-
Save hishidama/f6d631553ddd78a97c37 to your computer and use it in GitHub Desktop.
Javaじゃんけん3
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.Comparator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Optional; | |
import java.util.Random; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
/** | |
* http://blog.goo.ne.jp/hishidama/e/ee5bacfceb7179b942d3392bb83e21a6 | |
* <p> | |
* JDK1.8 | |
* </p> | |
* | |
* @author hishidama | |
*/ | |
public class JankenExample3 { | |
public enum Hand { | |
/** グー */ | |
ROCK("グー", "1") { | |
@Override | |
public boolean isWinTo(Hand other) { | |
return other == SCISSORS; | |
} | |
}, | |
/** チョキ */ | |
SCISSORS("チョキ", "2") { | |
@Override | |
public boolean isWinTo(Hand other) { | |
return other == PAPER; | |
} | |
}, | |
/** パー */ | |
PAPER("パー", "3") { | |
@Override | |
public boolean isWinTo(Hand other) { | |
return other == ROCK; | |
} | |
}; | |
private final String label; | |
private final String key; | |
private Hand(String label, String key) { | |
this.label = label; | |
this.key = key; | |
} | |
public String label() { | |
return label; | |
} | |
public String key() { | |
return key; | |
} | |
public abstract boolean isWinTo(Hand other); | |
} | |
private static final List<Hand> HAND_LIST; | |
private static final String HAND_MIN_KEY; | |
private static final String HAND_MAX_KEY; | |
private static final Map<String, Hand> KEY_HAND_MAP; | |
static { | |
HAND_LIST = Arrays.asList(Hand.values()); | |
HAND_MIN_KEY = HAND_LIST.stream().map(Hand::key).min(Comparator.naturalOrder()).get(); | |
HAND_MAX_KEY = HAND_LIST.stream().map(Hand::key).max(Comparator.naturalOrder()).get(); | |
KEY_HAND_MAP = HAND_LIST.stream().collect(Collectors.toMap(Hand::key, Function.identity())); | |
} | |
protected final BufferedReader reader; | |
int winCount = 0; | |
int loseCount = 0; | |
public JankenExample3(BufferedReader reader) { | |
this.reader = reader; | |
} | |
public void loop() throws IOException { | |
for (;;) { | |
Hand input = getInputHand(); | |
Hand other = getComputerHand(); | |
if (!judge(input, other)) { | |
continue; | |
} | |
if (isEnd()) { | |
break; | |
} | |
} | |
} | |
protected Hand getInputHand() throws IOException { | |
for (;;) { | |
for (Hand hand : HAND_LIST) { | |
System.out.printf("%s %s\n", hand.key(), hand.label()); | |
} | |
System.out.printf("%s〜%sを入力してEnterを押して下さい。→", HAND_MIN_KEY, HAND_MAX_KEY); | |
String line = reader.readLine(); | |
if (line == null) { | |
throw new IOException("EOF"); | |
} | |
Optional<Hand> input = parseInput(line); | |
if (input.isPresent()) { | |
return input.get(); | |
} | |
System.out.println("認識できない文字でした。もう一度入力して下さい。"); | |
} | |
} | |
protected Optional<Hand> parseInput(String key) { | |
return Optional.ofNullable(KEY_HAND_MAP.get(key)); | |
} | |
private final Random random = new Random(); | |
protected Hand getComputerHand() { | |
return HAND_LIST.get(random.nextInt(HAND_LIST.size())); | |
} | |
protected boolean judge(Hand input, Hand other) { | |
assert input != null; | |
assert other != null; | |
if (input == other) { | |
System.out.printf("あなたは%s、わたしは%sで、あいこでした。\n", input.label(), other.label()); | |
return false; | |
} | |
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); | |
return true; | |
} | |
protected boolean isEnd() throws IOException { | |
System.out.println(); | |
System.out.println("もう一度やりますか?"); | |
System.out.println("続ける場合は何か入力してEnterを押して下さい。やめる場合はEnterのみを押して下さい。"); | |
String line = reader.readLine(); | |
if (line == null || line.isEmpty()) { | |
System.out.println("終了します。"); | |
return true; | |
} | |
return false; | |
} | |
public static void main(String... args) throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
new JankenExample3(reader).loop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment