Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 21
Trades: 0
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
 
 
Register now for full access!
Old 10-01-2007, 06:46 AM Re: Creating a "Guess the Number" Game
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
What is the problem? How to store the number between pages?
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

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, 12:23 PM Re: Creating a "Guess the Number" Game
Super Talker

Posts: 130
Trades: 0
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

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
flann is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 03:04 PM Re: Creating a "Guess the Number" Game
MGT-Evelath's Avatar
Average Talker

Posts: 21
Trades: 0
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, 03:11 PM Re: Creating a "Guess the Number" Game
Super Talker

Posts: 130
Trades: 0
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

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
flann is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 04:56 PM Re: Creating a "Guess the Number" Game
MGT-Evelath's Avatar
Average Talker

Posts: 21
Trades: 0
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, 06:03 PM Re: Creating a "Guess the Number" Game
Super Talker

Posts: 130
Trades: 0
get the posted amount then increment it by 1
__________________
flann

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
flann is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 07:11 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
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
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-02-2007, 04:10 PM Re: Creating a "Guess the Number" Game
Average Talker

Posts: 20
Name: Bling
Trades: 0
hmm, looks like a decent start
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
idkwot is offline
Reply With Quote
View Public Profile Visit idkwot's homepage!
 
Old 10-02-2007, 05:25 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
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
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-03-2007, 08:21 AM Re: Creating a "Guess the Number" Game
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 10-03-2007, 12:38 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
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
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-03-2007, 08:15 PM Re: Creating a "Guess the Number" Game
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
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!!
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 10-04-2007, 01:16 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
*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..
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-04-2007, 01:51 PM Re: Creating a "Guess the Number" Game
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
What about dynamic IPs?
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-04-2007, 02:40 PM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
What about them?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-06-2007, 11:07 PM Re: Creating a "Guess the Number" Game
Average Talker

Posts: 22
Name: Paul
Trades: 0
This script could come in handy for a word game.....

hmmm......
__________________
Free Web Hosting Available At Simply Web Host

Please login or register to view this content. Registration is FREE
smithster is offline
Reply With Quote
View Public Profile Visit smithster's homepage!
 
Old 10-07-2007, 06:40 AM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
i know its one of those things which when i get round to i wil mae could be fun Dam
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-07-2007, 08:49 AM Re: Creating a "Guess the Number" Game
Average Talker

Posts: 22
Name: Paul
Trades: 0
From what I understand so far, I think you would need to create an array of say 1-200 words. And you could keep the rest of the script the same as it is. Would that work? Unfortunately, I still haven't learnt how to use arrays yet.
__________________
Free Web Hosting Available At Simply Web Host

Please login or register to view this content. Registration is FREE
smithster is offline
Reply With Quote
View Public Profile Visit smithster's homepage!
 
Old 10-07-2007, 11:45 AM Re: Creating a "Guess the Number" Game
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
words?....

This is about a guess the NUMBER game...
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to Creating a "Guess the Number" Game

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.54679 seconds with 12 queries