import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main
(String[] args
) { Scanner scan
= new Scanner
(System.
in);
System.
out.
println("コンピュータ同士でじゃんけんをします。"); System.
out.
println("対戦するコンピュータの人数を入力して下さい。(2人以上8人以内)"); String cpuNumStr
= scan.
nextLine(); int cpuNum = 0;
if (cpuNumStr.matches("[2-8]"))
cpuNum
= Integer.
parseInt(cpuNumStr
); else {
System.
out.
println("2から8までの数字を入力して下さい。終了します。"); }
int cpuHands[] = new int[cpuNum];
int hands[] = new int[3]; // hands[0]はグー、hands[1]はチョキ、hands[2]はパーの人数
int numOfSuv = cpuNum; // 勝ち残りの人数
for (int i=0; i<cpuHands.length; i++)
cpuHands[i] = rand.nextInt(3);
cpuHandsDisp(cpuHands, hands);
numOfSuv = winnersDisp(cpuHands, hands, numOfSuv);
while (numOfSuv!=1) {
hands[0] = 0;
hands[1] = 0;
hands[2] = 0;
for (int i=0; i<cpuHands.length; i++) {
if (cpuHands[i]!=3)
cpuHands[i] = rand.nextInt(3);
}
cpuHandsDisp(cpuHands, hands);
numOfSuv = winnersDisp(cpuHands, hands, numOfSuv);
}
}
public static void cpuHandsDisp(int[] cpuHands, int[] hands) {
for (int i=0; i<cpuHands.length; i++) {
if (cpuHands[i]==3) {
} // 敗者非表示
else if (cpuHands[i]==0) {
System.
out.
println("コンピュータ"+i
+"はグーです。"); hands[0]++;
}
else if (cpuHands[i]==1) {
System.
out.
println("コンピュータ"+i
+"はチョキです。"); hands[1]++;
}
else { // cpuHands[i]==2
System.
out.
println("コンピュータ"+i
+"はパーです。"); hands[2]++;
}
}
}
public static int winnersDisp(int cpuHands[], int[] hands, int numOfSuv) {
if ((hands[0]>=1 && hands[1]>=1 && hands[2]>=1) || hands[1]+hands[2]==0 || hands[2]+hands[0]==0 || hands[0]+hands[1]==0)
System.
out.
println("あいこです。"+"\n"); else if (hands[2]==0) { // グーが勝ちの時
for (int i=0; i<cpuHands.length; i++) {
if (cpuHands[i]==0)
System.
out.
print("コンピュータ"+i
+" "); else if (cpuHands[i]==3) {
} // 敗者除外中
else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
else if (hands[0]==0) { // チョキが勝ちの時
for (int i=0; i<cpuHands.length; i++) {
if (cpuHands[i]==1)
System.
out.
print("コンピュータ"+i
+" "); else if (cpuHands[i]==3) {
}
else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
else { // パーが勝ちの時
for (int i=0; i<cpuHands.length; i++) {
if (cpuHands[i]==2)
System.
out.
print("コンピュータ"+i
+" "); else if (cpuHands[i]==3) {
}
else {
cpuHands[i]=3;
numOfSuv--;
}
}
System.
out.
println("です。"+"\n"); }
if (numOfSuv==1) {
System.
out.
println("最終勝者が確定したので終了します。"); }
return numOfSuv;
}
}