Manwar Posted August 23, 2012 Share Posted August 23, 2012 Hello everyone, I am learning PHP in college and have a project at hand. Basically, here's what the description for the project is. === A user basically guesses a number between 1 to 99. This number is generated only once at the start of the game and stays in the Cookie for three tries. If in those three tries, the number is guessed correctly, points are awarded and a new number is generated. If the user guesses it correctly in the first try, he gets 10 points. Second Try, he gets 5 points and third try he gets 2 points. All in all he has three tries every session along with a a "Try Again" button which destroys his earlier session and a new game begins. === Now, this code has turned into a disaster for me. And I really need help here. Please find the code below - <html> <head> <title> PHP Guessing Game </title> </head> <body> <?php $n1=10; //$n2=11; //$attempts=1; //$score=0; $guess=$_POST['guess']; $submit=$_POST['submit']; //$value=isset($_COOKIE["user1"]); $value=0; if(isset($submit)) { if(isset($_COOKIE["user1"]) && isset($_COOKIE["user2"])) { $score=$_COOKIE['user1']; $attempts=$_COOKIE['user2']; //$number=$_COOKIE['user3']; $value=1; } if ($attempts<= 3) { if($guess<1 ||$guess>100) { echo "your guess must be a number between 1 and 100"; } else if($guess!=n1 && $guess!=$number) { if($value!=1) { $score=0; setcookie("user1", $score); $attempts=2; setcookie("user2", $attempts); echo "Incorrect"; echo '<a href="game.php">Want to Try Again?</a>'; } else { $score=$_COOKIE['user1']; setcookie("user1", $score); $attempts=$_COOKIE['user2'] + 1; setcookie("user2", $attempts); echo "Incorrect"; echo '<a href="game.php">Want to Try Again?</a>'; } } //else if($guess==$n1 || $guess==$number) { if($value!=1) { $score=0; setcookie("user1", $score); $attempts = 1; setcookie("user2", $attempts); } if($attempts==1) { //$rand=rand(1,100); //setcookie("user3",$rand); //$number=$_COOKIE['user3']; $score=$_COOKIE['user1']+5; echo "That is correct!"; echo "Your score is ".$score; setcookie("user1", $score); $attempts = 1; echo '<a href="game.php">Want to Try Again?</a>'; } if($attempts==2) { //$rand=rand(1,100); //setcookie("user3",$rand); //$number=$_COOKIE['user3']; $score=$_COOKIE['user1']+3; setcookie("user1", $score); $attempts = 1; setcookie("user2", $attempts); echo "That is correct!"; echo "Your score is ".$score; echo '<a href="game.php">Want to Try Again?</a>'; } if($attempts==3) { //$rand=rand(1,100); //setcookie("user3",$rand); //$number=$_COOKIE['user3']; $score=$_COOKIE['user1']+2; setcookie("user1", $score); $attempts = 1; setcookie("user2", $attempts); echo "That is correct!"; echo "Your score is ".$score; echo '<a href="game.php">Want to Try Again?</a>'; } } } else { setcookie("user1", $score, time()-3600); setcookie("user2", $attempts, time()-3600); //setcookie("user3",$number,time()-3600); echo 'Sorry, your 3 tries are up. <a href="game.php">Want to Play Again?</a>.'; } } ?> <form action="trial.php" method="POST"> Guess:<input type="number" name="guess"><br/> <input type="submit" name="submit" value="Guess"> </form> </body> </html> Any help would be greatly appreciated. Thanks in advance. Manwar Quote Link to comment Share on other sites More sharing options...
Monkuar Posted August 23, 2012 Share Posted August 23, 2012 Not to be rude, but I don't really know what you want us to do? I also recommend storing those cookie data's in a db? Please explain a little bit more and I can try to help Quote Link to comment Share on other sites More sharing options...
Manwar Posted August 23, 2012 Author Share Posted August 23, 2012 Hey, Thanks for the reply. As I have shown above, This is what I am trying to achieve. A user basically guesses a number between 1 to 99. This number is generated only once at the start of the game and stays in the Cookie for three tries. If in those three tries, the number is guessed correctly, points are awarded and a new number is generated. If the user guesses it correctly in the first try, he gets 10 points. Second Try, he gets 5 points and third try he gets 2 points. All in all he has three tries every session along with a a "Try Again" button which destroys his earlier session and a new game begins. Now as you can see from the above code, the entire thing has turned into a mess. No matter, what answer a user provides - the points automatically keep on increasing. And message like this appears everytime - IncorrectWant to Try Again? That is correct!Your score is xx Want to Try Again? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 "the entire thing has turned into a mess." Then let's not even look at that code ok? First of all, don't use cookies for this, users can edit cookies. If you need it to persist between multiple browser sessions, use a DB. otherwise just use Sessions. This is not at all tested, and you'll have to add all your HTML formatting, but try this and see if it does what you are trying to do. <?php session_start(); $points = array(10,5,2); if(!isset($_SESSION['number'])){ $_SESSION['number'] = rand(1,99); $_SESSION['tries'] = 0; } if(isset($_POST['guess'])){ if($_SESSION['tries'] <= 3){ if($_POST['guess'] == $_SESSION['number']){ echo 'You win! '.$points[$_SESSION['tries']].' Points!'; }else{ echo 'You guessed wrong'; $_SESSION['tries']++; } }else{ echo 'Out of guesses'; } } if(isset($_POST['start_over'])){ unset($_SESSION['number']); unset($_SESSION['tries']); } echo 'You have '.(3-$_SESSION['tries']).' chances left!'; ?> <form method="post"> <input type="text" name="guess" /> <input type="submit" name="submit" value="Submit Guess" /> </form> <form method="post"> <input type="submit" name="start_over" value="Start Over" /> </form> Quote Link to comment 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.