プレイヤーが選択


【Source Code】
<body> から Init() 関数と Check() 関数と Play() 関数を呼び出します。
選択した手の画像が表示されることを確認して下さい。
じゃんけんの回数(cnt)がカウントされることも確認して下さい。
<body>
<h2>プレイヤーが選択</h2>

<script type="text/javascript">
Init();
Check();
Play();
</script>

param["mode"] == 0 でじゃんけんをします。
プレイヤーはラジオボタンで選択します。
コンピュータ側のプレイは、今回は 0(グー)に設定しています。
// プレイ関数
function Play()
{
    if (param["mode"]==0)   //手を選択
    {
        document.write('<form action="jyanken2.html" method="get">');
        document.write('<input type="hidden" name="mode" value=1>');
        document.write('<input type="hidden" name="score_m" value=',param["score_m"],'>');
        document.write('<input type="hidden" name="score_c" value=',param["score_c"],'>');
        document.write('<input type="hidden" name="cnt" value=',param["cnt"],'>');
        document.write('<input type="hidden" name="com" value=0>');
        document.write('<input type=radio name="man" value=0 CHECKED>グー<br>');
        document.write('<input type=radio name="man" value=1>チョキ<br>');
        document.write('<input type=radio name="man" value=2>パー<br>');
        document.write('<input type="submit" value="送信">');
        document.write('</form>');
        return;
    }

param["mode"] == 1 で本来は勝負の判定をします。
今回はプレイヤーが選択した画像を表示してみました。
「確認」ボタンで param["mode"] を 0 にしてじゃんけんモードに戻ります。
    if (param["mode"]==1)   //画像の表示
    {
        var man = param["man"];
        if (man==0) document.write('<img src="jgu.gif"><br>');
        if (man==1) document.write('<img src="jchi.gif"><br>');
        if (man==2) document.write('<img src="jpa.gif"><br>');
        document.write('<form action="jyanken2.html" method="get">');
        document.write('<input type="hidden" name="mode" value=0>');
        document.write('<input type="hidden" name="score_m" value=',param["score_m"],'>');
        document.write('<input type="hidden" name="score_c" value=',param["score_c"],'>');
        document.write('<input type="hidden" name="cnt" value=',(param["cnt"]+1),'>');
        document.write('<input type="submit" value="確認">');
        document.write('</form>');
    }
}
</script>

[Next Chapter ↓] じゃんけんゲーム-3
[Previous Chapter ↑] じゃんけんゲーム-1

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