今回は石取りゲームを作ります。
このゲームは二人のプレイヤーが与えられた石の山から交互に石を取り、最後に残った石を取れる方が負けです。
import java.io.*;
import java.text.*;
class Ishitori {
public static void main(String args[]) throws IOException, ParseException {
BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
DecimalFormat DF = new DecimalFormat();
String s;
Number num;
int S; // 石の数
int n; // 取る個数
//------------------------------------------------
System.out.println("★★★★★★★★★★★★");
System.out.println("石取りゲームを始めます。");
//------------------------------------------------
S = (int)(55 * Math.random()) + 4;
//------------------------------------------------
while( <1> ){
n = <2> ;
System.out.println("-----------------------------------");
System.out.println("石はあと " + S + "個あります。");
while( <3> ){
System.out.print("あなたの番です。何個取りますか? : ");
<4>
<5>
<6>
}
<7>
//------------------------------------------------
<8> {
System.out.println("あなたの負けです。");
}
//------------------------------------------------
<9> {
System.out.println("石はあと " + S + "個あります。");
System.out.println("PCの番です。");
//------------------------------------------------
<10> {
n = (int)(3 * Math.random()) + 1;
} <11> {
<12>
} <13> {
<14>
}
//------------------------------------------------
<7>
System.out.println("PCは " + n + "個取り、あと " + S + " 個残っています。\n");
<15> {
System.out.println("PCの負けです。");
}
}
}
}
}
プログラムをより強くするには人間が石を取るときに1、5、9、13、17、21・・・といった数にすればよい。
これは石の個数をSとすると、次の式の値をプログラムが石を取る個数とすればよい。
(S - 1) % 4