thekauz Posted December 14, 2010 Share Posted December 14, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/ Share on other sites More sharing options...
OOP Posted December 14, 2010 Share Posted December 14, 2010 Hi there, You are showing the correct number if someone's guess was wrong so you need to change the number in each request anyway Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147307 Share on other sites More sharing options...
thekauz Posted December 14, 2010 Author Share Posted December 14, 2010 well I have it display the actual random number so that I can make sure that it does not change. Any advice? Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147309 Share on other sites More sharing options...
ignace Posted December 14, 2010 Share Posted December 14, 2010 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147311 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147312 Share on other sites More sharing options...
OOP Posted December 14, 2010 Share Posted December 14, 2010 You can use session to store your random number till someone is able to guess it right, then you generate a new one. You need also to store the user ip as well. Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147313 Share on other sites More sharing options...
Anti-Moronic Posted December 14, 2010 Share Posted December 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147315 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 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. never said it is a very good way... Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147318 Share on other sites More sharing options...
Anti-Moronic Posted December 14, 2010 Share Posted December 14, 2010 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. never said it is a very good way... Not commenting on what anyone said just clarifying for OP. Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147319 Share on other sites More sharing options...
OOP Posted December 14, 2010 Share Posted December 14, 2010 Thanks Anti-Moronic for the clarification ...I was wrong in this. Quote Link to comment https://forums.phpfreaks.com/topic/221661-random-number-game/#findComment-1147320 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.