Jump to content

Random Number Guess Game


illadelphiakid

Recommended Posts

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

numguess-1.gif

 

 

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".

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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>

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.