Jump to content

Archived

This topic is now archived and is closed to further replies.

ItsWesYo

Guess The Number

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

Share this post


Link to post
Share on other sites
Well, I'm still learning PHP.

So I doubt I could do it =\

Depends on what I have to do.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Ken's code works great, the only problem is that you have unlimited guesses.

Orio.

Share this post


Link to post
Share on other sites
I said it needed work in that area... :)

BTW, most of the coding was done in about a 1/2 hour -- the rest of the time was spent debuggiing while very tired...

Ken

Share this post


Link to post
Share on other sites
It's nicely coded, with all of the if/elses. I tried making my own script, but it became ugly and messy so I stopped. Maybe some other time :)

Orio.

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.