<?php
/*****************************************************
Define constants
This avoids 'magic numbers' e.g. 20 as in
think of a number between 1 and 20. If you
change 20 to 10, you have to search all your
code for 20 and change it to 10. You also
have to check for 21 and 19 (as in
if number is 21 or greater then say "Too high")
****************************************************/
define("MIN", 1);
define("MAX", 30);
/*****************************************************
Rename _REQUEST vars for readability and just in
case you change where you get them from you only
have to change it in one place - not too likely in this
case, I'll grant you
****************************************************/
// First, a default
$guess = 0;
// Then, if it exists, set $this_guess to the entered value
if(isset($_REQUEST['guess'])) $guess = $_REQUEST['guess'];
// Or you could do it this way
$number = (isset($_REQUEST['number']))? $_REQUEST['number']: 0; // Number to
guess
$low = (isset($_REQUEST['low']))? $_REQUEST['low']: MIN-1; // Nearest low
guess
$high = (isset($_REQUEST['high']))? $_REQUEST['high']: MAX+1; // Nearest
high guess
// Or this way
isset($_REQUEST['guesses']) ? $guesses = $_REQUEST['guesses'] : $guesses=0;
// Number of guesses
// Output all the stuff at the top of the page
output_header();
// The newgame field is only on the instruction page, so
// we know that we have to start a new game
if(isset($_REQUEST['submit']))
{
// This is not the first time through, so process the guess
if(isset($_REQUEST['newgame']))
{
// We need to set up the game
$number = rand (MIN,MAX);
// Phew! Glad that's over!
output_guess_form();
}
else
{
// We need to process this guess
// We know it withing MIN and MAX because
// we have javascript to ensure that it is.
// add this guess to the list
$guesses++;
// Did they manage it?
if ($guess == $number)
{
guessed_right();
}
else
{
guessed_wrong();
}
}
}
else
{
// This is the first time through (no request data) then
// we might as well put out some instructions
output_instructions();
}
?>
</body>
</html>
<?php
function guessed_right()
{
// We need this common data (we could have use the $_SERVER superglobal
instead)
global $guess;
global $guesses;
// Print out success
print " <div>Your guess of '$guess' was absolutely correct</div>";
print " <div>You took $guesses guess".(($guesses==1)?"":"es")." to get
it</div>";
// Print instructions - the newgame field indicate that we need a, er, new
game
output_instructions();
}
function guessed_wrong()
{
global $guess;
global $number;
global $low;
global $high;
// Give them a clue
print " <div>Your guess of '$guess' was too ";
print ($guess < $number)?"low":"high";
print ". Try again.</div>";
// Set the low and high guesses if nearer
if($guess < $number && $guess > $low) $low = $guess;
if($guess > $number && $guess < $high) $high = $guess;
// Show the guess form
output_guess_form();
}
function output_guess_form()
{
global $number;
global $guess;
global $guesses;
global $low;
global $high;
// Output the form
print '<div class="text" id="info">I have thought of a number between
'.MIN.' and '.MAX.'</div>';
print '<div class="text" id="info">From the information you have, you know
it is in the range '.($low + 1).'-'.($high - 1).'</div>';
print '<form action="" method="post">';
print '<input name="guess" type="text" id="guess" />';
print '<input name="submit" type="submit" accesskey="G" tabindex="2" ';
// Set up the javascript to do a bit of client-side checking
print 'onClick="if (guess.value < ';
print MIN;
print ' || guess.value > ';
print MAX;
print ') {alert(\'Invalid guess - must be between ';
print MIN;
print ' and ';
print MAX;
print '\');return false;};" value="Guess" />';
// Save the values we need next time
print '<input name="number" type="hidden" id="number" value="'.$number.'"
/>';
print '<input name="guesses" type="hidden" id="number" value="'.$guesses.'"
/>';
print '<input name="low" type="hidden" id="number" value="'.$low.'" />';
print '<input name="high" type="hidden" id="number" value="'.$high.'" />';
print '</form>';
}
function output_instructions()
{
print '<div class="text" id="info">I will think of a number between '.MIN.'
and '.MAX.' and you have to guess what it is</div>';
print '</div>';
print '<form action="" method="post">';
print '<input name="newgame" type="hidden" id="newgame" value="newgame" />';
print '<input name="submit" type="submit" accesskey="G" tabindex="2"
value="Go" />';
print '</form>';
}
function output_header()
{
print <<<text_area
<!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=iso-8859-1" />
<style>
body
{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 0.8em;
margin: 0px;
padding: 0px;
text-align:center;
}
#guess
{
width:2em;
}
</style>
<title>It's a puzzle...</title>
</head>
<body>
text_area;
}
?>
[Non-text portions of this message have been removed]
------------------------ Yahoo! Groups Sponsor --------------------~-->
Fair play? Video games influencing politics. Click and talk back!
http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/HKFolB/TM
--------------------------------------------------------------------~->
Community email addresses:
Post message: [email protected]
Subscribe: [EMAIL PROTECTED]
Unsubscribe: [EMAIL PROTECTED]
List owner: [EMAIL PROTECTED]
Shortcut URL to this page:
http://groups.yahoo.com/group/php-list
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/php-list/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/