石取ゲームβ版


【Source Code】
ページが呼び出されると new jewel_class(); で ObjectClass を生成してパラメータを取得します。
次に param["num"] から param["val"] 個の石を取り除きます。
jewel.disp(); で石を表示します。
jewel.play(); でプレイヤーとコンピュータが交互にプレイします。
<script src="jewel_class.js"></script>
</head>

<body>
<h2>石取ゲームβ版</h2>

<script type="text/javascript">
var jewel = new jewel_class();
// num から val 個の石を取り除く 
if (param["num"] >= param["val"])   param["num"] -= param["val"]; 
// 石を表示する
jewel.disp();
// プレイする
jewel.play();
</script>

石取ゲームβ版の完成です。
コンピュターを相手にゲームを楽しんで下さい。
play 関数でゲームの終了判定と、プレイヤーのプレイ、コンピュターのプレイを設定します。
「num < 2」になればゲームオーバーです。
勝敗を判定してメッセージを表示します。
num をゼロにして、"送信" ボタンのクリックで次のゲームを始めます。
    this.play = function()
    {
        if (param["num"] < 2)
        {   // ゲームの終了
            document.write('<form action="isitori3.html" method="get">');
            document.write('<input type="hidden" name="num" value=0>');
            document.write('<input type="hidden" name="max" value=1>');
            document.write('<input type="hidden" name="teban" value=1>');
            if ((param["num"]==1 && param["teban"]==1) ||
                (param["num"]==0 && param["teban"]==-1))
                document.write('<input type="text" value="私の勝ちです">');
            else
                document.write('<input type="text" value="私の負けです">');
            document.write('<input type="submit" value="送信">');
            document.write('</form>');
            return;
        }

param["teban"] == 1 のときは、プレイヤーの手番です。
石取ゲーム-2と同様に form のボタンクリックでプレイします。
        if (param["teban"] == 1)
        {   // プレイヤーがプレイする
            document.write('<form action="isitori3.html" method="get">');
            document.write('<input type="hidden" name="num" value=',param["num"],'>');
            document.write('<input type="hidden" name="max" value=',param["max"],'>');
            document.write('<input type="hidden" name="teban" value=-1>');
            w = param["max"];
            if (w>param["num"]) w = param["num"];
            document.write('<input type=radio name="val" value=1 CHECKED>1 個取る<br>');
            for(i=2; i<=w; i++)
                document.write('<input type=radio name="val" value=',i,'>',i,' 個取る<br>');
            document.write('<input type="submit" value="送信">');
            document.write('</form>');
        }

コンピュータの手番です。
param["num"] から石を取り除いて form に設定して、コンピュータ側のプレイを確認できるようにします。
        else
        {   // コンピュータがプレイする
            mx = param["max"];
            nm = param["num"];
            v = (nm+mx) % (mx+1);
            if (v==0)   v = 1;
            param["num"] -= v;
            document.write('<form action="isitori3.html" method="get">');
            document.write('<input type="hidden" name="num" value=',param["num"],'>');
            document.write('<input type="hidden" name="max" value=',param["max"],'>');
            document.write('<input type="hidden" name="teban" value=1>');
            document.write('<input type="text" value="私の番です">');
            document.write('<input type="submit" value="送信">');
            document.write('</form>');
        }
    }
}
</script>

[Previous Chapter ↑] 石取ゲーム-2

前田稔の超初心者のプログラム入門
超初心者のプログラム入門(JavaScript)