用PHP编写扫雷游戏


php其实不适合做游戏,但是简单的游戏还是可以实现的,下面用php 实现简单的扫雷游戏,包含注释,代码只有169行。保存为UFT-8格式。

演示地址:http://www.6688.cc/web/shaolei/shaolei.php

  1. <?php  
  2. //是根据Microsoft的挖地雷游戏编写
  3. //来源 www.bkjia.com
  4. $init = $_POST["init"];//game restart  
  5. $clickvalue = $_POST["clickvalue"];//minesweeping  
  6. $checkflag = 0;//Victory or defeat  
  7. $click_count = 0;//clicks count  
  8. if($init == null && $clickvalue == null){//initialization  
  9.     $_POST = array();//set POST with a array  
  10.     $_POST["rows"] = 9;//set rows  
  11.     $_POST["cols"] = 9;//set cols  
  12.     $_POST["num"] = 10;//set num  
  13.     $_POST["timeshow"] = "00:00"; //set starttime  
  14.     $init = true;//set initialization  
  15. }  
  16. $rows = $_POST["rows"];//get rows  
  17. $cols = $_POST["cols"];//get cols  
  18. $num = $_POST["num"];//get num  
  19. $starttime = $_POST["starttime"];//get starttime  
  20. if($init){// is initialization  
  21.     $timeshow = "00:00";//set starttime  
  22.     $data = array();//data initialization  
  23.     for($i=0;$i<$rows;$i++){//all the rows  
  24.         for($j=0;$j<$cols;$j++){//all the cols  
  25.             $data["data".$i."_".$j] = 0;//set mine with null  
  26.             $data["open".$i."_".$j] = 0;//set node with close  
  27.         }  
  28.     }  
  29.     $i=0;//reset the index,and set the mines(Random setting)  
  30.     while($i < $num){//number of mine  
  31.         $r = rand(0,$rows - 1);//row's index  
  32.         $c = rand(0,$cols - 1);//col's index  
  33.         if($data["data".$r."_".$c] == 0){//if not a mine  
  34.             $data["data".$r."_".$c] = 100;//set the node with a mine  
  35.             $i++;  
  36.         }  
  37.     }  
  38.     for($i=0;$i<$rows;$i++){//all the rows  
  39.         for($j=0;$j<$cols;$j++){//all the cols  
  40.             if($data["data".$i."_".$j] == 100)continue;//is not a mine , set number of adjacent mines   
  41.             $cnt = 0;  
  42.             if($i - 1 >= 0 && $j - 1 >= 0 && $data["data".($i - 1)."_".($j - 1)] == 100)$cnt++;//upper left  
  43.             if($i - 1 >= 0 && $data["data".($i - 1)."_".$j] == 100)$cnt++;//left  
  44.             if($i - 1 >= 0 && $j + 1 < $cols && $data["data".($i - 1)."_".($j + 1)] == 100)$cnt++;//lower left  
  45.             if($j - 1 >= 0 && $data["data".$i."_".($j - 1)] == 100)$cnt++;//upper  
  46.             if($j + 1 < $cols && $data["data".$i."_".($j + 1)] == 100)$cnt++;//lower  
  47.             if($i + 1 < $rows && $j - 1 >= 0 && $data["data".($i + 1)."_".($j - 1)] == 100)$cnt++;//upper right  
  48.             if($i + 1 < $rows && $data["data".($i + 1)."_".$j] == 100)$cnt++;//right  
  49.             if($i + 1 < $rows && $j + 1 < $cols && $data["data".($i + 1)."_".($j + 1)] == 100)$cnt++;//lower right  
  50.             $data["data".$i."_".$j] = $cnt;//set number  
  51.         }  
  52.     }  
  53. }else{  
  54.     $data = $_POST;//get data  
  55.     if($data["data".$clickvalue] == 100){//check the value of users click  
  56.         $checkflag = 2;//if click on a mine,gameover  
  57.         for($i=0;$i<$rows;$i++){//all the rows  
  58.             for($j=0;$j<$cols;$j++){//all the cols  
  59.                 $data["open".$i."_".$j] = 1;//set all nodes to open  
  60.             }  
  61.         }  
  62.     }else{  
  63.         $node = explode("_", $clickvalue);//get the node of click  
  64.         openNode($node[0],$node[1]);//set nodes to open  
  65.         for($i=0;$i<$rows;$i++){//all the rows  
  66.             for($j=0;$j<$cols;$j++){//all the cols   
  67.                 if($data["open".$i."_".$j] == 1)$click_count++;//get the number of opennode   
  68.             }  
  69.         }  
  70.         if($rows*$cols - $click_count == $num)$checkflag = 1;//if all the node is open,game clear   
  71.     }  
  72. }  
  73. if($checkflag == 0 && $click_count == 1){//if game is start ,time start  
  74.     $starttime = date("H:i:s");  
  75. }  
  76. if($starttime){//Computing time and display  
  77.     $now = date("H:i:s");  
  78.     $nowlist = explode(":",$now);  
  79.     $starttimelist = explode(":",$starttime);  
  80.     $time_count = $nowlist[0]*3600+$nowlist[1]*60 + $nowlist[2] - ($starttimelist[0]*3600+$starttimelist[1]*60 + $starttimelist[2]);  
  81.     $min = floor($time_count / 60);  
  82.     $sec = $time_count % 60;  
  83.     $timeshow = ($min>9?$min:"0".$min).":".($sec>9?$sec:"0".$sec);  
  84. }else{  
  85.     $timeshow = "00:00";//if game is stop , time stop  
  86. }  
  87. function openNode($i,$j){//set nodes to open,if it is can open  
  88.     global $rows;//get the rows  
  89.     global $cols;//get the cols  
  90.     global $data;//get the data  
  91.     if($i < 0 || $i >= $rows || $j < 0 || $j >= $cols || $data["open".$i."_".$j])return;//it is not a node,or it has been opened  
  92.     $data["open".$i."_".$j] = 1;//open the node  
  93.     if($data["data".$i."_".$j] > 0)return;//need to continue?  
  94.     openNode($i - 1,$j - 1);  
  95.     openNode($i - 1,$j);  
  96.     openNode($i - 1,$j + 1);  
  97.     openNode($i,$j - 1);  
  98.     openNode($i,$j + 1);  
  99.     openNode($i + 1,$j - 1);  
  100.     openNode($i + 1,$j);  
  101.     openNode($i + 1,$j + 1);  
  102. }  
  103. ?>  
  104. <html>  
  105. <head>  
  106. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  107. <title>minesweeper</title>  
  108. </head>  
  109. <body>  
  110. <form action="" method="post">  
  111. <input type="hidden" name="starttime" value="<?php echo $starttime;?>">  
  112. <input type="hidden" name="clickvalue">  
  113. <table style="position:absolute;top:10px;left:0px;z-index:0;" border="1px">  
  114. <tr>  
  115. <td width="100px" align="center">  
  116.     <table width="100%" border="1px">  
  117.         <tr><td>rows:</td><td><input type="text" name="rows" value="<?php echo $rows;?>" size="1"></td></tr>  
  118.         <tr><td>cols</td><td><input type="text" name="cols" value="<?php echo $cols;?>" size="1"></td></tr>  
  119.         <tr><td>num:</td><td><input type="text" name="num" value="<?php echo $num;?>" size="1"></td></tr>  
  120.         <tr><td colspan="2" align="center"><input type="submit" value="restart" name="init"></td></tr>  
  121.     </table>  
  122. </td>  
  123. <td width="50px" align="center"><font size="10px"><?php echo $checkflag < 2?"☺":"☹";?></font></td>  
  124. <td width="100px" align="center">  
  125. <?php   
  126.     if($checkflag == 1)echo "game clear!<br />";  
  127.     else if($checkflag == 2)echo "game over!<br />";  
  128. ?>  
  129.     <input type="text" name="timeshow" value="<?php echo $timeshow;?>" size="4" readonly >  
  130. </td>  
  131. </tr>  
  132. </table>  
  133. <table style="position:absolute;top:155px;left:0px;z-index:0;" border="1px">  
  134. <?php for($i=0;$i<$rows;$i++){ ?>  
  135.     <tr>  
  136.     <?php for($j=0;$j<$cols;$j++){    ?>  
  137.         <td style="width:24px;height:24px;" align="center">  
  138.         <input type="hidden" name="open<?php echo $i."_".$j;?>" value="<?php echo $data["open".$i."_".$j];?>">  
  139.         <input type="hidden" name="data<?php echo $i."_".$j;?>" value="<?php echo $data["data".$i."_".$j];?>">  
  140.         <?php if($data["open".$i."_".$j]){//show the value of node,if the node has been opened ?>  
  141.             <?php echo $data["data".$i."_".$j]==100?"☀":$data["data".$i."_".$j];?>  
  142.         <?php }else{//show a button ,if the node has not been opened ?>  
  143.             <input type="button" value="" onclick="clickNum('<?php echo $i."_".$j;?>')"  style="width:20px;height:20px;">  
  144.         <?php } ?>  
  145.         </td>  
  146.     <?php } ?>  
  147.     </tr>  
  148. <?php } ?>  
  149. </table>  
  150. </form>  
  151. <script type="text/javascript">  
  152. function clickNum(value){//click a node  
  153.     <?php if($checkflag > 0)echo 'return;';//if game is clear or game is over ?>  
  154.     document.forms[0].clickvalue.value = value;  
  155.     document.forms[0].submit();  
  156. }  
  157. <?php if($checkflag == 0 && $click_count>0)echo 'setTimeout("timerun()",1000);';//time running ?>  
  158. <?php if($checkflag == 1)echo 'alert("game clear!");';?>  
  159. <?php if($checkflag == 2)echo 'alert("game over!");';?>  
  160. function timerun(){//time running  
  161.     var timelist = document.forms[0].timeshow.value.split(":");  
  162.     var sec = parseInt(timelist[1],10) + 1;  
  163.     var min = sec < 60?parseInt(timelist[0],10):(parseInt(timelist[0],10) + 1);  
  164.     document.forms[0].timeshow.value = (min>9?min:"0"+min)+":"+(sec > 9?sec:"0"+sec);  
  165.     setTimeout("timerun()",1000);  
  166. }  
  167. </script>  
  168. </body>  
  169. </html>  

相关内容