Jump to content

Random Number Game


thekauz

Recommended Posts

Hello,

I am trying to create a simple game random number guessing game in PHP.

Currently, I have an HTML page with a form on it where the user enters a number.

Then when the user clicks the submit button on the form, their number is passed onto the second page.

On the second page, I have some PHP code that generates a random number between 1 and 20, then it checks to see if the two numbers are the same.

If they are the same, it says correct.

If the guess is higher, it says so.

If the guess is lower, it says so.

I also made it display a form to re-guess the number (if the initial guess was incorrect).

 

The problem I have is when the user attempts to re-guess the number. Whenever the user attempts to enter a new guess, the random number changes.

I want to make it so that the random number stays the same as it is.

Any help?

 

Here is the Code for the first HTML page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>phpNumberGame</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="main"></div>

<div id="enterNumber">
	<form action="result.php" method="post">
	  Enter a number: <input type="text" name="inputnumber" /><br />
	  <input type="submit" value="Guess" />
	</form>
</div>

</body>
</html>

 

Here is the code for the second page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>result</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="main"></div>

    <div id="enterNumber">

	<?php
	$number_actual = rand (1,20);
	$number_guess = $_REQUEST["inputnumber"];
        	echo "You chose: " . $number_guess . "<br />";
		echo "The random number is: " . $number_actual . "<br />";
			if ($number_guess==$number_actual)
				echo "You guessed <b>Correctly!</b>";
			elseif ($number_guess<$number_actual)
					{
					echo "You are too <b>Low</b>!";
					echo "<br />";
					echo "<br />";
					echo "Try guessing again...";
					echo "<form>
							<input type='text' name='inputnumber' />
							<input type='submit' value='Guess' />
						  </form>";
					}
			elseif ($number_guess>$number_actual)
					{
					echo "You are too <b>High</b>!";
					echo "<br />";
					echo "<br />";
					echo "Try guessing again...";
					echo "<form>
							<input type='text' name='inputnumber' />
							<input type='submit' value='Guess' />
						  </form>";
					}
	?>
    </div>

</body>
</html>

 

Here is what I have so far on my website, you can see it here:

http://joshkautz.com/phpNumberGame/phpNumberGame.html

 

I think I included everything necessary for people to give input, if not please let me know and I will give more information!

Do I need to redo things completely?

Any advice would be greatly appreciated.

Thanks a ton!

Link to comment
Share on other sites

define('RAND_MIN', 0);
define('RAND_MAX', 20);

session_start();
if(!isset($_SESSION['random_number'])) {
    $_SESSION['random_number'] = rand(RAND_MIN, RAND_MAX);
}
if(isset($_POST['guess']))
{
    switch(true) {
        case $_POST['guess'] < $_SESSION['random_number']:
            echo 'higher'; break;

        case $_POST['guess'] > $_SESSION['random_number']:
            echo 'lower'; break;

        case $_POST['guess'] == $_SESSION['random_number']:
            echo 'you won!';
            $_SESSION['random_number'] = rand(RAND_MIN, RAND_MAX); break;
        
        default: echo 'ola ese! big cojones muchacho'; break;
    }
}

Link to comment
Share on other sites

You can either store it in a database or in a textfile, and recognize the user by either a cookie or ip address.

 

It is also possible to remember the number in the cookie. Of course, possible to cheat then, but you could make it so they can't tell what number it is, but your script know.

 

session/cookie

Link to comment
Share on other sites

You don't need the user ip, sessions cannot be shared between multiple users. Session id is stored in a cookie in the users browser. Only way to conflict these sessions is if somebody steals the user's cookie.

if you comment what I said, then I just said that is one of the ways of recognizing who the user is. :P never said it is a very good way...

Link to comment
Share on other sites

You don't need the user ip, sessions cannot be shared between multiple users. Session id is stored in a cookie in the users browser. Only way to conflict these sessions is if somebody steals the user's cookie.

if you comment what I said, then I just said that is one of the ways of recognizing who the user is. :P never said it is a very good way...

 

Not commenting on what anyone said :P just clarifying for OP.

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.