php实现的简易扫雷游戏实例
本文实例讲述了php实现的简易扫雷游戏。分享给大家供大家参考。具体如下:
<?php
$init=$_POST["init"];//gamerestart
$clickvalue=$_POST["clickvalue"];//minesweeping
$checkflag=0;//Victoryordefeat
$click_count=0;//clickscount
if($init==null&&$clickvalue==null){//initialization
$_POST=array();//setPOSTwithaarray
$_POST["rows"]=9;//setrows
$_POST["cols"]=9;//setcols
$_POST["num"]=10;//setnum
$_POST["timeshow"]="00:00";//setstarttime
$init=true;//setinitialization
}
$rows=$_POST["rows"];//getrows
$cols=$_POST["cols"];//getcols
$num=$_POST["num"];//getnum
$starttime=$_POST["starttime"];//getstarttime
if($init){//isinitialization
$timeshow="00:00";//setstarttime
$data=array();//datainitialization
for($i=0;$i<$rows;$i++){//alltherows
for($j=0;$j<$cols;$j++){//allthecols
$data["data".$i."_".$j]=0;//setminewithnull
$data["open".$i."_".$j]=0;//setnodewithclose
}
}
$i=0;//resettheindex,andsetthemines(Randomsetting)
while($i<$num){//numberofmine
$r=rand(0,$rows-1);//row'sindex
$c=rand(0,$cols-1);//col'sindex
if($data["data".$r."_".$c]==0){//ifnotamine
$data["data".$r."_".$c]=100;//setthenodewithamine
$i++;
}
}
for($i=0;$i<$rows;$i++){//alltherows
for($j=0;$j<$cols;$j++){//allthecols
if($data["data".$i."_".$j]==100)continue;
//isnotamine,setnumberofadjacentmines
$cnt=0;
if($i-1>=0&&$j-1>=0&&$data["data".($i-1)."_".($j-1)]==100)$cnt++;//upperleft
if($i-1>=0&&$data["data".($i-1)."_".$j]==100)$cnt++;//left
if($i-1>=0&&$j+1<$cols&&$data["data".($i-1)."_".($j+1)]==100)$cnt++;//lowerleft
if($j-1>=0&&$data["data".$i."_".($j-1)]==100)$cnt++;//upper
if($j+1<$cols&&$data["data".$i."_".($j+1)]==100)$cnt++;//lower
if($i+1<$rows&&$j-1>=0&&$data["data".($i+1)."_".($j-1)]==100)$cnt++;//upperright
if($i+1<$rows&&$data["data".($i+1)."_".$j]==100)$cnt++;//right
if($i+1<$rows&&$j+1<$cols&&$data["data".($i+1)."_".($j+1)]==100)$cnt++;//lowerright
$data["data".$i."_".$j]=$cnt;//setnumber
}
}
}else{
$data=$_POST;//getdata
if($data["data".$clickvalue]==100){
//checkthevalueofusersclick
$checkflag=2;//ifclickonamine,gameover
for($i=0;$i<$rows;$i++){//alltherows
for($j=0;$j<$cols;$j++){//allthecols
$data["open".$i."_".$j]=1;
//setallnodestoopen
}
}
}else{
$node=explode("_",$clickvalue);//getthenodeofclick
openNode($node[0],$node[1]);//setnodestoopen
for($i=0;$i<$rows;$i++){//alltherows
for($j=0;$j<$cols;$j++){//allthecols
if($data["open".$i."_".$j]==1)$click_count++;
//getthenumberofopennode
}
}
if($rows*$cols-$click_count==$num)$checkflag=1;
//ifallthenodeisopen,gameclear
}
}
if($checkflag==0&&$click_count==1){
//ifgameisstart,timestart
$starttime=date("H:i:s");
}
if($starttime){//Computingtimeanddisplay
$now=date("H:i:s");
$nowlist=explode(":",$now);
$starttimelist=explode(":",$starttime);
$time_count=$nowlist[0]*3600+$nowlist[1]*60+$nowlist[2]-($starttimelist[0]*3600+$starttimelist[1]*60+$starttimelist[2]);
$min=floor($time_count/60);
$sec=$time_count%60;
$timeshow=($min>9?$min:"0".$min).":".($sec>9?$sec:"0".$sec);
}else{
$timeshow="00:00";//ifgameisstop,timestop
}
functionopenNode($i,$j){//setnodestoopen,ifitiscanopen
global$rows;//gettherows
global$cols;//getthecols
global$data;//getthedata
if($i<0||$i>=$rows||$j<0||$j>=$cols||$data["open".$i."_".$j])return;
//itisnotanode,orithasbeenopened
$data["open".$i."_".$j]=1;//openthenode
if($data["data".$i."_".$j]>0)return;//needtocontinue?
openNode($i-1,$j-1);
openNode($i-1,$j);
openNode($i-1,$j+1);
openNode($i,$j-1);
openNode($i,$j+1);
openNode($i+1,$j-1);
openNode($i+1,$j);
openNode($i+1,$j+1);
}
?>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
<title>扫雷游戏</title>
</head>
<body>
<formaction=""method="post">
<inputtype="hidden"name="starttime"value="<?phpecho$starttime;?>"/>
<inputtype="hidden"name="clickvalue"/>
<tablestyle="top:10px;left:0px;z-index:0;margin:10pxauto"border="1px">
<tr>
<tdwidth="100px"align="center">
<tablewidth="100%"border="1px">
<tr><td>行数:</td><td><inputtype="text"name="rows"value="<?phpecho$rows;?>"size="1"/></td></tr>
<tr><td>列数</td><td><inputtype="text"name="cols"value="<?phpecho$cols;?>"size="1"/></td></tr>
<tr><td>雷数:</td><td><inputtype="text"name="num"value="<?phpecho$num;?>"size="1"/></td></tr>
<tr><tdcolspan="2"align="center"><inputtype="submit"value="重新开始"name="init"/></td></tr>
</table>
</td>
<tdwidth="50px"align="center"><fontsize="10px"><?phpecho$checkflag<2?"☺":"☹";?></font></td>
<tdwidth="100px"align="center">
<?php
if($checkflag==1)echo"恭喜,雷全部清掉了!<br/>";
elseif($checkflag==2)echo"太挫了,又被雷炸死了<br/>";
?>
<inputtype="text"name="timeshow"value="<?phpecho$timeshow;?>"size="4"readonly>
</td>
</tr>
</table>
<tablestyle="top:155px;left:0px;z-index:0;margin:10pxauto"border="1px">
<?phpfor($i=0;$i<$rows;$i++){?>
<tr>
<?phpfor($j=0;$j<$cols;$j++){?>
<tdstyle="width:24px;height:24px;"align="center">
<inputtype="hidden"name="open<?phpecho$i."_".$j;?>"value="<?phpecho$data["open".$i."_".$j];?>">
<inputtype="hidden"name="data<?phpecho$i."_".$j;?>"value="<?phpecho$data["data".$i."_".$j];?>">
<?phpif($data["open".$i."_".$j]){//showthevalueofnode,ifthenodehasbeenopened?>
<?phpecho$data["data".$i."_".$j]==100?"☀":$data["data".$i."_".$j];?>
<?php}else{//showabutton,ifthenodehasnotbeenopened?>
<inputtype="button"value=""onclick="clickNum('<?phpecho$i."_".$j;?>')"style="width:20px;height:20px;">
<?php}?>
</td>
<?php}?>
</tr>
<?php}?>
</table>
</form>
<scripttype="text/javascript">
functionclickNum(value){//clickanode
<?phpif($checkflag>0)echo'return;';//ifgameisclearorgameisover?>
document.forms[0].clickvalue.value=value;
document.forms[0].submit();
}
<?phpif($checkflag==0&&$click_count>0)echo'setTimeout("timerun()",1000);';//timerunning?>
<?phpif($checkflag==1)echo'alert("恭喜,雷全部清掉了!");';?>
<?phpif($checkflag==2)echo'alert("太挫了,又被雷炸死了");';?>
functiontimerun(){//timerunning
vartimelist=document.forms[0].timeshow.value.split(":");
varsec=parseInt(timelist[1],10)+1;
varmin=sec<60?parseInt(timelist[0],10):(parseInt(timelist[0],10)+1);
document.forms[0].timeshow.value=(min>9?min:"0"+min)+":"+(sec>9?sec:"0"+sec);
setTimeout("timerun()",1000);
}
</script>
</body>
</html>
希望本文所述对大家的php程序设计有所帮助。