Home Backend Development PHP Tutorial Simple minesweeper game example implemented in php

Simple minesweeper game example implemented in php

Jul 25, 2016 am 08:44 AM

The example in this article describes a simple minesweeper game implemented in php. Share it with everyone for your reference. The details are as follows:

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

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

php


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles