Jump to content


Photo

Guess The Number


  • Please log in to reply
9 replies to this topic

#1 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 06 August 2006 - 04:04 AM

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

#2 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 06 August 2006 - 04:13 AM

That would probably be more than one script.. and complicated, how much PHP do you know?
Link shortener with advanced, detailed statistics:

http://tyny.us/

#3 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 06 August 2006 - 04:17 AM

Well, I'm still learning PHP.

So I doubt I could do it =\

Depends on what I have to do.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 06:31 AM

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:
<?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>

Have fun.

Ken

#5 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 06 August 2006 - 02:56 PM

Thanks.

But you didn't have to spend an hour doing it =\


#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 06 August 2006 - 03:50 PM

Ken's code works great, the only problem is that you have unlimited guesses.

Orio.
Think you're smarty?

(Gone until 20 to November)

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 03:57 PM

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

#8 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 06 August 2006 - 04:50 PM

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.
Think you're smarty?

(Gone until 20 to November)

#9 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 07 August 2006 - 08:59 AM

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

<?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>


#10 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 07 August 2006 - 09:06 AM

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.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users