Page 107 - Beginning PHP 5.3
P. 107

Chapter 4: Decisions and Loops
                             The built-in abs() function determines the absolute value of a number. For example, abs(3) is 3, and
                             abs(-3) is also 3.

                         The next loop in the script is also a do...while loop, and comprises the main body of the simulation. The
                         first code within the loop uses decision-making to simulate the pigeon’s homing instinct. It simply checks
                         to see if the x coordinate of the pigeon is greater or less than the x coordinate of the home square, and
                         adjusts the pigeon’s x coordinate appropriately. The y coordinate is adjusted in the same way:
                               // Move the pigeon closer to home

                               if ( $pigeonX < $homeX )
                                 $pigeonX++;
                               elseif ( $pigeonX > $homeX )
                                 $pigeonX--;
                               if ( $pigeonY < $homeY )
                                 $pigeonY++;
                               elseif ( $pigeonY > $homeY )
                                 $pigeonY--;
                             Note that if the x or y coordinate of the pigeon matches the corresponding home coordinate, there’s no
                             need to adjust the pigeon’s coordinate. Hence there is no else code branch.
                         The last section of code within the loop is concerned with displaying the current map. This code itself
                         comprises two nested for loops that move through all the x and y coordinates of the map. For each
                         square within the map, the code displays a + symbol if the square matches the coordinates of the home
                         position, and a % symbol if the square matches the pigeon coordinates. Otherwise, it displays a dot (.).
                         After each square, it adds a space character (unless it’s the last square on the row):

                               // 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”;




                                                                                                          69





                                                                                                      9/21/09   8:52:13 AM
          c04.indd   69
          c04.indd   69                                                                               9/21/09   8:52:13 AM
   102   103   104   105   106   107   108   109   110   111   112