Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Beginning PHP 5.3
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP 5.3
This is the forum to discuss the Wrox book Beginning PHP 5.3 by Matt Doyle; ISBN: 978-0-470-41396-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 5.3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 16th, 2012, 09:35 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Please help me tweak the Homing pigeon exercise

I'm going trough the book of Matt Doyle, Beginning PHP 5.3, so far so good, but for the sake of fun, I would like to take the Homing pigeon exercises a bit further and I'm getting stuck. The "pigeon" (%) gets assigned a random x and y and it goes "home" (+).

I tweaked the code to have two pigeons and was working fine, but now I try to make it with an eagle, called "sas" (W) that should ignore the "home" and focus on hunting the pigeon. Whoever gets first, the pigeon home, or the eagle to catch the pigeon, the game should stop.

I can't get the eagle to hunt the pigeon, I don't know what I'm doing wrong... It's behaving the same way as the pigeon...

I wonder if anyone could point me to the right direction. Thanks.

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Homing pigeon simulator</title>
<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.bird{
        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);
        
$sasX rand (0$mapSize-1);
        
$sasY rand (0$mapSize-1);
} while ( (
abs($homeX $pigeonX ) < $mapSize/2) && (abs$homeY $pigeonY ) < $mapSize/2) && (abs($pigeonX $sasX ) < $mapSize/2) && (abs$pigeonY $sasY ) < $mapSize/2));

//move the pigeon closer to target
do {
        if ( 
$pigeonX $homeX)
                
$pigeonX++;
        elseif ( 
$pigeonX $homeX)
                
$pigeonX--;
        if ( 
$pigeonY $homeY )
                
$pigeonY++;
        elseif (
$pigeonY $homeY)
                
$pigeonY--;
        if ( 
$sasX $pigeonX)
                
$sasX++;
        elseif ( 
$sasX $pigeonX)
                
$sasX--;
        if ( 
$sasY $pigeonY )
                
$sasY++;
        elseif (
$sasY $pigeonY)
                
$sasY--;
        
        
//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="bird">%</span>'// Pigeon   
                        
} elseif ($x == $sasX && $y == $sasY) {
                                echo 
'<span class="bird">W</span>';
                        }       else {
                                echo 
'<span class="empty">.</span>';
                        }
                        
                        echo ( 
$x != $mapSize-1) ? " " " ";
                }
                echo 
"\n";
        }
        echo 
"</pre></div>\n";
} while (
$pigeonX != $homeX || $pigeonY != $homeY || $pigeonX != $sasX || $pigeonY != $sasY);
?>

</body>
</html>
Reply With Quote
  #2 (permalink)  
Old February 25th, 2012, 06:06 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Here is the solution

Hi all,

A dear friend has helped me out with this puzzle and showed me how to solve it. On page 44, you get more info about the difference between == and ===

To make it easier to understand the eagle is E and the pigeon is P:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Homing pigeon simulator</title> 
<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.bird{ 
        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); 
        $sasX = rand (0, $mapSize-1); 
        $sasY = rand (0, $mapSize-1); 
} while ( 
	(abs($homeX - $pigeonX ) < $mapSize/2) 
	&& (abs( $homeY - $pigeonY ) < $mapSize/2) 
	&& (abs($pigeonX - $sasX ) < $mapSize/2) 
	&& (abs( $pigeonY - $sasY ) < $mapSize/2)
); 

//move the pigeon closer to target 
do { 
        if ( $pigeonX < $homeX)
            $pigeonX++; 
        elseif ( $pigeonX > $homeX) 
            $pigeonX--; 
        if ( $pigeonY < $homeY ) 
            $pigeonY++; 
        elseif ($pigeonY > $homeY) 
            $pigeonY--; 
        if ( $sasX < $pigeonX) 
            $sasX++; 
        elseif ( $sasX > $pigeonX) 
            $sasX--; 
        if ( $sasY < $pigeonY ) 
            $sasY++; 
        elseif ($sasY > $pigeonY) 
            $sasY--; 
         
        //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 ) { 
                        	// Home
                        	echo '<span class="home">+</span>';      
                        } elseif ( $x === $sasX && $y === $sasY ) { 
                        	// Eagle 
                            echo '<span class="bird">E</span>'; 
                        } elseif ( $x === $pigeonX && $y === $pigeonY ) {
	                        // Pigeon 
                            echo '<span class="bird">P</span>'; 
                        } else { 
                            echo '<span class="empty">.</span>'; 
                        } 
                         
                        echo  " "; 
                } 
                echo "\n"; 
        } 
        echo "</pre></div>\n"; 
} while (
	!($pigeonX === $homeX && $pigeonY === $homeY) 
	&& !($pigeonX === $sasX && $pigeonY === $sasY)
);

?> 

</body> 
</html>
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exercise 1.5 TallThunda BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 7 May 27th, 2013 04:37 PM
Lesson 4 Exercise 2 Rennuke BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 3 January 17th, 2013 07:49 PM
Chapter 8 exercise 1 Will BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 March 2nd, 2010 02:26 PM
Chapter 5 exercise 3 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 27th, 2009 02:41 PM
Please, could anyone help me with this exercise? java2007 Java Basics 3 December 10th, 2007 11:19 AM



All times are GMT -4. The time now is 04:32 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.