Skip to content

Instantly share code, notes, and snippets.

@ihcomega56
Created December 26, 2014 02:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihcomega56/4c3acab67cd7eb481b74 to your computer and use it in GitHub Desktop.
Save ihcomega56/4c3acab67cd7eb481b74 to your computer and use it in GitHub Desktop.
練習用アプリ:じゃんけん
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.Normalizer;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author ihcomega
*/
public class Janken {
private final String ROCK = "グー";
private final String PAPER = "パー";
private final String SCISSORS = "チョキ";
private int win;
private int loss;
Map<String, String> results;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public Janken() {
results = new HashMap<>();
results.put(ROCK, PAPER);
results.put(PAPER, SCISSORS);
results.put(SCISSORS, ROCK);
}
private String decideShape() {
int shape = (int) (Math.random() * 3 + 1);
return Integer.toString(shape);
}
private String transferNum(String num) {
switch (num) {
case "1":
return "グー";
case "2":
return "チョキ";
case "3":
return "パー";
default:
throw new IllegalArgumentException();
}
}
private boolean isTied(String formedShape, String selectedShape) {
if (formedShape.equals(selectedShape)) {
System.out.println("あなたは" + formedShape + "、わたしも" + selectedShape + "。あいこ!もう1回!");
return true;
} else {
return false;
}
}
private void printResult(String formedShape, String selectedShape) throws NullPointerException {
String strongerShape = results.get(formedShape);
if (strongerShape.equals(selectedShape)) {
loss++;
System.out.println("あなたは" + formedShape + "で、わたしは" + selectedShape + "。わたしの勝ち!");
} else {
win++;
System.out.println("あなたは" + formedShape + "で、わたしは" + selectedShape + "。あなたの勝ち!");
}
System.out.println("いま、" + win + "勝" + loss + "敗だよ!");
}
private void match() throws IOException {
String formedShape;
String selectedShape;
System.out.println("1 グー");
System.out.println("2 チョキ");
System.out.println("3 パー");
System.out.print("1〜3の数字を打ち込んでEnter押してね!⇛");
String inputText = Normalizer.normalize(in.readLine().trim(), Normalizer.Form.NFKD);
formedShape = transferNum(inputText);
selectedShape = transferNum(decideShape());
if (isTied(formedShape, selectedShape)) {
match();
} else {
printResult(formedShape, selectedShape);
repeat();
}
}
private void repeat() throws IOException {
System.out.println("");
System.out.println("もう1回?");
System.out.println("続けるなら何か入力してEnterを押してね!やめるならEnterのみ押してね!");
String inputText = in.readLine();
if (!inputText.isEmpty()) {
System.out.println("よし、もう1回〜");
match();
} else {
System.out.println("おしまい。またね。");
}
}
public static void main(String... args) {
Janken janken = new Janken();
try {
System.out.println("じゃんけんするよ!何出す??");
janken.match();
} catch (IOException | NullPointerException e) {
System.err.println("エラーや!");
} catch (IllegalArgumentException e) {
System.err.println("1か2か3だけを入れてね!!!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment