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

php实现的简易扫雷游戏实例

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

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

  1. $init = $_POST["init"];//game restart
  2. $clickvalue = $_POST["clickvalue"];//minesweeping
  3. $checkflag = 0;//Victory or defeat
  4. $click_count = 0;//clicks count
  5. if($init == null && $clickvalue == null){//initialization
  6. $_POST = array();//set POST with a array
  7. $_POST["rows"] = 9;//set rows
  8. $_POST["cols"] = 9;//set cols
  9. $_POST["num"] = 10;//set num
  10. $_POST["timeshow"] = "00:00"; //set starttime
  11. $init = true;//set initialization
  12. }
  13. $rows = $_POST["rows"];//get rows
  14. $cols = $_POST["cols"];//get cols
  15. $num = $_POST["num"];//get num
  16. $starttime = $_POST["starttime"];//get starttime
  17. if($init){// is initialization
  18. $timeshow = "00:00";//set starttime
  19. $data = array();//data initialization
  20. for($i=0;$i<$rows;$i++){//all the rows
  21. for($j=0;$j<$cols;$j++){//all the cols
  22. $data["data".$i."_".$j] = 0;//set mine with null
  23. $data["open".$i."_".$j] = 0;//set node with close
  24. }
  25. }
  26. $i=0;//reset the index,and set the mines(Random setting)
  27. while($i < $num){//number of mine
  28. $r = rand(0,$rows - 1);//row's index
  29. $c = rand(0,$cols - 1);//col's index
  30. if($data["data".$r."_".$c] == 0){//if not a mine
  31. $data["data".$r."_".$c] = 100;//set the node with a mine
  32. $i++;
  33. }
  34. }
  35. for($i=0;$i<$rows;$i++){//all the rows
  36. for($j=0;$j<$cols;$j++){//all the cols
  37. if($data["data".$i."_".$j] == 100)continue;
  38. //is not a mine , set number of adjacent mines
  39. $cnt = 0;
  40. if($i - 1 >= 0 && $j - 1 >= 0 && $data["data".($i - 1)."_".($j - 1)] == 100)$cnt++;//upper left
  41. if($i - 1 >= 0 && $data["data".($i - 1)."_".$j] == 100)$cnt++;//left
  42. if($i - 1 >= 0 && $j + 1 < $cols && $data["data".($i - 1)."_".($j + 1)] == 100)$cnt++;//lower left
  43. if($j - 1 >= 0 && $data["data".$i."_".($j - 1)] == 100)$cnt++;//upper
  44. if($j + 1 < $cols && $data["data".$i."_".($j + 1)] == 100)$cnt++;//lower
  45. if($i + 1 < $rows && $j - 1 >= 0 && $data["data".($i + 1)."_".($j - 1)] == 100)$cnt++;//upper right
  46. if($i + 1 < $rows && $data["data".($i + 1)."_".$j] == 100)$cnt++;//right
  47. if($i + 1 < $rows && $j + 1 < $cols && $data["data".($i + 1)."_".($j + 1)] == 100)$cnt++;//lower right
  48. $data["data".$i."_".$j] = $cnt;//set number
  49. }
  50. }
  51. }else{
  52. $data = $_POST;//get data
  53. if($data["data".$clickvalue] == 100){
  54. //check the value of users click
  55. $checkflag = 2;//if click on a mine,gameover
  56. for($i=0;$i<$rows;$i++){//all the rows
  57. for($j=0;$j<$cols;$j++){//all the cols
  58. $data["open".$i."_".$j] = 1;
  59. //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++;
  68. //get the number of opennode
  69. }
  70. }
  71. if($rows*$cols - $click_count == $num)$checkflag = 1;
  72. //if all the node is open,game clear
  73. }
  74. }
  75. if($checkflag == 0 && $click_count == 1){
  76. //if game is start ,time start
  77. $starttime = date("H:i:s");
  78. }
  79. if($starttime){//Computing time and display
  80. $now = date("H:i:s");
  81. $nowlist = explode(":",$now);
  82. $starttimelist = explode(":",$starttime);
  83. $time_count = $nowlist[0]*3600+$nowlist[1]*60 + $nowlist[2] - ($starttimelist[0]*3600+$starttimelist[1]*60 + $starttimelist[2]);
  84. $min = floor($time_count / 60);
  85. $sec = $time_count % 60;
  86. $timeshow = ($min>9?$min:"0".$min).":".($sec>9?$sec:"0".$sec);
  87. }else{
  88. $timeshow = "00:00";//if game is stop , time stop
  89. }
  90. function openNode($i,$j){//set nodes to open,if it is can open
  91. global $rows;//get the rows
  92. global $cols;//get the cols
  93. global $data;//get the data
  94. if($i < 0 || $i >= $rows || $j < 0 || $j >= $cols || $data["open".$i."_".$j])return;
  95. //it is not a node,or it has been opened
  96. $data["open".$i."_".$j] = 1;//open the node
  97. if($data["data".$i."_".$j] > 0)return;//need to continue?
  98. openNode($i - 1,$j - 1);
  99. openNode($i - 1,$j);
  100. openNode($i - 1,$j + 1);
  101. openNode($i,$j - 1);
  102. openNode($i,$j + 1);
  103. openNode($i + 1,$j - 1);
  104. openNode($i + 1,$j);
  105. openNode($i + 1,$j + 1);
  106. }
  107. ?>
  108. 扫雷游戏

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

php

人气教程排行