<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Homing Pigeon Simulator</title> <!-- Link to external CSS file for styling --> <link rel="stylesheet" type="text/css" href="css/common.css" /> <!-- Inline CSS for specific styles used on the page --> <style> 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; /* Make the home and pigeon icons bold */ } span.empty { color: #666; /* Style for empty cells */ } </style> </head> <body> <h1>Don Alexander Eckford\'s Redo of PHP 5.3 Homing Pigeon Example</h1> <?php
$mapSize = 10; // Set the size of the grid (10x10)
// Randomly position the home and the pigeon on the grid do { $homeX = rand(0, $mapSize - 1); // Random X-coordinate for home $homeY = rand(0, $mapSize - 1); // Random Y-coordinate for home $pigeonX = rand(0, $mapSize - 1); // Random X-coordinate for pigeon $pigeonY = rand(0, $mapSize - 1); // Random Y-coordinate for pigeon } while ( ( abs($homeX - $pigeonX) < $mapSize / 2 ) && ( abs($homeY - $pigeonY) < $mapSize / 2 ) ); // Ensure home and pigeon are not too close to each other (distance > half the map size)
// Begin the simulation loop to move the pigeon towards the home do {
// Move pigeon one step closer to home on the X-axis if ($pigeonX < $homeX) { $pigeonX++; } elseif ($pigeonX > $homeX) { $pigeonX--; }
// Move pigeon one step closer to home on the Y-axis if ($pigeonY < $homeY) { $pigeonY++; } elseif ($pigeonY > $homeY) { $pigeonY--; }
// Display the current state of the grid echo '<div class="map" style="width: ' . $mapSize . 'em;"><pre>';
// Iterate through the grid rows for ($y = 0; $y < $mapSize; $y++) {
// Iterate through the grid columns for ($x = 0; $x < $mapSize; $x++) {
// Check if this position is home if ($x == $homeX && $y == $homeY) { echo '<span class="home">+</span>'; // Home icon } // Check if this position is the pigeon elseif ($x == $pigeonX && $y == $pigeonY) { echo '<span class="pigeon">%</span>'; // Pigeon icon } // Otherwise, this is an empty square else { echo '<span class="empty">.</span>'; // Empty square }
// Add space between columns, except at the end of a row echo ($x != $mapSize - 1) ? " " : ""; }
// End of the row, move to the next line echo "\n"; }
echo "</pre></div>\n";
// Continue the loop until the pigeon reaches the home } while ($pigeonX != $homeX || $pigeonY != $homeY);
?>
</body> </html> <?php // Include the footer file (assumed to contain closing HTML or scripts) include('includes/footer.php'); ?>