Jump to content

Guessing game PHP with sessions


helpmehelpmehelpme

Recommended Posts

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";
}


?>

Link to comment
Share on other sites

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";
}


?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.