[PHP]代码
<?php
$init = $_POST["init"];//game restart
$clickvalue = $_POST["clickvalue"];//minesweeping
$checkflag = 0;//Victory or defeat
$click_count = 0;//clicks count
if($init == null && $clickvalue == null){//initialization
$_POST = array();//set POST with a array
$_POST["rows"] = 9;//set rows
$_POST["cols"] = 9;//set cols
$_POST["num"] = 10;//set num
$_POST["timeshow"] = "00:00"; //set starttime
$init = true;//set initialization
}
$rows = $_POST["rows"];//get rows
$cols = $_POST["cols"];//get cols
$num = $_POST["num"];//get num
$starttime = $_POST["starttime"];//get starttime
if($init){// is initialization
$timeshow = "00:00";//set starttime
$data = array();//data initialization
for($i=0;$i<$rows;$i++){//all the rows
for($j=0;$j<$cols;$j++){//all the cols
$data["data".$i."_".$j] = 0;//set mine with null
$data["open".$i."_".$j] = 0;//set node with close
}
}
$i=0;//reset the index,and set the mines(Random setting)
while($i < $num){//number of mine
$r = rand(0,$rows - 1);//row's index
$c = rand(0,$cols - 1);//col's index
if($data["data".$r."_".$c] == 0){//if not a mine
$data["data".$r."_".$c] = 100;//set the node with a mine
$i++;
}
}
for($i=0;$i<$rows;$i++){//all the rows
for($j=0;$j<$cols;$j++){//all the cols
if($data["data".$i."_".$j] == 100)continue;//is not a mine , set number of adjacent mines
$cnt = 0;
if($i - 1 >= 0 && $j - 1 >= 0 && $data["data".($i - 1)."_".($j - 1)] == 100)$cnt++;//upper left
if($i - 1 >= 0 && $data["data".($i - 1)."_".$j] == 100)$cnt++;//left
if($i - 1 >= 0 && $j + 1 = 0 && $data["data".$i."_".($j - 1)] == 100)$cnt++;//upper
if($j + 1 = 0 && $data["data".($i + 1)."_".($j - 1)] == 100)$cnt++;//upper right
if($i + 1 9?$min:"0".$min).":".($sec>9?$sec:"0".$sec);
}else{
$timeshow = "00:00";//if game is stop , time stop
}
function openNode($i,$j){//set nodes to open,if it is can open
global $rows;//get the rows
global $cols;//get the cols
global $data;//get the data
if($i = $rows || $j = $cols || $data["open".$i."_".$j])return;//it is not a node,or it has been opened
$data["open".$i."_".$j] = 1;//open the node
if($data["data".$i."_".$j] > 0)return;//need to continue?
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);
}
?>
<meta>
<title>扫雷游戏</title>
<script>
function clickNum(value){//click a node
<?php if($checkflag > 0)echo 'return;';//if game is clear or game is over ?>
document.forms[0].clickvalue.value = value;
document.forms[0].submit();
}
<?php if($checkflag == 0 && $click_count>0)echo 'setTimeout("timerun()",1000);';//time running ?>
<?php if($checkflag == 1)echo 'alert("恭喜,雷全部清掉了!");';?>
<?php if($checkflag == 2)echo 'alert("太挫了,又被雷炸死了");';?>
function timerun(){//time running
var timelist = document.forms[0].timeshow.value.split(":");
var sec = parseInt(timelist[1],10) + 1;
var min = 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>
使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










