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

WBOY
Libérer: 2016-07-25 08:44:55
original
806 Les gens l'ont consulté

本文实例讲述了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 for($j=0;$j $data["data".$i."_".$j] = 0;//set mine with null
  21. $data["open".$i."_".$j] = 0;//set node with close
  22. }
  23. }
  24. $i=0;//reset the index,and set the mines(Random setting)
  25. while($i $r = rand(0,$rows - 1);//row's index
  26. $c = rand(0,$cols - 1);//col's index
  27. if($data["data".$r."_".$c] == 0){//if not a mine
  28. $data["data".$r."_".$c] = 100;//set the node with a mine
  29. $i++;
  30. }
  31. }
  32. for($i=0;$i for($j=0;$j if($data["data".$i."_".$j] == 100)continue;
  33. //is not a mine , set number of adjacent mines
  34. $cnt = 0;
  35. if($i - 1 >= 0 && $j - 1 >= 0 && $data["data".($i - 1)."_".($j - 1)] == 100)$cnt++;//upper left
  36. if($i - 1 >= 0 && $data["data".($i - 1)."_".$j] == 100)$cnt++;//left
  37. if($i - 1 >= 0 && $j + 1 if($j - 1 >= 0 && $data["data".$i."_".($j - 1)] == 100)$cnt++;//upper
  38. if($j + 1 if($i + 1 = 0 && $data["data".($i + 1)."_".($j - 1)] == 100)$cnt++;//upper right
  39. if($i + 1 if($i + 1 $data["data".$i."_".$j] = $cnt;//set number
  40. }
  41. }
  42. }else{
  43. $data = $_POST;//get data
  44. if($data["data".$clickvalue] == 100){
  45. //check the value of users click
  46. $checkflag = 2;//if click on a mine,gameover
  47. for($i=0;$i for($j=0;$j $data["open".$i."_".$j] = 1;
  48. //set all nodes to open
  49. }
  50. }
  51. }else{
  52. $node = explode("_", $clickvalue);//get the node of click
  53. openNode($node[0],$node[1]);//set nodes to open
  54. for($i=0;$i for($j=0;$j if($data["open".$i."_".$j] == 1)$click_count++;
  55. //get the number of opennode
  56. }
  57. }
  58. if($rows*$cols - $click_count == $num)$checkflag = 1;
  59. //if all the node is open,game clear
  60. }
  61. }
  62. if($checkflag == 0 && $click_count == 1){
  63. //if game is start ,time start
  64. $starttime = date("H:i:s");
  65. }
  66. if($starttime){//Computing time and display
  67. $now = date("H:i:s");
  68. $nowlist = explode(":",$now);
  69. $starttimelist = explode(":",$starttime);
  70. $time_count = $nowlist[0]*3600+$nowlist[1]*60 + $nowlist[2] - ($starttimelist[0]*3600+$starttimelist[1]*60 + $starttimelist[2]);
  71. $min = floor($time_count / 60);
  72. $sec = $time_count % 60;
  73. $timeshow = ($min>9?$min:"0".$min).":".($sec>9?$sec:"0".$sec);
  74. }else{
  75. $timeshow = "00:00";//if game is stop , time stop
  76. }
  77. function openNode($i,$j){//set nodes to open,if it is can open
  78. global $rows;//get the rows
  79. global $cols;//get the cols
  80. global $data;//get the data
  81. if($i = $rows || $j = $cols || $data["open".$i."_".$j])return;
  82. //it is not a node,or it has been opened
  83. $data["open".$i."_".$j] = 1;//open the node
  84. if($data["data".$i."_".$j] > 0)return;//need to continue?
  85. openNode($i - 1,$j - 1);
  86. openNode($i - 1,$j);
  87. openNode($i - 1,$j + 1);
  88. openNode($i,$j - 1);
  89. openNode($i,$j + 1);
  90. openNode($i + 1,$j - 1);
  91. openNode($i + 1,$j);
  92. openNode($i + 1,$j + 1);
  93. }
  94. ?>
  95. 扫雷游戏
  96. 行数:
    列数
    雷数:
  97. if($checkflag == 1)echo "恭喜,雷全部清掉了!
    ";
  98. else if($checkflag == 2)echo "太挫了,又被雷炸死了
    ";
  99. ?>
  100. " value="">
  101. " value="">
  102. ')" style="width:20px;height:20px;">
复制代码

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

php


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal