Jump to content

Displaying/outputting a simple map


spence911
Go to solution Solved by Ch0cu3r,

Recommended Posts

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) ? " " : " ";
Link to comment
Share on other sites

See "Ternary operator" on this page http://php.net/manual/en/language.operators.comparison.php

 

Basically

echo (condition) ? 'A' : 'B';

is shorthand for

if (condition == true) {
    echo 'A';
} else {
    echo 'B';
}

In your code it is useless as it echos the same value regardless of the test.

Link to comment
Share on other sites

  • Solution

Change lines 26 - 33 from a while loop to a do-while loop

do {
    $px = floor(rand ($px1, $homeX)/10)*10;
    $py = floor(rand ($py1, $homeY)/10)*10;
    imageline($im, $px1, $py1, $px, $py, $rte);
    imagefilledellipse($im, $px, $py, 5, 5, $wht);
    $px1 = $px;
    $py1 = $py;
} while ($px != $homeX && $py != $homeY);
Link to comment
Share on other sites

 

Change lines 26 - 33 from a while loop to a do-while loop

do {
    $px = floor(rand ($px1, $homeX)/10)*10;
    $py = floor(rand ($py1, $homeY)/10)*10;
    imageline($im, $px1, $py1, $px, $py, $rte);
    imagefilledellipse($im, $px, $py, 5, 5, $wht);
    $px1 = $px;
    $py1 = $py;
} while ($px != $homeX && $py != $homeY);

Awesome! I think this is a great example to wrap my head around image mapping with. Much appreciated.

Link to comment
Share on other sites

Remove the first two lines (2 and 3) from pigeon.php. The database connection is not needed.

Sorry about those 2 lines, I have those automatically in my php files as nearly all are DB related.

 

Are pigeon.php and the file with the image tag in the same folder - it was definitely working when it left the factory. (sample attached)

post-3105-0-21823100-1383686955_thumb.png

Link to comment
Share on other sites

Sorry about those 2 lines, I have those automatically in my php files as nearly all are DB related.

 

Are pigeon.php and the file with the image tag in the same folder - it was definitely working when it left the factory. (sample attached)

 

Yes I did finally get it to work thanks to the edits by Ch0cu3. Awesome script btw, very practical, definitely gonna enjoy it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.