illadelphiakid Posted October 13, 2011 Share Posted October 13, 2011 Can someone help me create this?? Upon outside entry, a random number is chosen between 1 and 10. Use this function to generate the guess number $guessnum = rand(1,10); // you may change the variable name The user tries to guess this number by choosing from a selection list of possibilities. The user "loses" if there are three failed attempts. For reentrant activations the selection list must be sticky and the same guess number must be maintained. The guess number and the attempt number must both be propagated through hidden inputs. Here is a screen shot: Upon outside entry, or using the "Start Over" hyperlink (with the list activated): Let's suppose the number to be guessed is 6. I select 4 and guess it. Response should say "Try #1" I select 9 and guess it. Response should say "Try #2" I select 3 and guess it. Response should say "You Lose" At this point assume that we must "Start Over". This time, suppose the generated number is 8. I select 8 and guess it. Response should say "You Win" Again, assume we must "Start Over". Quote Link to comment https://forums.phpfreaks.com/topic/249007-random-number-guess-game/ Share on other sites More sharing options...
MasterACE14 Posted October 13, 2011 Share Posted October 13, 2011 I won't do your homework for you, but I can point you to the relevant information. - You will obviously need to know how to create a HTML form which has a 'dropdown' box, 'hidden' inputs and a 'submit' button. - You will also need to know how to use Sessions in PHP to store guesses and attempts. - Here's the Rand() function if you're curious. - You may want to use a GET variable for your 'start over' link. - You will also need to know how either If/Else conditional statements work or Switch statements to check for correct/incorrect guesses. - Lastly, probably could use a While loop, to loop through the guesses. Good Luck! Quote Link to comment https://forums.phpfreaks.com/topic/249007-random-number-guess-game/#findComment-1278820 Share on other sites More sharing options...
codefossa Posted October 13, 2011 Share Posted October 13, 2011 One of many ways to do it .. <?php $session_id = session_id(); if (empty($session_id)) session_start(); if (isset($_POST['guess'])) { if ($_POST['guess'] == $_SESSION['number']) { echo "You Win! "; unset($_SESSION['number']); } else { $_SESSION['tries']++; if ($_SESSION['tries'] < 3) { echo "Try #{$_SESSION['tries']}<br /><br />"; } else { echo "Try #3 : You Lose "; unset($_SESSION['number']); } } } if (empty($_SESSION['number'])) { $_SESSION['tries'] = 0; $_SESSION['number'] = rand(1, 10); } ?> <html> <head> <title>Guessing Game</title> </head> <body> <form method="post" action=""> <label for="guess">Guess</label> <select name="guess" id="guess"> <?php for ($i = 1; $i < 11; $i++) { echo "<option value='{$i}'>{$i}</option>"; } ?> </select> <input type="submit" value="Guess" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/249007-random-number-guess-game/#findComment-1278868 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.