Search the Community
Showing results for tags 'guessing game'.
-
I am supposed to build a game so that the human thinks of a number and the computer guesses it, now each time the human hits to low or to high, the computer will guess lower than the "to high" number or higher than the "to low" number" it is also supposed to count how many guesses it took computer to guess the answer, I have this but its not all working correct, when you run the program it automatically guessed a number without being asked or anything and the count is forever long.. Please and Thank you <?php session_start() ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> <head> <title>COmputer can you guess my number</title> <style type = "text/css"> body { background: pink; color: green; } </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($_SESSION["counter"])){ $counter = $_SESSION["counter"]; } else { $counter = 0; } // end if $counter++; //store new data in counter $_SESSION["counter"] = $counter; print <<< HERE <form action = "" method = "post"> HERE; $highest = isset($_POST['highest']) ? $_POST['highest'] : 100; $lowest = isset($_POST['lowest']) ? $_POST['lowest'] : 1; $myGuess = isset($_POST['myGuess']) ? $_POST['myGuess'] : 0; if(isset($_POST['choice'])){ if($_POST['choice']=="correct") print "<br>I got it! it took me $counter tries. <br>"; else if($_POST['choice']=="high") $highest=$myGuess-1; else if($_POST['choice']=="low") $lowest=$myGuess+1; } $myGuess=rand($lowest,$highest); $show_submit = true; if(!isset($_SESSION['number_to_guess'])){ $num = rand(1, 100); $_SESSION['number_to_guess'] = $num; } else{ $num = $_SESSION['number_to_guess']; } if(!isset($_POST['btn_submit'])){ $num_guessed = ""; $num_guesses = 0; $status = "Please enter your guess in the field below."; $_SESSION['number_guess'] = $num_guesses; } else{ $num_guessed = $_POST['txt_guess']; $_SESSION['number_guess']++; SWITCH ($num){ CASE $num_guessed == $num: $status = "Spot on, the number is $num"; $show_submit = false; break; CASE $num_guessed < $num: $status = "Your guess is too low"; break; CASE $num_guessed > $num: $status = "Your guess is too high"; break; DEFAULT: $status = "Please select number"; } } if(isset($_POST['btn_new'])){ session_destroy(); header("location: index.php"); } if(isset($_POST['btn_new'])){ session_unset($_SESSION['number_guess']); header("location: index.php"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Guess the number</title> </head> <body> Your guesses = <?php echo $_SESSION['number_guess'] ?><br /> <input type="radio" name="choice" value="correct"><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"> <p>Guess: <?php print $myGuess;?> <form method="post" action="" name="myGuess"> <br /> </form> </body> </html>
- 32 replies