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 -
[php]
<?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']; aa//$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 'Want to Try Again?'; } else { $score=$_COOKIE['user1']; setcookie("user1", $score); $attempts=$_COOKIE['user2'] + 1; setcookie("user2", $attempts); echo "Incorrect"; echo 'Want to Try Again?'; } } aa//else if($guess==$n1 || $guess==$number) { if($value!=1) { $score=0; setcookie("user1", $score); $attempts = 1; setcookie("user2", $attempts); } if($attempts==1) { aa//$rand=rand(1,100); aa//setcookie("user3",$rand); aa//$number=$_COOKIE['user3']; $score=$_COOKIE['user1']+5; echo "That is correct!"; echo "Your score is ".$score; setcookie("user1", $score); $attempts = 1; echo 'Want to Try Again?'; } if($attempts==2) { aa//$rand=rand(1,100); aa//setcookie("user3",$rand); aa//$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 'Want to Try Again?'; } if($attempts==3) { aa//$rand=rand(1,100); aa//setcookie("user3",$rand); aa//$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 'Want to Try Again?'; } } } else { setcookie("user1", $score, time()-3600); setcookie("user2", $attempts, time()-3600); aa//setcookie("user3",$number,time()-3600); echo 'Sorry, your 3 tries are up. Want to Play Again?.'; } } ?> Guess:[/php]