// 初期化
function Init()
{
P = new Array();
if (window.location.search.length<3)
{ // 最初の呼び出し
document.write('<audio src="start.mp3" autoplay></audio>');
document.write("あなたと私が交互に石を取り除きます<br>");
document.write("一つの山から幾らでも取り除くことが出来ます<br>");
document.write("最後の一個を取らされた方が負けです<br>");
document.write("取り除く石をクリックして下さい<br><br>");
document.write('<form action="miyama4.html" method="get">');
document.write('<input type="hidden" name="P0" value=0>');
document.write('<input type="submit" value="開始">');
document.write('</form>');
return;
}
// パラメータを取得する
var query = window.location.search.substring(1);
var parameters = query.split('&');
for(i=0; i<parameters.length; i++)
{
var element = parameters[i].split('=');
var paramName = decodeURIComponent(element[0]);
var paramValue = decodeURIComponent(element[1]);
P[i] = parseInt(paramValue);
}
}
// Parameter Check
function Check()
{
if (P.length<1)
{ document.write("null Parameter<br>");
return;
}
for(i=0; i<P.length; i++)
document.write("P", i, ":", P[i], " ");
document.write("<br>");
}
// 石を表示
function Stone(n)
{
for(i=0; i<n; i++)
{
document.write('<img src="img/jewel.gif">');
if (i%5==4) document.write(" ");
}
document.write("<br>");
}
// クリックを設定して石を表示
function Disp(n,id)
{
//window.alert("disp 関数 n:" + n + " id:" + id);
for(i=0; i<n; i++)
{
document.write('<img src="img/jewel.gif"onClick=Click(',id+i,')>');
if (i%5==4) document.write(" ");
}
document.write("<br>");
}
// マウスクリック
function Click(id)
{
//window.alert(id);
var y = Math.floor(id/20)+1;
var n = id%20;
P[y] = n;
location.href = "miyama4.html?P0=-1" + "&P1=" + P[1] + "&P2=" + P[2] + "&P3=" + P[3];
}
// プレイ関数
function Play()
{
if (P[0] == 0)
{ // 山に石を設定
P[0] = 1;
P[1] = Math.floor(Math.random()*15)+1;
P[2] = Math.floor(Math.random()*15)+1;
P[3] = Math.floor(Math.random()*15)+1;
}
if (P[0] == 1)
{ // プレイヤーがプレイする(石のクリックで次のステージ)
Disp(P[1],0);
Disp(P[2],20);
Disp(P[3],40);
}
if (P[0] == -1)
{ // コンピュータがプレイする
rc = Think();
//window.alert(rc);
switch(rc)
{ case -1: //コンピュータの勝ち
document.write('<audio src="win.mp3" autoplay></audio>');
document.write('私の勝ちです\(^o^)/<br>');
document.write('<form action="miyama4.html" method="get">');
document.write('<input type="hidden" name="P0" value=0>');
document.write('<input type="submit" value="確認">');
document.write('</form>');
break;
case 1: //コンピュータの負け
document.write('<audio src="lose.mp3" autoplay></audio>');
document.write('私の負けです(^_^;)');
document.write('<form action="miyama4.html" method="get">');
document.write('<input type="hidden" name="P0" value=0>');
document.write('<input type="submit" value="確認">');
document.write('</form>');
break;
default: //プレイの確認
Stone(P[1]);
Stone(P[2]);
Stone(P[3]);
document.write('<form action="miyama4.html" method="get">');
document.write('<input type="hidden" name="P0" value=1>');
document.write('<input type="hidden" name="P1" value=',P[1],'>');
document.write('<input type="hidden" name="P2" value=',P[2],'>');
document.write('<input type="hidden" name="P3" value=',P[3],'>');
document.write('<input type="submit" value="私のプレイを確認">');
document.write('</form>');
break;
}
}
}
// コンピュータが考える
function Think()
{
var n1; //石が残っている山の数
var n2; //石が一個だけの山の数
var mx; //石が一番多い山の番号
var flag; //設定済みフラグ
n1 = 0;
n2 = 0;
mx = 0;
flag = 0;
for(i=1; i<4; i++)
{ if (P[i]>0)
{ n1++;
if (P[i]==1) n2++;
if (P[i]>P[mx]) mx= i;
}
}
switch(n1)
{ case 0: //三個の山が空(コンピュータの勝ち)
return -1;
case 1: //石が残っている山は一個
if (n2==1) return 1; //最後の石(コンピュータの負け)
P[mx]= 1; //一個を残して全て取り除く
flag = 1;
break;
case 2: //石が残っている山は二個
if (n2==1)
{ P[w]= 0; //最後の一個を残す
flag = 1;
}
break;
case 3: //石が残っている山は三個
if (n2==2)
{ P[mx]= 1; //1,1,1 に設定
flag = 1;
}
break;
}
if (flag == 1) return 0;
var w = P[1]^P[2]^P[3];
for(i=1; i<4; i++)
{ n= P[i]^w;
if (n<P[i]) break;
}
if (i>3 || Math.random()<0.1)
{ P[mx]--;
return 0;
}
P[i]= n;
return 0;
}
|