Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(!isset($_GET['Answer']))
- {
- $dim_x = 20;
- $dim_y = 20;
- $cell_count = $dim_x*$dim_y;
- $moves = array();
- // MAZE CREATION
- for($x=0;$x<$cell_count;$x++){
- $maze[$x] = "01111"; // visted, NSEW
- }
- $pos = rand(0,$cell_count-1);
- $html .= "My start position is randomly set at $pos<br>";
- $Start = $pos;
- $maze[$pos]{0} = 1;
- $visited ++;
- // determine possible directions
- while($visited<$cell_count){
- $possible = "";
- if((floor($pos/$dim_x)==floor(($pos-1)/$dim_x)) and ($maze[$pos-1]{0}==0)){
- $possible .= "W";
- }
- if((floor($pos/$dim_x)==floor(($pos+1)/$dim_x)) and ($maze[$pos+1]{0}==0)){
- $possible .= "E";
- }
- if((($pos+$dim_x)<$cell_count) and ($maze[$pos+$dim_x]{0}==0)){
- $possible .= "S";
- }
- if((($pos-$dim_x)>=0) and ($maze[$pos-$dim_x]{0}==0)){
- $possible .= "N";
- }
- if($possible){
- $visited ++;
- array_push($moves,$pos);
- $direction = $possible{rand(0,strlen($possible)-1)};
- switch($direction){
- case "N":
- $maze[$pos]{1} = 0;
- $maze[$pos-$dim_x]{2} = 0;
- $pos -= $dim_x;
- break;
- case "S":
- $maze[$pos]{2} = 0;
- $maze[$pos+$dim_x]{1} = 0;
- $pos += $dim_x;
- break;
- case "E":
- $maze[$pos]{3} = 0;
- $maze[$pos+1]{4} = 0;
- $pos ++;
- break;
- case "W":
- $maze[$pos]{4} = 0;
- $maze[$pos-1]{3} = 0;
- $pos --;
- break;
- }
- $maze[$pos]{0} = 1;
- }
- else{
- $pos = array_pop($moves);
- }
- }
- $html = " <table style=\"border:2px solid black;\" cellspacing=\"0\" cellpadding=\"0\">";
- for($x=0;$x<$cell_count;$x++){
- if($x % $dim_x == 0){
- $html .= "<tr>";
- }
- $style = $maze[$x]{2}.$maze[$x]{3};
- if($x!=$pos){
- $html.= "<td class=\"c$style p$x\">.</td>";
- }
- else{
- $html .= "<td class=\"c$style p$x\"><strong>.</strong></td>";
- $Last = $x;
- }
- if(($x % $dim_x) == ($dim_x-1)){
- $html .= "</tr>";
- }
- }
- $html .= "</table>";
- }
- else
- {
- $html = "<h3 style='text-align: center;'>Sorry not good enough!</h3>";
- }
- ?>
- <html>
- <head>
- <style>
- body{line-height:5px;}
- table { margin: 20px auto; }
- td{text-align:center;}
- .c00{width:20px;height:20px;border:0px;}
- .c01{width:20px;height:20px;border:0px;border-right:1px solid black;}
- .c10{width:20px;height:20px;border:0px;border-bottom:1px solid black;}
- .c11{width:20px;height:20px;border:0px;border-bottom:1px solid black;border-right:1px solid black;}
- strong{color:red;}
- <?php
- echo ".p$Start";
- ?>
- {
- background-color: green;
- }
- <?php
- echo ".p$Last";
- ?>
- {
- background-color: red;
- }
- </style>
- </head>
- <body>
- <?php echo $html;?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement