ajjp5o7 ajjp5o7 - 5 months ago 6
PHP Question

Generating a Map with PHP

Hello I am trying to learn PHP and I've been using the WROX Beginning PHP 5.3 textbook.
The book has an exercise that generates a map and two coordinates assigned to a "Home" and "Pigeon".

Here is the site:

http://ajjpreble.com/learning-php/homing_pigeon.php

And here is the code:

<!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Strict// EN"
"http://www.w3c.org/TR/xhtml/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="utf-8">
<title>Homing Pigeon Simulator</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">


div.map { float: left; text-align: center; border: 1px solid #666; background-color: #fcfcfc; margin: 5px; padding: 1em;}
span.home, span.pigeon {font-weight: bold:}
span.empty { color: #666;}

</style>
</head>
<body>
<?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 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 && $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>


My question is how is the map displayed? What is telling the map that there is an X and a Y coordinate and that it should not be more than 10?

Answer

how is the map displayed?

for ($y = 0; $y < $mapSize; $y++) {
    for ($x = 0; $x < $mapSize; $x++ ) {...}
}

The nested for loops mean "for each x coordinate, of each y coordinate..." As a result we go through each point of the map. At each point, what we draw depends on the following conditions:

if ( $x == $homeX && $y == $homeY )...

Means if it's the location of the home, draw +.

elseif ( $x == $pigeonX && $y == $pigeonY )...

Means elseif it's the location of the pigeon, draw %

else {...}

Means otherwise, just draw . The actual drawing is done with the echo command in each logical branch.

What is telling the map that there is an X and a Y coordinate

This isn't the right question. Suppose you draw a grid of lines on a page. You wouldn't ask "What is telling the page that there are lines?" The page is just the canvas and you act on it. The map is the same way. It just reflects what the code draws on it. How the drawing appears is a mix of what I just discussed above and the CSS code that determines what things look like (everything between the <style> tags)

What is telling the map that the coordinates should not be more than 10?

The map size is determined by how many values of x and y are allowed (this should make sense since we draw the map by cycling through every x and every y as the answer to the first question shows)

 $mapSize = 10;
 $homeX = rand ( 0, $mapSize-1 );
 $homeY = rand ( 0, $mapSize-1 );
 $pigeonX = rand ( 0, $mapSize-1 );
 $pigeonY = rand ( 0, $mapSize-1 );

The first line determines the size of the map because the for loops at the bottom go from coordinates 0 through $mapSize-1. The next 4 lines determine the location of the home an the pigeon. Using random numbers within the 0-9 range guarantees that when the map is drawn, it will contain both the home and the pigeon

Comments