Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

Guessing Game

Code Wizard, 12-30-2006

This uses a form and random numbers to play a numerical guessing game - based on the card game 'war'.
The user sets the points to play for, and the computer player guesses a number, and the computer sets
the true number. Whoever is closest wins the points. It only requires this file plus two images, one named
red.jpg and one named blue.jpg so the graph showing who is winning works. It could easily be rewritten to have
a character in place of the images, if you like.

//Guessing Game by Stuart Coutchie
//Reversed Polarity WebDesign
//www.reversedpolarity.com
//
//Feel free to use this to your advantage!
//

<html>
<head>
</head>
<body>
<center>
<font face="Arial Black" size="3">Guessing Game</font>
</center>
<hr>
<?php


if ($play == "play") {

//setting the random numbers

//computers guess
srand ((double) microtime() * 1000000);
$myguess = rand (0,20);

//real answer
srand ((double) microtime() * 1000000);
$thevalue = rand (0,20);

//displaying the guesses from the previous results
echo "Your last guess was: ";
echo $guess;
echo "
<br>";
echo "My last guess was: ";
echo $myguess;
echo "
<br>";
echo "<br>";
echo "The right answer was: ";
echo $thevalue;
echo "
";
echo "<br>";
echo $message;
echo "
";

//setting the difference of the guesses vs the real answer
$yourdiff=$guess-$thevalue;
$yourdiffcorrected=abs($yourdiff);

$mydiff=$myguess-$thevalue;
$mydiffcorrected=abs($mydiff);


//adding up the scores
if ($yourdiffcorrected>$mydiffcorrected)
{
$myscore=$points+$myscore;
$yourscore=0+$yourscore;
$message="I won the points";
}

if ($yourdiffcorrected<$mydiffcorrected)
{
$yourscore=$points+$yourscore;
$myscore=0+$myscore;
$message="You won the points";
}

if ($yourdiffcorrected==$mydiffcorrected)
{
$yourscore=$points+$yourscore;
$myscore=$points+$myscore;
$message="We tied, so we both get points";
}
echo "<br>";
echo $message;
echo "
";
}

?>

<br><br>
Closest to a number wins points
<br>

<?php
echo "<form action="$PHP_SELF?action=direct">";
?>

Lets play for how many points?
<br>
<?php
echo "<INPUT TYPE="text" NAME="points" SIZE="10" value="".$points."">";
?>
<br>
<br>
Guess a number between 0 and 20
<br>
<input type="text" name="guess" size="10">
<?php


echo "<input type="hidden" name="yourscore" value="".$yourscore."">
";
echo "<input type="hidden" name="myscore" value="".$myscore."">
";
echo "<INPUT TYPE="submit" NAME="play" VALUE="play">
";
echo "</FORM>
";
?>


<?php



//setting the message telling who is winning

if ($play == "play") {


if ($myscore==$yourscore){
$mymessage="We're Tied";
$yourmessage="We're Tied";

}
if ($myscore>$yourscore){
$mymessage="I'm winning";
$yourmessage="Your losing";

}
if ($myscore<$yourscore){
$yourmessage="Your winning";
$mymessage="I'm losing";

}

//displaying the table showing the scores
echo " <table border="1" width="450">
";
echo "<tr>
";
echo "<td width="50"><font face="Arial Narrow" size="1">ME</font></td>
";
echo "<td width="50"><font face="Arial Narrow" size="1">";
echo $myscore;
echo "</font></td>
";
echo "<td width="250"><img src="red.jpg" height="10"width="".$myscore.""></td>
";
echo "<td width="150">".$mymessage."</td>
";
echo "</tr>";


echo "<tr>
";
echo "<td width="50"><font face="Arial Narrow" size="1">You</font></td>
";
echo "<td width="50"><font face="Arial Narrow" size="1">";
echo $yourscore;
echo "</font></td>
";
echo "<td width="250"><img src="blue.jpg" height="10"width="".$yourscore.""></td>
";
echo "<td width="150">".$yourmessage."</td>
";
echo "</tr>";
echo "</table>";


//resetting the form if you want to start over
//by setting the play variable to something other than 'play'
echo "<form action="$PHP_SELF?action=direct">";
echo "<input type="submit" name="play" value="Start Over">";

}

?>
</body>
</html>
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions