benjahnee Posted March 24, 2013 Share Posted March 24, 2013 hello guys i have some guessing game code that works fine i just need a few things adding to it that i dont know how to do if some body could please show me how. its just a simple number game where the user has to keep guessing numbers between 1 and 10 till they get it right. could somebody please how me how to add to the code so that. - there is a counter that tells the user how many guesses they have had. - the program stops at 7 guesses. thanks guys my code is if(isset($_POST['guess'])) { $guess = intval($_POST['guess']); $secret = intval($_POST['secret']); $output = ""; if($guess == $secret) { $output .= "<h1>Correct!</h1>"; $output .= "<a href='guessinggame.php'>Play again</a>"; } elseif($guess < $secret) { $output .= "<h1>Your answer was too low. Please try again!"; } else { $output .= "<h2>Your answer was too high. Please try again!</h2>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/ Share on other sites More sharing options...
DaveyK Posted March 24, 2013 Share Posted March 24, 2013 Please put your code in BB code blocks. What have you tried so far to achieve what you describe? Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420724 Share on other sites More sharing options...
benjahnee Posted March 24, 2013 Author Share Posted March 24, 2013 ok i have tried some code but i have an error on the first line. could someone please tell me how to fix it. the error is Parse error: syntax error, unexpected '=', expecting ',' or ')' in F:\EasyPHP-12.1\www\scripting\guessinggame.php on line 1 and my code is <?phpif(isset($_POST['guess'])){if(!isset($_SESSION['life']=1);else if($_SESSION['life']==7){echo "Your Life end<br>Thanks for playing<br>";unset($_SESSION['life']);}else$_SESSION['life']++;$guess=intval($_POST['guess']);$secret=rand(1,10);if($guess==$secret){$output="<h1>Correct!</h1>";$output .="<a href='guessinggame.php'>Play again</a>";unset($_SESSION['life']);}elseif($guess<$secret){$output.="<h1>Your answer was too low. Please try again!";}else{$output.="<h2>Your answer was too high. Please try again!</h2>";}}?> [code/] any ideas??? Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420730 Share on other sites More sharing options...
PaulRyan Posted March 24, 2013 Share Posted March 24, 2013 Was bored, so I gave this a little go. Try this: <?PHP session_start(); #session_destroy(); //### Current file name $filename = basename($_SERVER['PHP_SELF']); //### If play is set, reset all values if(isset($_GET['play'])) { unset($_SESSION['number']); unset($_SESSION['guesses']); unset($_SESSION['guessed']); } //### Start the output $output = ''; //### Check if the form is posted if($_SERVER['REQUEST_METHOD'] == 'POST') { //### Set the number that was guessed $numberGuessed = isset($_POST['numberGuessed']) ? (int)$_POST['numberGuessed'] : FALSE ; //### If no number is guessed, display error if(empty($numberGuessed)) { $output .= 'Please enter a number to guess. <br>'; //### Validation passed } else { //### Increment guesses $_SESSION['guesses']++; //### Check to make sure they have guesses available if($_SESSION['guesses'] <= 7) { //### check if they guessed the secret number if($numberGuessed == $_SESSION['number']) { $_SESSION['guessed'] = TRUE; //### check if the number is lower than the secret number } else if($_SESSION['number'] < $numberGuessed) { $output .= 'Bad luck! The number is lower. <br>'; //### Else the number is higher } else { $output .= 'Bad luck! The number is higher. <br>'; } } } } //### Set the secret number and amount of guesses $_SESSION['number'] = isset($_SESSION['number']) ? $_SESSION['number'] : mt_rand(1,10) ; $_SESSION['guesses'] = isset($_SESSION['guesses']) ? min($_SESSION['guesses'], 7) : 0 ; //### Debug purposes echo '<pre>'; print_r($_SESSION); echo '</pre>'; //### Check if the secret number has been guessed if(isset($_SESSION['guessed'])) { $output .= 'Well done! You guessed the number. <br>'; $output .= '<a href="'.$filename.'?play=true">Play again?</a>'; //### Check if they have used all of their guesses } else if($_SESSION['guesses'] >= 7) { $output .= 'You used up all your guesses. <br>'; $output .= 'The secret number was: '. $_SESSION['number'] .'. <br>'; $output .= '<a href="'.$filename.'?play=true">Play again?</a>'; //### If both checks passed, show form } else { $output .= '<form method="POST" action="'. $filename .'"> Your Guess: <input type="text" name="numberGuessed"> <input type="submit" value="Submit Guess"> </form>'; } echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420736 Share on other sites More sharing options...
benjahnee Posted March 24, 2013 Author Share Posted March 24, 2013 paul ryan u sir are my hero are u open to giving me ur email so i could discuss problems i may have with u in the future? i wont pester you all the time i promise Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420740 Share on other sites More sharing options...
benjahnee Posted March 24, 2013 Author Share Posted March 24, 2013 oh one more thing paul ryan could you show me what code to add so that it displays what numbers have been guessed please thanks a bunch Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420744 Share on other sites More sharing options...
PaulRyan Posted March 24, 2013 Share Posted March 24, 2013 Use the session variable 'guesses' to display how many guesses. I won't be able to give my e-mail address out, but you can always post here if you have problems, there's always someone around to help/ Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420745 Share on other sites More sharing options...
benjahnee Posted March 24, 2013 Author Share Posted March 24, 2013 thats fine about email sorry i wasnt very clear.. i need it to sumhow display what actual numbers have been guessed, as well as how many guesses have been made? Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420747 Share on other sites More sharing options...
PaulRyan Posted March 24, 2013 Share Posted March 24, 2013 Ohh right, well you need to create a new session variable something like "$_SESSION['numbers_guessed']" and then add each guessed number that is wrong to that variable. Look into array for that. Displaying amount of guesses I explained in my previous post, use the variable "$_SESSION['guesses']" I did the hard work, now have a little try to see what you can do, and pop back with any questions Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420752 Share on other sites More sharing options...
benjahnee Posted March 24, 2013 Author Share Posted March 24, 2013 thanks alot il give it a shot Quote Link to comment https://forums.phpfreaks.com/topic/276085-php-guessing-game-help/#findComment-1420753 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.