php实现的简易扫雷游戏实例_php技巧

本文实例讲述了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 < $cols && $data["data".($i - 1)."_".($j + 1)] == 100)$cnt++;//lower left
      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++;//upper right
      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++;//lower right
      $data["data".$i."_".$j] = $cnt;//set number
    }
  }
}else{
  $data = $_POST;//get data
  if($data["data".$clickvalue] == 100){
  //check the value of users click
    $checkflag = 2;//if click on a mine,gameover
    for($i=0;$i<$rows;$i++){//all the rows
      for($j=0;$j<$cols;$j++){//all the cols
        $data["open".$i."_".$j] = 1;
        //set all nodes to open
      }
    }
  }else{
    $node = explode("_", $clickvalue);//get the node of click
    openNode($node[0],$node[1]);//set nodes to open
    for($i=0;$i<$rows;$i++){//all the rows
      for($j=0;$j<$cols;$j++){//all the cols
        if($data["open".$i."_".$j] == 1)$click_count++;
        //get the number of opennode
      }
    }
    if($rows*$cols - $click_count == $num)$checkflag = 1;
    //if all the node is open,game clear
  }
}
if($checkflag == 0 && $click_count == 1){
//if game is start ,time start
  $starttime = date("H:i:s");
}
if($starttime){//Computing time and display
  $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";//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 < 0 || $i >= $rows || $j < 0 || $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);
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>扫雷游戏</title>
</head>
<body>
<form action="" method="post">
<input type="hidden" name="starttime" value="<?php echo $starttime;?>"/>
<input type="hidden" name="clickvalue"/>
<table style="top:10px;left:0px;z-index:0;margin:10px auto" border="1px">
<tr>
<td width="100px" align="center">
  <table width="100%" border="1px">
    <tr><td>行数:</td><td><input type="text" name="rows" value="<?php echo $rows;?>" size="1"/></td></tr>
    <tr><td>列数</td><td><input type="text" name="cols" value="<?php echo $cols;?>" size="1"/></td></tr>
    <tr><td>雷数:</td><td><input type="text" name="num" value="<?php echo $num;?>" size="1"/></td></tr>
    <tr><td colspan="2" align="center"><input type="submit" value="重新开始" name="init"/></td></tr>
  </table>
</td>
<td width="50px" align="center"><font size="10px"><?php echo $checkflag < 2?"":"☹";?></font></td>
<td width="100px" align="center">
<?php
  if($checkflag == 1)echo "恭喜,雷全部清掉了!<br />";
  else if($checkflag == 2)echo "太挫了,又被雷炸死了<br />";
?>
  <input type="text" name="timeshow" value="<?php echo $timeshow;?>" size="4" readonly >
</td>
</tr>
</table>
<table style="top:155px;left:0px;z-index:0;margin:10px auto" border="1px">
<?php for($i=0;$i<$rows;$i++){ ?>
  <tr>
  <?php for($j=0;$j<$cols;$j++){  ?>
    <td style="width:24px;height:24px;" align="center">
    <input type="hidden" name="open<?php echo $i."_".$j;?>" value="<?php echo $data["open".$i."_".$j];?>">
    <input type="hidden" name="data<?php echo $i."_".$j;?>" value="<?php echo $data["data".$i."_".$j];?>">
    <?php if($data["open".$i."_".$j]){//show the value of node,if the node has been opened ?>
      <?php echo $data["data".$i."_".$j]==100?"":$data["data".$i."_".$j];?>
    <?php }else{//show a button ,if the node has not been opened ?>
      <input type="button" value="" onclick="clickNum('<?php echo $i."_".$j;?>')" style="width:20px;height:20px;">
    <?php } ?>
    </td>
  <?php } ?>
  </tr>
<?php } ?>
</table>
</form>
<script type="text/javascript">
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>
</body>
</html>

希望本文所述对大家的php程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php
扫雷游戏
最简易扫雷c语言代码、java简易扫雷程序、php简易的扫雷制作、mfc简易扫雷源码、扫雷技巧,以便于您获取更多的相关知识。

时间: 2024-09-14 17:03:32

php实现的简易扫雷游戏实例_php技巧的相关文章

jQuery+PHP实现的掷色子抽奖游戏实例_php技巧

本文实例讲述了jQuery+PHP实现的掷色子抽奖游戏详细步骤.分享给大家供大家参考.具体分析如下: 该游戏是以大富翁游戏为背景,综合运用jQuery和PHP知识,设计出以掷色子点数来达成抽奖的效果,当然抽奖概率是可控的,开发者可以将本实例稍作修改即可运用到网站中的抽奖活动场景中.效果图如下: 完整实例代码点击此处本站下载. HTML部分: 首先我们需要准备两粒色子和奖品素材,这些作者已经打包上传了,请大家放心下载.我们将在html页面中写下如下的html结构代码,.wrap用来放置色子和提示信

php开发的简易扫雷游戏

我觉得用PHP+JS可以设计出挺牛逼的二维网页游戏,当然网络型的网页游戏后台数据的交互可以使用java来做数据处理. 开发的简易扫雷游戏-扫雷游戏开发">   <?php     $init = $_POST["init"];//game restart   $clickvalue = $_POST["clickvalue"];//minesweeping   $checkflag = 0;//Victory or defeat   $clic

php实现的美国50个州选择列表实例_php技巧

本文实例讲述了php实现的美国50个州选择列表.分享给大家供大家参考.具体如下: 这里展示的是php生成的美国50个州的选择列表,自动选择当前州 <select name="state" id="state"> <option value="AL" <?PHP if($state=="AL") echo "selected";?>>Alabama</option&g

php将图片保存为不同尺寸图片的图片类实例_php技巧

本文实例讲述了php将图片保存为不同规格的图片类.分享给大家供大家参考.具体如下: 图片处理类.imagecls.php如下: <?php /** 图片处理类 */ class imagecls { /** * 文件信息 */ var $file = array(); /** * 保存目录 */ var $dir = ''; /** * 错误代码 */ var $error_code = 0; /** * 文件上传最大KB */ var $max_size = -1; function es_i

php判断linux下程序问题实例_php技巧

本文实例讲述了php判断linux下程序问题.分享给大家供大家参考.具体如下: 有时候在服务器上面写一些脚本的时候,经常要放到crontab里面定时运行.时间长了就有一个问题,那就是程序重复运行消耗太多的资源,怎么处理呢?下面璞玉写了两种方法. //第一种:用linux里面的正则匹配 function ifrun($clsname,$bf = 0) { //下面进行检测,如有一个进程正在运行,则不运行 $str=shell_exec("/bin/ps ax > /home/root/&qu

php实现用于计算执行时间的类实例_php技巧

本文实例讲述了php实现用于计算执行时间的类.分享给大家供大家参考.具体如下: 有了这个php类,计算函数或者一段代码的执行时间就简单了 <?php class c_Timer { var $t_start = 0; var $t_stop = 0; var $t_elapsed = 0; function start() { $this->t_start = microtime(); } function stop() { $this->t_stop = microtime(); }

基于Python实现的扫雷游戏实例代码_python

本文实例借鉴mvc模式,核心数据为model,维护1个矩阵,0表无雷,1表雷,-1表已经检测过. 本例使用python的tkinter做gui,由于没考虑可用性问题,因此UI比较难看,pygame更有趣更强大更好看,做这些小游戏更合适,感兴趣的读者可以尝试一下! 具体的功能代码如下: # -*- coding: utf-8 -*- import random import sys from Tkinter import * class Model: """ 核心数据类,维护一

js制作简易年历完整实例_javascript技巧

本文实例讲述了js制作简易年历的方法.分享给大家供大家参考.具体如下: 今天学习了一下用js来实现年历的制作,顺便复习了一下this的用法,跟选项卡的制作有点差别,新用到了innerHTML,希望自己坚持下去,各位js大神也多多指点. innerHtml的用法 现在用top.innerHTML="..........";的方法就可以向这个id的位置写入HTML代码了. 例如top.innerHTML="";就可以在top对应的位置出现一个button了! 程序实现思

微信 开发生成带参数的二维码的实例_php技巧

微信开发生成带参数的二维码的讲解 在微信公众号平台开发者那里,在"账号管理"那里,有一项功能是"生成带参数的二维码",通过这儿生成的二维码,只要通过微信扫一扫之后,会把事件自动推送到微信公众号上 用微信扫一扫推送到开发者网址那儿的数据如下: <xml><ToUserName><![CDATA[gh_452****b0f2]]></ToUserName> <FromUserName><![CDATA[o