//package myapp.game;
import java.io.*;
import java.lang.*;
import java.util.*;
class Janken
{
Janken janken = new Janken();
janken.start();
}
public Janken() {
}
String[] handname
= {"グー",
"チョキ",
"パー"};
int cpuhand = 0;
int playerhand = 0;
int playcount = 0;
int playerwin = 0;
int cpuwin = 0;
int playerwinwin = 0;
int cpuwinwin = 0;
int playermaxwinwin = 0;
int cpumaxwinwin = 0;
int brain = 0;
int handswitch = 0;
public void start
() throws java.
lang.
Exception { System.
out.
println("わたしとじゃんけんをしましょう!"); init();
mainloop();
}
private void init() {
cpuhand = rand.nextInt(3);
playcount = 0;
playerwin = 0;
cpuwin = 0;
playerwinwin = 0;
cpuwinwin = 0;
playermaxwinwin = 0;
cpumaxwinwin = 0;
brain = rand.nextInt(11);
handswitch = rand.nextInt(2);
}
private void switchHand() {
handswitch = 1 - handswitch;
}
private void nextHand() {
switch (brain) {
case 0:
cpuhand = (cpuhand + 1) % 3; // lose hand of my hand
break;
case 1:
cpuhand = (cpuhand + 2) % 3; // win hand of my hand
break;
case 2:
cpuhand = (playerhand + 1) % 3; // lose hand of your hand
break;
case 3:
cpuhand = (playerhand + 2) % 3; // win hand of your hand
break;
case 4:
cpuhand = (cpuhand + handswitch) % 3; // not win hand of my hand
break;
case 5:
cpuhand = (cpuhand + handswitch * 2) % 3; // not lose hand of my hand
break;
case 6:
cpuhand = (cpuhand + handswitch + 1) % 3; // win or lose hand of my hand
break;
case 7:
cpuhand = (playerhand + handswitch) % 3; // not win hand of your hand
break;
case 8:
cpuhand = (playerhand + handswitch * 2) % 3; // not lose hand of your hand
break;
case 9:
cpuhand = (playerhand + handswitch + 1) % 3; // win or lose hand of your hand
break;
default:
cpuhand = playerhand; // your hand
}
switchHand();
}
private boolean checkHands() {
int diff = cpuhand - playerhand;
System.
out.
println("わたしの手:" + handname
[cpuhand
]); System.
out.
println("あなたの手:" + handname
[playerhand
]); playcount++;
if (diff == 0) {
playerwinwin = 0;
cpuwinwin = 0;
} else if (diff == 1 || diff == -2) {
playerwin++;
playerwinwin++;
cpuwinwin = 0;
playermaxwinwin
= Math.
max(playermaxwinwin, playerwinwin
); } else {
cpuwin++;
playerwinwin = 0;
cpuwinwin++;
cpumaxwinwin
= Math.
max(cpumaxwinwin, cpuwinwin
); }
System.
out.
println(" (あなたの勝敗:勝ち=" + playerwin
+ "、負け=" + cpuwin
+ "、引き分け=" + (playcount
- playerwin
- cpuwin
) + ")"); if (cpuwin >= 5) {
System.
out.
println("勝負あり!わたしの勝ちだね!"); return true;
} else if (playerwin >= 5) {
System.
out.
println("勝負あり!あなたの勝ちだよ!"); return true;
} else {
System.
out.
println("もっかいいくよ!"); nextHand();
}
return false;
}
private void mainloop
() throws java.
lang.
Exception { String msg
= "じゃんけん・・・( 0:" + handname
[0] + "、1:" + handname
[1] + "、2:" + handname
[2] + " ) ? ";
while ((line = in.readLine()) != null) {
if (line.matches("^[012]$")) {
playerhand
= Integer.
parseInt(line
); if (checkHands()) {
System.
out.
println("バイバイまたね"); break;
}
} else if ("bye".equals(line)) {
System.
out.
println("バイバイまたね"); break;
} else {
System.
out.
println("違うよー!とりなおし!"); }
}
}
}