spence911 Posted November 4, 2013 Share Posted November 4, 2013 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) ? " " : " "; Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/ Share on other sites More sharing options...
Barand Posted November 4, 2013 Share Posted November 4, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1456850 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2013 Share Posted November 4, 2013 (edited) IGNORE Edited November 4, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1456851 Share on other sites More sharing options...
spence911 Posted November 4, 2013 Author Share Posted November 4, 2013 @Barand. The code is actually very useful in displaying the map. As is - It outputs a series of 10 dots across the y-axis of the map even if there are no values inside the quotations. Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1456876 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2013 Share Posted November 4, 2013 @Barand. The code is actually very useful in displaying the map. The line with the ternary operator is useless as it just echo's a space no matter what. You could simply replace it with echo " "; and you'll get the same result. Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1456880 Share on other sites More sharing options...
Barand Posted November 4, 2013 Share Posted November 4, 2013 (edited) I've done a graphical version for you to play with (attached) which creates an image. Place on a page with an ordinary HTML image tag <img src='pigeon.php' width='500' height='500' alt='pigeon'/> pigeon.php Edited November 4, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1456924 Share on other sites More sharing options...
spence911 Posted November 5, 2013 Author Share Posted November 5, 2013 I was really looking forward to playing with that code Barand. I created a pigeon.php, dropped that image tag into a seperate file, saved it and when i opened it the output it generated was just an empty frame with no image. Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1457047 Share on other sites More sharing options...
Ch0cu3r Posted November 5, 2013 Share Posted November 5, 2013 Remove the first two lines (2 and 3) from pigeon.php. The database connection is not needed. Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1457078 Share on other sites More sharing options...
spence911 Posted November 5, 2013 Author Share Posted November 5, 2013 Remove the first two lines (2 and 3) from pigeon.php. The database connection is not needed. I just removed both lines and still not working. All I'm getting is a box with a missing image :[ Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1457085 Share on other sites More sharing options...
Solution Ch0cu3r Posted November 5, 2013 Solution Share Posted November 5, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1457090 Share on other sites More sharing options...
spence911 Posted November 5, 2013 Author Share Posted November 5, 2013 (edited) IGNORE Edited November 5, 2013 by spence911 Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1457100 Share on other sites More sharing options...
spence911 Posted November 5, 2013 Author Share Posted November 5, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1457101 Share on other sites More sharing options...
Barand Posted November 5, 2013 Share Posted November 5, 2013 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) Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1457102 Share on other sites More sharing options...
spence911 Posted November 5, 2013 Author Share Posted November 5, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283573-displayingoutputting-a-simple-map/#findComment-1457113 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.