helpmehelpmehelpme Posted December 8, 2011 Share Posted December 8, 2011 I am supposed to create a guessing game in PHP that uses sessions to store a random number between 0 and 100, along with the number of guesses the user has attempted. Each time the user guesses wrong, display the number of times the user has guessed. And a give up link that displays the generated number for the current game. I have been working on this for a week and I can't figure it out. Any help would be appreciated. Here's my code thus far: game.php <?php session_start(); if (!isset($_SESSION["randNum"])) { $_SESSION["randNum"]=rand(1,100); } $randNum=intval($_SESSION["randNum"]); ?> <html> <body> <center><form action="game_check.php" method="POST"> Guess a number 1-100:<input type="text" name="userGuess"/> <input type="submit" value="Guess"/> </form></center> <center><form action="game.php"> <input type="submit" value="Restart"/> </form></center> </body> </html> game_check.php <?php session_start(); $_SESSION["randNum"] = $_POST["randNum"]; $userGuess=$_POST["userGuess"]; if (isset($randNum)) { if ($userGuess<$randNum) { echo "<center>You guessed too low!</center>"; } if ($userGuess>$randNum) { echo "<center>You guessed too high!</center>"; } if ($userGuess==$randNum) { echo "<center>Congratulations You're right!!!</center>"; unset($_SESSION["randNum"]); } } else { echo "Uh oh"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252785-guessing-game-php-with-sessions/ Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 Try this: game.php <?php session_start(); $_SESSION['randNum'] = isset($_SESSION['randNum']) ? $_SESSION['randNum'] : rand(1, 100); $_SESSION['guesses'] = isset($_SESSION['guesses']) ? $_SESSION['guesses'] : 0; ?> <html> <body> <center><form action="game_check.php" method="POST"> Guess a number 1-100:<input type="text" name="userGuess"/> <input type="submit" value="Guess"/> </form></center> <center><form action="game.php"> <input type="submit" value="Restart"/> </form></center> </body> </html> game_check.php <?php session_start(); $randNum = $_SESSION['randNum']; $userGuess=$_POST["userGuess"]; if (isset($randNum)) { if ($userGuess<$randNum) { echo "<center>You guessed too low!</center>"; $_SESSION['randNum']++; } if ($userGuess>$randNum) { echo "<center>You guessed too high!</center>"; $_SESSION['randNum']++; } if ($userGuess==$randNum) { echo "<center>Congratulations You're right!!!</center>"; unset($_SESSION["randNum"], $_SESSION['guesses']); } } else { echo "Uh oh"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252785-guessing-game-php-with-sessions/#findComment-1295988 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 8, 2011 Author Share Posted December 8, 2011 awesome...thank you...it works, i see what I did wrong now.. any ideas about the counter that keeps track of the number of guesses. Im still stumped there too. thanks Quote Link to comment https://forums.phpfreaks.com/topic/252785-guessing-game-php-with-sessions/#findComment-1295989 Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 I added it, but I made a typo - so here it is: if ($userGuess<$randNum) { echo "<center>You guessed too low!</center>"; $_SESSION['guesses']++; } if ($userGuess>$randNum) { echo "<center>You guessed too high!</center>"; $_SESSION['guesses']++; } if ($userGuess==$randNum) { echo "<center>Congratulations You're right!!!</center>"; unset($_SESSION["randNum"], $_SESSION['guesses']); } Note the "$_SESSION['guesses']++" lines. Every time they make a wrong guess, the guesses is increment. Quote Link to comment https://forums.phpfreaks.com/topic/252785-guessing-game-php-with-sessions/#findComment-1295990 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 8, 2011 Author Share Posted December 8, 2011 okay, so to print out the counter guesses would you do echo "$_SESSION['guesses'];" Quote Link to comment https://forums.phpfreaks.com/topic/252785-guessing-game-php-with-sessions/#findComment-1295993 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 8, 2011 Author Share Posted December 8, 2011 nevermind, I got it ....thank you for your help i thought i would never get that done Quote Link to comment https://forums.phpfreaks.com/topic/252785-guessing-game-php-with-sessions/#findComment-1295997 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.