Js超级简单游戏
脚本描述:
将以下代码添加到
& ltstyle & gt
。大细胞
背景色:# aa9966
边框:4px纯色# aa9966
文本对齐:居中;
}
。单元格{
宽度:40px
高度:40px
字体系列:Verdana,Arial
font-size:10pt;
字体粗细:粗体;
背景色:# 996633;
颜色:# ffff33
边框-顶部:2px实心# aa9966
左边边框:2px纯色# aa9966
右边框:2px solid # 663300
边框-底部:2px solid # 663300
文本对齐:居中;
}
。洞{
宽度:40px
高度:40px
背景色:# aa9966
文本对齐:居中;
}
体,h1,h2,h3,.msg,capt1,capt2 {font-family:Verdana,Comic Sans MS,Arial}
body { margin:0px;}
h 1 { font-size:28pt;字体粗细:粗体;边距-底部:0px}
H2 { font-size:16pt;边距:0px字体粗细:粗体;}
H3 { font-size:8pt;边距:0px字体粗细:粗体;}
。msg { font-size:8pt;字体粗细:粗体;}
。tab { cursor:hand;}
. capt 1 { font-size:10pt;字体粗细:粗体;}
. capt 2 { font-size:9pt;字体粗细:粗体;}
. capt 3 { font-size:14pt;字体粗细:粗体;颜色:黄色;}
. capt 4 { font-size:10pt;字体粗细:粗体;颜色:黄色;}
。但是{ font-size:9pt;字体粗细:粗体;高度:30px背景色:# aaaa99}
& lt/style & gt;
& ltBODY onLoad = " load board(4)" & gt;
& lt脚本& gt
var gsize,ghrow,ghcol,gtime,gmoves,gintervalid=-1,gshuffling
函数toggleHelp()
{
if (butHelp.value == "隐藏帮助")
{
help . style . display = " none ";
butHelp.value = "显示帮助";
}
其他
{
help . style . display = " ";
butHelp.value = "隐藏帮助";
}
}
//低和高之间的随机数
功能r(低,高)
{
return math . floor((hi-low)* math . random()+low);
}
//介于1和hi之间的随机数
函数r1(高)
{
return math . floor((hi-1)* math . random()+1);
}
//0到hi之间的随机数
功能r0(高)
{
return math . floor((hi)* math . random());
}
函数startGame()
{
shuffle();
gtime = 0;
g moves = 0;
tick time();
gintervalid = setInterval(" tick time()",1000);
}
函数stopGame()
{
if (gintervalid==-1)返回;
clear interval(gintervalid);
fldStatus.innerHTML =
gintervalid =-1;
}
函数tickTime()
{
show status();
gtime++;
}
函数checkWin()
{
var i,j,s;
if (gintervalid==-1)返回;//游戏没有开始!
如果(!isHole(gsize-1,gsize-1))返回;
for(I = 0;我& ltgsizei++)
for(j = 0;j & ltgsizej++)
{
如果(!(I = = gsize-1 & amp;& ampj = = gsize-1))//忽略最后一个块(理想情况下是一个洞)
{
if (getValue(i,j)!=(i*gsize+j+1)。toString())返回;
}
}
stop game();
s = " & lttable cellpadding=4 >;
s+= " & lt;tr & gt& lttd align=center class=capt3>。!!恭喜你。!& lt/TD & gt;& lt/tr >;;
s+= " & lt;tr class=capt4>。& lttd align = center & gt你已经做到了“+ gtime +”秒”;
s += "带"+ gmoves +"移动!& lt/TD & gt;& lt/tr >;;
s+= " & lt;tr & gt& lttd align = center class = capt4 & gt。你的速度是"+math . round(1000 * g moves/gtime)/1000+"步数/秒& lt/TD & gt;& lt/tr >;;
s+= " & lt;/table >;
fld status . innerhtml = s;
//shuffle();
}
函数showStatus()
{
fld status . innerhtml = " Time:"+gtime+"秒移动:"+g移动
}
函数showTable()
{
var i,j,s;
stop game();
s = " & lt表格边框=3单元格填充=0单元格间距= 0 bgcolor = ' # 666655 ' & gt& lttr & gt& lttd class=bigcell >;
s = s+" & lt;表格边框=0单元格填充=0单元格间距= 0 & gt”;
for(I = 0;我& ltgsizei++)
{
s = s+" & lt;tr >;;
for(j = 0;j & ltgsizej++)
{
s = s+" & lt;TD id = a _ "+I+" _ "+j+" onclick = ' move(this)' class = cell & gt;+(I * gsize+j+1)+" & lt;/TD >;;
}
s = s+" & lt;/tr >;;
}
s = s+" & lt;/table >;
s = s+" & lt;/TD & gt;& lt/tr & gt;& lt/table >;
返回s;
}
函数getCell(行,列)
{
return eval(" a _ "+row+" _ "+col);
}
函数设置值(行,列,值)
{
var v = getCell(row,col);
v.innerHTML = val
v.className = " cell
}
函数getValue(行,列)
{
// alert(row +","+col);
var v = getCell(row,col);
返回v.innerHTML
}
函数集孔(行,列)
{
var v = getCell(row,col);
v.innerHTML =
v.className = " hole
ghrow = row
ghcol = col
}
函数getRow(obj)
{
var a = obj . id . split(" _ ");
返回一个[1];
}
函数getCol(obj)
{
var a = obj . id . split(" _ ");
return a[2];
}
函数isHole(行,列)
{
return(row = = ghrow & amp;& ampcol==ghcol)?真:假;
}
函数getHoleInRow(row)
{
var I;
return (row==ghrow)?GH col:-1;
}
函数getHoleInCol(col)
{
var I;
return (col==ghcol)?GH row:-1;
}
函数shiftHoleRow(src,dest,Row)
{
var I;
//在某些情况下需要转换成整数!
src = parse int(src);
dest = parse int(dest);
if(src & lt;目标)
{
for(I = src;我& ltdesti++)
{
setValue(row,I,getValue(row,I+1));
setHole(row,I+1);
}
}
if(dest & lt;src)
{
for(I = src;我& gtdest我-)
{
setValue(row,I,getValue(row,I-1));
setHole(世界其他地区,I-1);
}
}
}
函数shiftHoleCol(src,dest,Col)
{
var I;
//在某些情况下需要转换成整数!
src = parse int(src);
dest = parse int(dest);
if(src & lt;目标)
{//alert(" src = "+src+" dest = "+dest+" col = "+col);
for(I = src;我& ltdesti++)
{//alert(parse int(I)+1);
setValue(i,col,getValue(i+1,col));
setHole(i+1,col);
}
}
if(dest & lt;src)
{
for(I = src;我& gtdest我-)
{
setValue(i,col,getValue(i-1,col));
设置孔(识别号1,col);
}
}
}
功能移动(对象)
{
var r,c,hr,HC;
if(gintervalid = =-1 & amp;& amp!gshuffling)
{
提醒(“请点击“开始游戏”按钮”)
返回;
}
r = getRow(obj);
c = getCol(obj);
if (isHole(r,c))返回;
HC = getHoleInRow(r);
如果(hc!= -1)
{
shiftHoleRow(hc,c,r);
gmoves++;
checkWin();
返回;
}
HR = getHoleInCol(c);
如果(hr!= -1)
{
shiftHoleCol(hr,r,c);
gmoves++;
checkWin();
返回;
}
}
函数无序播放()
{
var t,I,j,s,frac
gshuffling = true
frac = 100.0/(gsize *(gsize+10));
s = " %
for(I = 0;我& ltgsizei++)
{
s+= " | ";
for(j = 0;j & ltgsize+10;j++)
{
window . status = " Loading "+math . round((I *(gsize+10)+j)* frac)+s
如果(j%2==0)
{
t = r0(gsize);
while(t = = GH row)t = r0(gsize);//跳过孔
getCell(t,ghcol)。单击();
}
其他
{
t = r0(gsize);
while(t = = GH col)t = r0(gsize);//跳过孔
getCell(ghrow,t)。单击();
}
}
}
window.status =
gshuffling = false
}
功能负载板(尺寸)
{
gsize = size
board . innerhtml = showTable(gsize);
setHole(gsize-1,gsize-1);
//shuffle();
}
& lt/script & gt;
& ltdiv id = test & gt& lt/div & gt;
& lt表格单元格cellpadding = 4 & gt
& lttr & gt& lttd align = center & gt
& ltb & gt请选择难度:
& ltselect id = level onchange = " load board(parse int(level . value))" & gt;
& lt选项值= ' 3 ' & gt3 & lt/option & gt;
& lt选项值= ' 4 ' selected & gt4 & lt/option & gt;
& lt脚本& gt
for(var I = 5;我& lt=10;i++)
{
document . write(" & lt;选项值='" + i +" ' >+I+" & lt;/option >;");
}
& lt/script & gt;
& lt/select & gt;
& lt/TD & gt;& lt/tr & gt;
& lttr & gt& lttd align = center & gt
& ltinput type = button class = but value = "开始游戏" onclick = " Start game();"& gt
& lttr & gt& ltTD align = center id = fld status class = capt 2 & gt。
& lt/TD & gt;& lt/tr & gt;
& lt/table & gt;
& ltdiv id = board & gt& lt/div & gt;