PHP implementation of simple Minesweeper game instance, PHP minesweeper instance
In this paper, we describe the simple game of minesweeper implemented by PHP. Share to everyone for your reference. Specific as follows:
<?php $init = $_post["init"];//game Restart $clickvalue = $_post["Clickvalue"];//minesweeping $checkflag = 0;// Victory or defeat $click _count = 0;//clicks count if ($init = = = NULL && $clickvalue = = null) {//initialization $_po ST = Array ();//set POST with a array $_post["rows"] = 9;//set rows $_post["cols"] = 9;//set cols $_post["num"] = 10; Set num $_post["timeshow"] = "00:00"; Set StartTime $init = True;//set initialization} $rows = $_post["Rows"];//get rows $cols = $_post["cols"];//get cols $num = $_post["num"];//get num $starttime = $_post["StartTime"];//get starttime if ($init) {//IS initialization $timeshow = "00:00";//set starttime $data = Array ();//data initialization for ($i =0; $i < $rows; $i + +) {//all the rows for ($j = 0; $j < $cols; $j + +) {//all the cols $data ["Data". $i. " _ ". $j] = 0;//set mine with null $data [" open ". $i." _ ". $j] = 0;//set node with close}} $i =0;//reset the Index,and set of the mines (Random setting) while ($i < $num) {//number of mine $r = rand (0, $rows-1);//row's Index $c = rand (0, $cols-1);//col's index if ($data ["Data". $r. " _ ". $c] = = 0) {//if not a mine $data [" Data ". $r." _ ". $c] = 100;//set The node with a mine $i + +; }} for ($i =0; $i < $rows; $i + +) {//all the rows for ($j =0; $j < $cols; $j + +) {//all the cols if ($data ["Data". $i. " _ ". $j] = = continue); is not a mine, set number of adjacent mines $cnt = 0; if ($i-1 >= 0 && $j-1 >= 0 && $data ["Data". $i-1). " _". ($j-1)] = =) $cnt ++;//upper left if ($i-1 >= 0 && $data ["Data". $i-1). " _ ". $j] = +) $cnt ++;//left if ($i-1 >= 0 && $j + 1 < $cols && $data [" Data ". $i-1). " _". ($j + 1)] = =) $cnt ++;//lower left if ($j-1 >= 0 && $data ["Data". $i. " _". ($j-1)] = =) $cnt ++;//upper if ($j + 1 < $cols && $data ["Data". $i. " _". ($j + 1)] = +) $cnt ++;//lower if ($i + 1 < $rows && $j-1 >= 0 && $data ["Data". $i + 1). " _". ($j-1)] = =) $cnt ++;//upper Right if ($i + 1 < $rows && $data ["Data". $i + 1). " _ ". $j] = +) $cnt ++;//right if ($i + 1 < $rows && $j + 1 < $cols && $data [" Data ". $i + 1). " _". ($j + 1)] = =) $cnt ++;//lower right $data ["data". $i. " _ ". $j] = $cnt;//set number}}}else{$data = $_post;//get data if ($data [" Data ". $clickvalue] = =) {//check T He value of users click $checkflag = 2;//if click on a mine,gameover for ($i =0; $i < $rows; $i + +) {//all the rows for ($j =0; $j < $cols; $j + +) {//all the cols $data ["open". $i. " _ ". $j] = 1; Set all nodes to open}}}else{$node = Explode ("_", $clickvalue);//get the node of click Opennode ( $node [0], $node [1]),//set nodes to open for ($i =0; $i < $rows; $i + +) {//all the rows for ($j =0; $j < $cols; $j + +) {//al L The cols if ($data ["open". $i. " _ ". $j] = = 1) $click _count++; Get the NUMber of Opennode}} if ($rows * $cols-$click _count = = $num) $checkflag = 1; If all of the node is open,game clear} if ($checkflag = = 0 && $click _count = = 1) {//if game is start, Time star T $starttime = date ("H:i:s"); if ($starttime) {//computing time and display $now = Date ("H:i:s"); $nowlist = Explode (":", $now); $starttimelist = Explode (":", $starttime); $time _count = $nowlist [0]*3600+ $nowlist [1]*60 + $nowlist [2]-($starttimelist [0]*3600+ $starttimelist [1]*60 + $ STARTTIMELIST[2]); $min = Floor ($time _count/60); $sec = $time _count% 60; $timeshow = ($min >9? $min: "0". $min). ":". ($sec >9? $sec: "0". $sec); }else{$timeshow = "00:00";//if game is stop, Time stop} function Opennode ($i, $j) {//set nodes to open,if it is can ope n global $rows;//get the rows global $cols;//get the cols global $data;//get the data if ($i < 0 | | $i >= $RO WS | | $j < 0 | | $j >= $cols | | $data ["Open". $i. " _ ". $j]) return; It isn't a node,or it has beeN Opened $data ["Open". $i. " _ ". $j] = 1;//open the node if ($data [" Data ". $i." _ ". $j] > 0) return;//need to continue? Opennode ($i-1, $j-1); Opennode ($i-1, $j); Opennode ($i-1, $j + 1); Opennode ($i, $j-1); Opennode ($i, $j + 1); Opennode ($i + 1, $j-1); Opennode ($i + 1, $j); Opennode ($i + 1, $j + 1); }?>
<title>Minesweeper Games</title>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1029597.html www.bkjia.com true http://www.bkjia.com/PHPjc/1029597.html techarticle PHP Implementation of simple Minesweeper game example, PHP minesweeper example of this article describes the PHP implementation of the simple minesweeper game. Share to everyone for your reference. The following: Php $init = $_post["in ...