Reply
Creating a "Guess the Number" Game
Old 10-01-2007, 03:29 AM Creating a "Guess the Number" Game
MGT-Evelath's Avatar
Average Talker

Posts: 21
This is what I've got so far. I'm trying to get a random between 1 and 100 created. Then announce if the user is too low, or too high when they enter a number.

PHP Code:
<html>

<head>
<title>"I'm thinking of a number"</title>
</head>

<body>

<?php

if (isset($_POST['GuessNumber']))
{
  
$SecretNumber rand(1,100);
}


if (
$SecretNumber == $GuessNumber)
{
  print 
"Correct!!";
}


else if (
$SecretNumber $GuessNumber)
{
  print 
"Too high";
}

else if (
$SecretNumber $GuessNumber)
{
  print 
"Too low";
}

else 
{
print 
"Please enter a number";
}
?>

<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
I'm thinking of a number from 1 to 100<br>
<input type="hidden" name="SecretNumber" value="<?php $SecretNumber ?>">
<input type="text" name="GuessNumber" value="<?php $GuessNumber ?>">
<input type="submit" value="Submit">
</form>

</body>
Any assistance would be great.
MGT-Evelath is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 10-01-2007, 05:46 AM Re: Creating a "Guess the Number" Game
mtishetsky's Avatar
King Spam Talker

Posts: 1,063
Name: Mike
Location: Mataro, Spain
What is the problem? How to store the number between pages?
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-01-2007, 11:23 AM Re: Creating a "Guess the Number" Game
Super Talker

Posts: 118
the way you've written this, you would need to have register_globals on. This is a bad coding practice however, so I would recommend turning it off and manually getting the values of your post elements. Also, you should sanitize them before you put them into variables.
__________________
flann
Free mortgage calculator
flann is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 02:04 PM Re: Creating a "Guess the Number" Game
MGT-Evelath's Avatar
Average Talker

Posts: 21
Ok, I changed it around to this:

PHP Code:
<html>

<head>
<title>I'm thinking of a number</title>
</head>

<body>


<?php

$SecretNumber 
$_REQUEST['SecretNumber'];
$GuessNumber $_REQUEST['GuessNumber'];

if (!isset(
$SecretNumber))
{
  
$SecretNumber rand(0,100);
}

if (
$_POST['SecretNumber'] == $GuessNumber)
{
  echo 
"Correct!!! You have guessed the number in ... attempt";
}

else if (
$_POST['GuessNumber'] > $SecretNumber)
{
  echo 
"Too high";
}

else if (
$_POST['GuessNumber'] < $SecretNumber)
{
  echo 
"Too low";
}

else
{
  echo 
"You should enter a number";
}

?>

<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
I'm thinking of a number from 1 to 100<br>
<input type="hidden" name="SecretNumber" value="<?php $SecretNumber ?>">
<input type="text" name="GuessNumber" value="<?php $GuessNumber ?>">
<input type="submit" value="Submit">
</form>

</body>
The error I get is that I can't actually get to the complete number. It is always too high or too low. And (if you use the code you'll see), When there is nothing in the input box it still comes up with "too high" or "too low" or "correct".
MGT-Evelath is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 02:11 PM Re: Creating a "Guess the Number" Game
Super Talker

Posts: 118
try this:
PHP Code:
<html>

<head>
<title>I'm thinking of a number</title>
</head>

<body>


<?php

$SecretNumber 
$_POST['SecretNumber'];
$GuessNumber $_POST['GuessNumber'];

if (!isset(
$SecretNumber))
{
  
$SecretNumber rand(0,100);
}

if (
$_POST['SecretNumber'] == $GuessNumber)
{
  echo 
"Correct!!! You have guessed the number in ... attempt";
}

else if (
$_POST['GuessNumber'] > $SecretNumber)
{
  echo 
"Too high";
}

else if (
$_POST['GuessNumber'] < $SecretNumber)
{
  echo 
"Too low";
}

else
{
  echo 
"You should enter a number";
}

?>

<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
I'm thinking of a number from 1 to 100<br>
<input type="hidden" name="SecretNumber" value="<?php echo($SecretNumber); ?>">
<input type="text" name="GuessNumber" value="<?php echo($GuessNumber); ?>">
<input type="submit" value="Submit">
</form>

</body>
__________________
flann
Free mortgage calculator
flann is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 03:56 PM Re: Creating a "Guess the Number" Game
MGT-Evelath's Avatar
Average Talker

Posts: 21
Awesome, thank you. I'm trying to display how many times it takes for the user to get to the correct number.

PHP Code:
<html>

<head>
<title>I'm thinking of a number</title>
</head>

<body>


<?php

$Amt
++;
$SecretNumber $_POST['SecretNumber'];
$GuessNumber $_POST['GuessNumber'];

if (!isset(
$SecretNumber))
{
  
$SecretNumber rand(0,100);
}

if (
$_POST['SecretNumber'] == $GuessNumber)
{
  echo 
"Correct!!! You have guessed the number in $Amt attempts";
}

else if (
$_POST['GuessNumber'] > $SecretNumber)
{
  echo 
"Too high";
}

else if (
$_POST['GuessNumber'] < $SecretNumber)
{
  echo 
"Too low";
}

else
{
  echo 
"You should enter a number";
}

?>

<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
I'm thinking of a number from 1 to 100<br>
<input type="hidden" name="SecretNumber" value="<?php echo($SecretNumber); ?>">
<input type="hidden" name="Amt" value="<?php echo($Amt); ?>">
<input type="text" name="GuessNumber" value="<?php echo($GuessNumber); ?>">
<input type="submit" value="Submit">
</form>

</body>
However it only states "1" for the amount of times.
MGT-Evelath is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 05:03 PM Re: Creating a "Guess the Number" Game
Super Talker

Posts: 118
get the posted amount then increment it by 1
__________________
flann
Free mortgage calculator
flann is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 06:11 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
This should do it

PHP Code:
<html>

<head>
<title>I'm thinking of a number</title>
</head>

<body>


<?php

$Amt
++;
$SecretNumber $_POST['SecretNumber'];
$GuessNumber $_POST['GuessNumber'];
$Amt $_POST['Amt'];

if (!isset(
$SecretNumber))
{
  
$SecretNumber rand(0,100);
}

if (
$_POST['SecretNumber'] == $GuessNumber)
{
  echo 
"Correct!!! You have guessed the number in $Amt attempts";
}

else if (
$_POST['GuessNumber'] > $SecretNumber)
{
  echo 
"Too high";
$Amt++;
}

else if (
$_POST['GuessNumber'] < $SecretNumber)
{
  echo 
"Too low";
$Amt++;
}

else
{
  echo 
"You should enter a number";
}

?>

<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
I'm thinking of a number from 1 to 100<br>
<input type="hidden" name="SecretNumber" value="<?php echo($SecretNumber); ?>">
<input type="hidden" name="Amt" value="<?php echo($Amt); ?>">
<input type="text" name="GuessNumber" value="<?php echo($GuessNumber); ?>">
<input type="submit" value="Submit">
</form>

</body>

Also if you use this kind of this as a competition, A you could easily cheat the script so you could do it and keep doing it so at a max (unless you guess it right on the first go) you could take 50 guesses and the script would think you only had two because you cheaeted..

TP apprieciated
Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-02-2007, 03:10 PM Re: Creating a "Guess the Number" Game
Average Talker

Posts: 20
Name: Bling
hmm, looks like a decent start
idkwot is offline
Reply With Quote
View Public Profile Visit idkwot's homepage!
 
Old 10-02-2007, 04:25 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
You know what they should add to forums is a "Nudge" feature, so if osmeon done replie to their thread to can send em a nudge which remind them

Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-03-2007, 07:21 AM Re: Creating a "Guess the Number" Game
Galaxian's Avatar
Dingleberry!

Posts: 592
Name: Rich
Location: United Kingdom
I'd recommend storing the secret number by other means because they can always view the secret number in the source if you put it in hidden.

Perhaps session, db, or cookie? But cookie they could just view as well lol
__________________
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 10-03-2007, 11:38 AM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
best bet is to simply have a db. a table which when they start the game, it creates a entry with their ip a session the magic number and attempts

everytime the page loads, if there is already a entry read the attempts and the magic number... etc etc..

If interested i could write it
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-03-2007, 07:15 PM Re: Creating a "Guess the Number" Game
Galaxian's Avatar
Dingleberry!

Posts: 592
Name: Rich
Location: United Kingdom
But then you've got the issue of people with shared ip like aol proxy or whatever.. Have a user system where it's assigned uniquely to the user that way it's guarenteed.

Or Session!!
__________________
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 10-04-2007, 12:16 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
*cough cough*

Quote:
their ip a session the magic
i did say use a session...

storing their ip was just well pointless but it can be handy..
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-04-2007, 12:51 PM Re: Creating a "Guess the Number" Game
Foundationflash's Avatar
Ultra Talker

Posts: 411
Name: Harry Burt
Location: Colchester, Essex, England
What about dynamic IPs?
__________________
Foundation Flash tutorials : www.foundation-flash.com

New Dreamed Up Web Design: www.dreamedupdesign.com
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-04-2007, 01:40 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Eat, Sleep, Code