<?php
$source
="m.php";

 if(
$_GET["show"]=="true") {
   
highlight_file($source);
   };

echo 
"<html><head><title></title>";
echo 
"</head>\n<body>\n";


$vararray = array("left","right","forward","back","map","facing","x","y","source");
$varstart count($vararray);
$startmap "###N####\n#      #\n#      #\nW  *   E\n#      #\n#      #\n####S###";


$step=1;

if (isset(
$HTTP_POST_VARS['submit'])){
  
$step=2;
  } else {
  
$step=1;
  };

echo 
"<h1></h1>\n";
echo 
"<h2></h2>\n";
echo 
"<hr>\n";

if (
$step==1) {
  
$facing 0;
  echo 
"<p>\n";
  echo 
"<form name=\"map\" action=\"m.php\" method=\"post\">\n<table border=\"0\"><tr>";
  
$b=0;
  echo 
"<td width=\"300\">&nbsp;</td><td><p>The map</p>\n";
  echo 
"<textarea name=\"".$vararray[4]."\" rows=\"10\" cols=\"10\">".$startmap."</textarea><br>\n";
  echo 
"<input type=\"submit\" name=\"submit\" value=\"Draw\">&nbsp;&nbsp;<input type=\"submit\" name=\"submit\" value=\"Explore\">&nbsp;&nbsp;<input type=\"reset\" name=\"reset\">";
  echo 
"</td></tr></table>\n";
  echo 
"</form>\n";
  };


if (
$step==2) {
  
$action $_POST["submit"];
  
$map $_POST[$vararray[4]];
  
$facing $_POST[$vararray[5]];


  
$yarray preg_split("/[\n]+/"$map);  
  
$maparray = array();
  
$lines count($yarray);
  
$x $_POST[$vararray[6]];
  
$y $_POST[$vararray[7]];
  for (
$a=0;$a<=$lines;$a++){
    
$maparray[$a] = preg_split('//',  $yarray[$a], -1PREG_SPLIT_NO_EMPTY); 
    };
  if (isset(
$x)){
    } else {
    for (
$a=0;$a<=$lines;$a++){
      if (
in_array ("*",$maparray[$a])){
        
$y $a;
        
$x array_search("*"$maparray[$a]); 
        };
      };
    };


  switch (
$facing) {
    case 
0:
      
$dx=0;
      
$dy=-1;
      
$direction="north";
      break;
    case 
1:
      
$dx=1;
      
$dy=0;
      
$direction="east";
      break;
    case 
2:
      
$dx=0;
      
$dy=1;
      
$direction="south";
      break;
    case 
3:
      
$dx=-1;
      
$dy=0;
      
$direction="west";
      break;
    };

  switch (
$action) {
    case 
$vararray[0]:
      if (
$facing>0) {
        
$facing--;
        } else {
        
$facing 3;
        };
      break;
    case 
$vararray[1]:
      if (
$facing<3) {
        
$facing++;
        } else {
        
$facing 0;
        };
      break;
    case 
$vararray[2]:
      if (
$maparray[$x+$dx][$y+$dy]=" ") {
        
$x=$x+$dx;
        
$y=$y+$dy;
        };
      break;
    case 
$vararray[3]:
      if (
$maparray[$x-$dx][$y-$dy]=" ") {
        
$x=$x-$dx;
        
$y=$y-$dy;
        };
      break;
    };


  switch (
$facing) {
    case 
0:
      
$dx=0;
      
$dy=-1;
      
$direction="north";
      break;
    case 
1:
      
$dx=1;
      
$dy=0;
      
$direction="east";
      break;
    case 
2:
      
$dx=0;
      
$dy=1;
      
$direction="south";
      break;
    case 
3:
      
$dx=-1;
      
$dy=0;
      
$direction="west";
      break;
    };


  
$Look = array ();          // contents of each cell
  
$lx = array();             // x offsets
  
$ly = array();             // y offsets
  
for ($d=0;$d<3;$d++){      // distance from observer
    
for ($c=0;$c<7;$c++){    // cell left to right
      
$lx[$c][$d] = $x+($dx*($d+1))-($dy*($c-3));
      if (
$lx[$c][$d]<0) {
        
$lx[$c][$d]=0;
        };
      
$ly[$c][$d] = $y+($dy*($d+1))-($dx*($c-3));
      if (
$ly[$c][$d]<0) {
        
$ly[$c][$d]=0;
        };
      };
    };

  echo 
"<p>\n";
  echo 
"<form name=\"map\" action=\"m.php\" method=\"post\">\n<table border=\"0\"><tr>";



  if (
$action == "Draw") {
    echo 
"<td width=\"300\"><p>The map</p>\n";
    echo 
"<pre>\n";
    for (
$d=2;$d>-1;$d--){      // distance from observer
      
for ($c=0;$c<7;$c++){    // cell left to right
      
echo $maparray[$ly[$c][$d]][$lx[$c][$d]];
      };
    echo 
"\n";
    };  
    echo 
"   *</pre>\n";
    echo 
"<p>Facing : ".$direction."</p>\n";

    } else {

    echo 
"<td width=\"300\"><p>The map</p>\n";
    echo 
"<pre>\n";
    for (
$d=2;$d>-1;$d--){      // distance from observer
      
for ($c=0;$c<7;$c++){    // cell left to right
      
echo $maparray[$ly[$c][$d]][$lx[$c][$d]];
      };
    echo 
"\n";
    };  
    echo 
"   *</pre>\n";
    echo 
"<p>Facing : ".$direction."</p>\n";

    echo 
"</td><td><p>Exploring the Map-</p><hr>\n<table><tr>";
    for (
$a=0;$a<4;$a++){
      echo 
"<td><input type=\"submit\" name=\"submit\" value=\"".$vararray[$a]."\"></td>";
      };
    echo 
"</tr></table>\n";
    };

  echo 
"</td></tr>\n<tr><td><p>Map :-<br>\n";
  echo 
"<textarea name=\"".$vararray[4]."\" rows=\"10\" cols=\"10\">".$map."</textarea><br>\n";
  echo 
"<input type=\"submit\" name=\"submit\" value=\"Draw\">&nbsp;&nbsp;<input type=\"submit\" name=\"submit\" value=\"Explore\">&nbsp;&nbsp;<input type=\"reset\" name=\"reset\">";
  echo 
"</td></tr></table>\n";
  echo 
"<input type=\"hidden\" name=\"".$vararray[5]."\" value=\"".$facing."\">\n";
  echo 
"<input type=\"hidden\" name=\"".$vararray[6]."\" value=\"".$x."\">\n";
  echo 
"<input type=\"hidden\" name=\"".$vararray[7]."\" value=\"".$y."\">\n";
  echo 
"</form>\n";


  echo 
"<p>Debug info</p>\n";
//  echo "<p>Map Array</p>\n";
//    print_r ($maparray);
//  echo "<p>Co-ordinates</p>\n";
//    echo "\n<p>X: ".$x." | Y: ".$y."</p>\n";
//  echo "<p>Cells viewed</p>\n";
//  echo "<p>X offset</p>\n";
//    print_r ($lx);
//  echo "<p>Y offset</p>\n";
//    print_r ($ly);
//  echo "<p>Square in front</p>\n";
//  echo "<p>|".$maparray[$x+$dx][$y+$dy]."|</p>\n";
  
};


?>

<p>View the <A HREF="<?php echo "$source?show=true" ?>">source</A>.</p>
</body>
</html>


 

The map


    

View the source.