Page 104 - Beginning PHP 5.3
P. 104

Part II: Learning the Language
                   Incidentally, you can also use a numeric argument with  break  in this way to break out of nested  switch
                 constructs (or, for example, a   switch  embedded within a  while  or  for  loop).


              Try It Out     A Homing Pigeon Simulator

                Here’s an example script that brings together some of the concepts you’ve learned in this chapter so
                far. The script graphically simulates the path of a homing pigeon as it flies from its starting point to its
                home. We’re not exactly talking 3-dimensional animated graphics here, but it gets the idea across!
                Here’s the script in all its glory:
                    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
                    <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
                      <head>
                        <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 the pigeon closer to home
                      if ( $pigeonX < $homeX )
                        $pigeonX++;
                      elseif ( $pigeonX > $homeX )
                        $pigeonX--;

                      if ( $pigeonY < $homeY )
                        $pigeonY++;





              66





                                                                                                      9/21/09   8:52:12 AM
          c04.indd   66                                                                               9/21/09   8:52:12 AM
          c04.indd   66
   99   100   101   102   103   104   105   106   107   108   109