Search the Community
Showing results for tags 'php map'.
-
This script is an exercise that i got from a text book. It simulates the path of a homing pigeon as it flies from its starting point to its home. The script is easy to comprehend. The only problem i have is the block of code that displays the map as I've highlighted below. <?php $mapsize = 10; // Position the home and the pigeon do{ $homeX = rand (0, $mapsize-1); $homeY = rand (0, $mapsize-1); $pigeonX = rand(0, $mapsize-1); $pigeonY = rand(0, $mapsize-1); } while ((abs($homeX-$pigeonX) < $mapsize/2) && (abs($homeY-$pigeonY) < $mapsize/2)); do{ // Move the pigeon closer to home if ($pigeonX < $homeX) $pigeonX++; elseif ($pigeonX > $homeX) $pigeonX--; if ($pigeonY < $homeY) $pigeonY++; elseif ($pigeonY > $homeY) $pigeonY--; // Display the current map echo '<div class="map" style="width: ' . $mapsize . 'em;"><pre>'; for ($y = 0; $y < $mapsize; $y++){ for ($x = 0; $x < $mapsize; $x++){ if ($x == $homeX AND $y == $homeY){ echo '<span class ="home">+</span>'; // Home } elseif ($x == $pigeonX && $y == $pigeonY) { echo '<span class ="pigeon">%</span>'; // Pigeon } else { echo '<span class = "empty">.</span>'; //Empty square } echo ($x != $mapsize - 1) ? " " : " "; } echo "\n"; } echo "</pre></div>\n"; } while ($pigeonX != $homeX || $pigeonY != $homeY); ?> </body> </html> How does this line affect the map? I have never come across the "?" and ":" operators, moreover the use of echo in this fashion. echo ($x != $mapsize - 1) ? " " : " ";