ivanmcruz32 Posted November 5, 2009 Share Posted November 5, 2009 Hi Everyone! I'm trying to write a game for my PHP class (I can't reach my teacher and nobody in the class answers). It is "I'm thinking of a number in reverse". Essentially, you think of a number, the computer has to guess the number. the person tells whether it's too high, low, or correct. I can't figure out where I'm going wrong....I'm kinda lost <html> <head> <title>Number Guessing v2.1.1.90</title> <style type = "text/css"> body { background: green; color: white; } </style> </head> <body> <h1>Number Guessing</h1><br><br> <h2>Please think of a number between 1 and 100 and I'll try to guess it!</h2><br><br> <?php if(isset($myGuess)){ if($_POST['correct']){ print "<br>I got it! <br>"; }else if($_POST['high']){ $highest=$myGuess; $myGuess=rand($lowest,$highest); }else if($_POST['low']){ $lowest=$myGuess; $myGuess=rand($lowest,$highest); } print "My guess is:"; print $_POST['$myGuess']; }else{ $myGuess=rand(1,100); $lowest=0; $highest=100; } ?> <p>Attempts: <?php print isset($_POST['attempts']) ? $_POST['attempts'] : 0; ?></p> <form method="post" action="" name="myGuess"> <input type="hidden" name="attempts" value="<?php print $_POST['attempts']; ?>" /> <input type="hidden" name="myGuess" value="<?php print is_numeric($_POST['myGuess']) ? $_POST['myGuess'] : rand(1,100); ?>" /> <br> <input type="radio" name="correct" value="correct" checked><b>Correct!</b><br> <input type="radio" name="high" value="high"><b>Too High</b><br> <input type="radio" name="low" value="low"><b>Too Low</b><br> <input type="Submit" value="Submit" align="MIDDLE"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/180366-im-thinking-of-a-number-game-in-reverse/ Share on other sites More sharing options...
seanlim Posted November 5, 2009 Share Posted November 5, 2009 I guess that you are just using this as a practice exercise and it is not essential to actually get this working? Either way, its probably better if you understood what went wrong: 1. You radio buttons should have the same "name" attribute. Take a look at your form, you will notice that when you click on either of the two bottom radio buttons, they don't toggle like they should! This is because only radio buttons with the same name toggle on/off when another is selected. 2. isset($myGuess) always returns false, because there is no variable $myGuess declared earlier on in the script. You have to use $_POST['myGuess'] in all instances, unless you use $myGuess = $_POST['myGuess']; It is good practice to do this if you intend to overwrite the contents of this variable somewhere in the script. 3. Users will not be able to see the script's guess, because it is in a hidden field! What you probably want is a input text field, with a read-only attribute. However, you can do away with the field totally, and print out the script's guess as text: <p>Guess: <?php print $myGuess;?> You should probably try to fix those major errors first, before posting back here if you need further help. Link to comment https://forums.phpfreaks.com/topic/180366-im-thinking-of-a-number-game-in-reverse/#findComment-951649 Share on other sites More sharing options...
ivanmcruz32 Posted November 6, 2009 Author Share Posted November 6, 2009 Thank you! Okay, i think i've fixed those issues, but now i have no idea what i'm doing. <html> <head> <title>Number Guessing v2.1.1.90</title> <style type = "text/css"> body { background: green; color: white; } </style> </head> <body> <h1>Number Guessing</h1><br><br> <h2>Please think of a number between 1 and 100 and I'll try to guess it!</h2><br><br> <?php $myGuess = $_POST['myGuess']; if(isset($_POST['myGuess'])){ if($_POST['correct']){ print "<br>I got it! <br>"; }else if($_POST['high']){ $highest=$myGuess; $myGuess=rand($lowest,$highest); }else if($_POST['low']){ $lowest=$myGuess; $myGuess=rand($lowest,$highest); } print "My guess is:"; print "$_ POST['$myGuess']"; }else{ $myGuess=rand(1,100); $lowest=0; $highest=100; } ?> <p>Attempts: <?php print isset($_POST['attempts']) ? $_POST['attempts'] : 0; ?></p> <p>Guess: <?php print $myGuess;?> <form method="post" action="" name="myGuess"> <input type="hidden" name="attempts" value="<?php print $_POST['attempts']; ?>" /><br> <input type="radio" name="choice" value="correct" checked><b>Correct!</b><br> <input type="radio" name="choice" value="high"><b>Too High</b><br> <input type="radio" name="choice" value="low"><b>Too Low</b><br> <input type="Submit" value="Submit" align="MIDDLE"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/180366-im-thinking-of-a-number-game-in-reverse/#findComment-952335 Share on other sites More sharing options...
seanlim Posted November 6, 2009 Share Posted November 6, 2009 Ok, that looks better. The problems now are: 1. Variables $highest and $lowest aren't being passed from page to page. In line with what you are trying to achieve (passing values through forms), you will have to pass both these variables through forms too. This is the time to use hidden input fields. $highest = isset($_POST['highest']) ? $_POST['highest'] : 100; $lowest = isset($_POST['lowest']) ? $_POST['lowest'] : 0; ... <input type="hidden" name="highest" value="<? print $highest;?>"> <input type="hidden" name="lowest" value="<? print $lowest;?>"> 2. The elements $_POST['high'] and $_POST['low'] does not exist. To get the value of the radio buttons the user has selected, you have to use if($_POST['choice']=="high") 3. Less vital, but nonetheless important: <?php print $_POST['attempts']; ?> is wrong. You should test whether such an element exists before using it, like how you have printed out the number of attempts. Once again, fix those mistakes and if you need further help, post again. Link to comment https://forums.phpfreaks.com/topic/180366-im-thinking-of-a-number-game-in-reverse/#findComment-952394 Share on other sites More sharing options...
seanlim Posted November 6, 2009 Share Posted November 6, 2009 Ok, here's the working code if you need it... <html> <head> <title>Number Guessing v2.1.1.90</title> <style type = "text/css"> body { background: green; color: white; } </style> </head> <body> <h1>Number Guessing</h1><br><br> <h2>Please think of a number between 1 and 100 and I'll try to guess it!</h2><br><br> <?php $highest = isset($_POST['highest']) ? $_POST['highest'] : 100; $lowest = isset($_POST['lowest']) ? $_POST['lowest'] : 0; $myGuess = isset($_POST['myGuess']) ? $_POST['myGuess'] : 0; if(isset($_POST['choice'])){ if($_POST['choice']=="correct") print "<br>I got it! <br>"; else if($_POST['choice']=="high") $highest=$myGuess-1; else if($_POST['choice']=="low") $lowest=$myGuess+1; } $myGuess=rand($lowest,$highest); ?> <p>Attempts: <?php print isset($_POST['attempts']) ? $_POST['attempts'] : 0; ?></p> <p>Guess: <?php print $myGuess;?> <form method="post" action="" name="myGuess"> <input type="hidden" name="attempts" value="<?php print $_POST['attempts']; ?>" /><br> <input type="text" name="highest" value="<? print $highest;?>"> <input type="text" name="myGuess" value="<? print $myGuess;?>"> <input type="text" name="lowest" value="<? print $lowest;?>"> <input type="radio" name="choice" value="correct" checked><b>Correct!</b><br> <input type="radio" name="choice" value="high"><b>Too High</b><br> <input type="radio" name="choice" value="low"><b>Too Low</b><br> <input type="Submit" value="Submit" align="MIDDLE"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/180366-im-thinking-of-a-number-game-in-reverse/#findComment-952396 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.