当前位置:Gxlcms > PHP教程 > php实现的简易扫雷游戏实例_php技巧

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

时间:2021-07-01 10:21:17 帮助过:47人阅读

本文实例讲述了php实现的简易扫雷游戏。分享给大家供大家参考。具体如下:

  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. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  110. <title>扫雷游戏</title>

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

人气教程排行