Jump to content

Guess The Number


ItsWesYo

Recommended Posts

Well, I can't find any simple 'guess the number' scripts nor know how to go about creating one.

So. How would I create a simple GTN script? All I want is:

- the number range be 1-30
- if they guess and it's wrong, make it say "lower" or "higher"
- the person gets 5 chances before it says they have to start over
Link to comment
Share on other sites

It can be done in one script using sessions. I just wrote one using less than 100 lines (including the HTML) in just about an hour and I'm extremely tired and should be in bed, asleep, right now... It works ok, but still needs some checking (like if the number of guesses is over 5).
Here is the whole script as an example of how it could be done:
[code]
<?php
session_start();
$the_number = (isset($_SESSION['the_number']))?$_SESSION['the_number']:rand(1,30);
$_SESSION['the_number'] = $the_number;
$number_guesses = (isset($_SESSION['number_guesses']))?$_SESSION['number_guesses']:5;
$got_it = false;
$higher = false;
$lower = false;
if (isset($_POST['submit'])) {
switch($_POST['submit']) {
case 'Guess':
$number_guesses--;
$got_it = ($_POST['guess'] == $the_number)?true:false;
$lower = ($_POST['guess'] < $the_number)?true:false;
$higher = ($_POST['guess'] > $the_number)?true:false;
$_SESSION['number_guesses'] = $number_guesses;
break;
case 'Yes':
unset($_SESSION['number_guesses']);
$_SESSION['the_number'] = rand(1,30);
unset($_POST['submit']);
break;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>Guess the Number</title>
</head>

<body>
<form method="post">
<p>I am thinking of a number between 1 and 30.<br>
<?php
if (isset($_POST['submit'])) {
if ($got_it) {
echo 'You guessed it! The number was <span style="font-weight:bold">' . $the_number . '</span>. You had ' . $number_guesses . ' guesses left</p>';
echo 'Do you wish to try again?<input type=submit name="submit" value="Yes">';
} else {
echo "You're guess was ";
if ($lower) echo '<span style="color:red;font-weight:bold">lower</span>';
if ($higher) echo '<span style="color:lime;font-weight:bold">higher</span>';
echo ' than the number</p>';
echo 'Please enter your guess:<input name="guess" type="text" max_length="2" size="2"><br>';
echo '<input type="submit" name="submit" value="Guess">';
}
}
else {
echo 'Please enter your guess:<input name="guess" type="text" max_length="2" size="2"><br>';
echo '<input type="submit" name="submit" value="Guess">';
}
?>
</form>


</body>
</html>[/code]

Have fun.

Ken
Link to comment
Share on other sites

You probably have a script working already, but I made this just for fun.  :D

[code=php:0]
<?php
/*
numbers: 1-30
gueses: 5
higher or lower:

*/
session_start();

//configuration variables
$guesses = 5;
$min = 1; $max = 30;

echo "<b>Guess a nunmber $min and $max for a total of $guesses guesses</b><br />\n";
if(!isset($_SESSION['num']))
{
$num = mt_rand($min, $max);
$_SESSION['num'] = $num;
$_SESSION['myguess'] = $guesses-1;
}

if(isset($_POST['numguess']))
{
  $num = $_SESSION['num'];
  $numguess = $_POST['numguess'];
  $myguess = $_SESSION['myguess'];

if($myguess != 0)
{
  if($numguess > $num)
  {
  echo "Lower <br />\n";
$_SESSION['myguess']--; 
  }
  else if($numguess < $num)
  {
  echo "Higher <br />\n";
$_SESSION['myguess']--;
  }
  else
  {
  echo "Winner! The number was $num <br />\n";
  echo "A new number is already generated...take a guess<br />\n";
$_SESSION = array();
session_destroy();
  }
}//end if myguess
else
{
  echo "Loser! The number was $num <br />\n";
  echo "A new number is already generated...take a guess<br />\n";
$_SESSION = array();
session_destroy();
}

if($myguess != 0 && isset($_SESSION['num']))
{
echo "Guesses left: " . $myguess;
}
}

?>

<form method="post" action="">
Num: <input type="text" name="numguess" /><br />
<input type="submit" name="submit" value="submit" />
</form>
[/code]
Link to comment
Share on other sites

Good script, but the only problem with it is when you have one guess left and you do guess the number, it tells you "loser".
For example- my last guess (fifth guess) was 19 and I got "Loser! The number was 19".

But it's nicely done too :)

Orio.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.