ItsWesYo Posted August 6, 2006 Share Posted August 6, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/ Share on other sites More sharing options...
techiefreak05 Posted August 6, 2006 Share Posted August 6, 2006 That would probably be more than one script.. and complicated, how much PHP do you know? Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70034 Share on other sites More sharing options...
ItsWesYo Posted August 6, 2006 Author Share Posted August 6, 2006 Well, I'm still learning PHP.So I doubt I could do it =\Depends on what I have to do. Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70035 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2006 Share Posted August 6, 2006 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]<?phpsession_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 Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70067 Share on other sites More sharing options...
ItsWesYo Posted August 6, 2006 Author Share Posted August 6, 2006 Thanks. But you didn't have to spend an hour doing it =\ Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70229 Share on other sites More sharing options...
Orio Posted August 6, 2006 Share Posted August 6, 2006 Ken's code works great, the only problem is that you have unlimited guesses.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70256 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2006 Share Posted August 6, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70260 Share on other sites More sharing options...
Orio Posted August 6, 2006 Share Posted August 6, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70281 Share on other sites More sharing options...
extrovertive Posted August 7, 2006 Share Posted August 7, 2006 You probably have a script working already, but I made this just for fun. :D[code=php:0]<?php/*numbers: 1-30gueses: 5higher 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] Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70536 Share on other sites More sharing options...
Orio Posted August 7, 2006 Share Posted August 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16681-guess-the-number/#findComment-70542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.