Jump to content

Php reverse guessing game


webdevdea

Recommended Posts

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>
Link to post
Share on other sites

I can see a few problems in your code.

 

You should only have one <!DOCTYPE .......> tag on a page.

 

You've got two if statements checking the same condition but doing different things, both of which has a header redirect. I don't think the second if statement will ever be reached if the condition is true, because the first one will have already initiated the redirect.

 

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");
}

I could be wrong on that, maybe the code will still execute, but even if it does, you should move the session_unset() to the first if, and just remove the second one, I don't see any point in having them separate.

 

You also need to put your input fields inside the <form> tags, it looks like you have them above with only a <br/> inside.

 

If you can fix all that up and come back, we'll see what's happening with it then.

 

Denno

Link to post
Share on other sites

unexpected else, line 55>>>?? help please.. 

<?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>


<?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'];
}






elseif{
    $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_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 />
<form>    
    <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">
</form>
<p>Guess: <?php print $myGuess;?>
<form method="post" action="" name="myGuess">
<br />


</form>
</body>
</html>
Link to post
Share on other sites

Where do you actually set 'highest' and 'lowest' in your form? It looks like that will always be resetting to 100 and 1 respectively. You should add two hidden input fields in your form which will store the previous highest and lowest values, that way this code

 

 

$highest = isset($_POST['highest']) ? $_POST['highest'] : 100;
$lowest = isset($_POST['lowest']) ? $_POST['lowest'] : 1;
Will actually be true on subsequent guesses.

 

Also, I'm not sure how this is working, because you don't have the input fields inside the form tags yet..

 

That won't fix your problem of it only increasing by 1 each time..

 

Could you put at the top of your code print_r($_POST); and make sure that it actually has something in there when you hit the submit button

Link to post
Share on other sites

now i have this and its kinda crazy when you play the game,

if you click to high it automatically goes to one and sometimes when you click to low it goes lower anyway ???  

<?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>

<?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>
Link to post
Share on other sites

Your input elements have to be within your <form> tag, not before it. Move the form tag up.

 

Also, you're trying to access a whole bunch of $_POST vars that do not exist at all in your form code. The only variable you have defined in your form is $_POST['choice']. As such, that is the only one you can use in your PHP code. If you need those other variables you need to generate inputs for them.

Link to post
Share on other sites
Check this out.. it still does not know higher from lower sometimes and always starts over with 

one what am I missing? 



<?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>

<?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['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 1;
$myGuess = isset($_POST['correct']) ? $_POST['correct'] : 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_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>
<form> 
    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">
</form>

<p>Guess: <?php print $myGuess;?>
<form method="post" action="" name="myGuess">
<br />

</form>
</body>
</html>
Link to post
Share on other sites

OK if someone could run this and see if you could help me figure out my little problems, Its almost there .. :-) 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>

<?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['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 1;
$myGuess = isset($_POST['correct']) ? $_POST['correct'] : 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_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>
	<form method="post" action="" name="choice">
			<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">
	</form>

<p>Guess: <?php print $myGuess;?>
<form method="post" action="" name="myGuess">
<br />

</form>
</body>
</html>
Link to post
Share on other sites

Where do you actually set 'highest' and 'lowest' in your form? It looks like that will always be resetting to 100 and 1 respectively. You should add two hidden input fields in your form which will store the previous highest and lowest values, that way this code

$highest = isset($_POST['highest']) ? $_POST['highest'] : 100;
$lowest = isset($_POST['lowest']) ? $_POST['lowest'] : 1;
Will actually be true on subsequent guesses.

 

Also, I'm not sure how this is working, because you don't have the input fields inside the form tags yet..

 

That won't fix your problem of it only increasing by 1 each time..

 

Could you put at the top of your code print_r($_POST); and make sure that it actually has something in there when you hit the submit button

It seems to be passing the variable I do believe, can you run this and see ? 
Please and thank you 



<!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>

<?php
print_r($_POST);


 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['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 1;
$myGuess = isset($_POST['correct']) ? $_POST['correct'] : 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_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>
	<form method="post" action="" name="choice">
			<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">
	</form>

<p>Guess: <?php print $myGuess;?>
<form method="post" action="" name="myGuess">
<br />

</form>
</body>
</html>
Link to post
Share on other sites

 

Check this out.. it still does not know higher from lower sometimes and always starts over with 

one what am I missing? 



<?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>

<?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['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 1;
$myGuess = isset($_POST['correct']) ? $_POST['correct'] : 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_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>
<form> 
    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">
</form>

<p>Guess: <?php print $myGuess;?>
<form method="post" action="" name="myGuess">
<br />

</form>
</body>
</html>

 

But remember each time you must save the values of low and high either as hidden in the form or as a session variable.

Then they must be retrieved back whenever needed (such as right before you reset low or high or get a random value

Link to post
Share on other sites

Add these after the inputs you already have (but still within the form tags:

<input type="hidden" id="highest" value="<?php echo $highest ?>"/>
<input type="hidden" id="lowest" value="<?php echo $lowest ?>"/>
Link to post
Share on other sites

For some reason when you hit to high it always goes back to one, is there any way to prevent this or should I just leave well enough alone? 

<?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>

<?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['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 1;
$myGuess = isset($_POST['correct']) ? $_POST['correct'] : 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_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>
	<form method="post" action="" name="choice">
			<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="hidden" id="highest" value="<?php echo $highest ?>"/>
			<input type="radio" name="choice" value="low"><b>Too Low</b><br>
			<input type="hidden" id="lowest" value="<?php echo $lowest ?>"/>
			<input type="Submit" value="Submit" align="MIDDLE">
	</form>

<p>Guess: <?php print $myGuess;?>
<form method="post" action="" name="myGuess">
<br />

</form>
</body>
</html>
Link to post
Share on other sites

put this at the top of your code

 

 

if(isset($_POST['choice'])){
var_dump($_POST);
die();

 

That will print out the contents of $_POST once you have made an incorrect guess. This way you can see what values are coming through.

Also, you may want to add name="highest/lowest" to each of the hidden input fields.. I can never remember which value is passed through the $_POST variable, so I usually put both, however I obviously forgot when I typed that example code before..

Link to post
Share on other sites

Here you go:

 

<?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>
 
<?php
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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);
  
//This is used for debugging - keeping track of what each value is when the submit button is pressed
  if(isset($_POST['choice'])){
print_r($_POST);
}  
  
//This following block of code doesn't do anything in the game, so not sure why it is here?
  $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_new'])){
    session_destroy();
session_unset($_SESSION['number_guess']);
    header("location: index.php");
}
 
?>
<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">
</form>
 
<p>Guess: <?php print $myGuess;?>
</body>
</html>

 

There are a few things I had to change:

 

 

//Before
$myGuess = isset($_POST['correct']) ? $_POST['correct'] : 0;
//After
$myGuess = isset($_POST['myGuess']) ? $_POST['myGuess'] : 0;

You had it right in your original post, but not sure why it was changed.

 

 

<input type="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>

These are all used to keep track of the previous guess, current highest and lowest numbers, and also how many attempts have been made.

 

They're the main things, and your guessing game will now work. Have a look at the code and make sure you understand it. Feel free to ask more questions if you're not sure about anything. Also, remove the debugging when you are finished, as that isn't needed for the final version, but it's handy for using while developing.

 

Denno

Link to post
Share on other sites

Here you go:

<?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>
 
<?php
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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);
  
//This is used for debugging - keeping track of what each value is when the submit button is pressed
  if(isset($_POST['choice'])){
print_r($_POST);
}  
  
//This following block of code doesn't do anything in the game, so not sure why it is here?
  $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_new'])){
    session_destroy();
session_unset($_SESSION['number_guess']);
    header("location: index.php");
}
 
?>
<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">
</form>
 
<p>Guess: <?php print $myGuess;?>
</body>
</html>

There are a few things I had to change:

//Before
$myGuess = isset($_POST['correct']) ? $_POST['correct'] : 0;
//After
$myGuess = isset($_POST['myGuess']) ? $_POST['myGuess'] : 0;

You had it right in your original post, but not sure why it was changed.

<input type="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>

These are all used to keep track of the previous guess, current highest and lowest numbers, and also how many attempts have been made.

 

They're the main things, and your guessing game will now work. Have a look at the code and make sure you understand it. Feel free to ask more questions if you're not sure about anything. Also, remove the debugging when you are finished, as that isn't needed for the final version, but it's handy for using while developing.

 

Denno

<?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>
 
<?php
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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);
  
//This is used for debugging - keeping track of what each value is when the submit button is pressed
  if(isset($_POST['choice'])){
print_r($_POST);
}  
  
?>
<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">
</form>
<p> You,The user, Think of a number, and I, the computer will try and guess it </p>
 
<p>Guess: <?php print $myGuess;?>
</body>
</html>

This is what I have now, the mystery code was something a friend had tried to add to make it work, obviously it did not work. 

I had added print_r($_POST); so that I could make sure the variables were getting passed correctly, I think it may be acting the right way now, I am going to take your advise and  go over the code so I can understand what I did not understand. Thank you so much. Did you try the game?

Link to post
Share on other sites

So I had this  mixed up, was that what was making it keep go back to one?

//Before
$myGuess = isset($_POST['correct']) ? $_POST['correct'] : 0;
//After
$myGuess = isset($_POST['myGuess']) ? $_POST['myGuess'] : 0;
Link to post
Share on other sites

How could i fix it so that you have to click the button before the computer starts picking a number? 

<?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>
 
<?php
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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);
  
//This is used for debugging - keeping track of what each value is when the submit button is pressed
  //if(isset($_POST['choice'])){
//print_r($_POST);
//}  
  
?>
<p> You,The user, Think of a number, and I, the computer will try and guess it </p>
<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">
<?php echo "<br> <a href='http://localhost/Assignments%20php%20class/Assignment6.php'>Reset Game</a>" ?>;
</form>

 
<p>Guess: <?php print $myGuess;?>
</body>
</html>
Link to post
Share on other sites

I did try the game, it works as expected.

The browser shouldn't matter with this, so not sure why it would get stuck on Safari.. Because this is all server-side, the browser merely shows what data it's given. You would usually only test browsers when you have CSS and javascript that needs checking. PHP will not care about browser.

 

I'm not exactly sure what was making it go back to 1, but it was probably something to do with the script not knowing what it had previous guessed, so setting high and low variables was kinda pointless.

 

For implementing a 'start' button, you will need a second form, with only a submit button in it. Then you need to use PHP to determine which form is going to show. So the if condition will need to check if the start button has been pressed or not.

 

Give that a crack and post back with some code that you come up with and I'll guide you from there.

Link to post
Share on other sites

Is this anything close? its not working right but maybe its close? 

<?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>
 
<?php
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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;
   }
   if (isset($_POST["start_game"])){
  $myGuess=rand($lowest,$highest);
  }
//This is used for debugging - keeping track of what each value is when the submit button is pressed
  //if(isset($_POST['choice'])){
//print_r($_POST);
//}  
  
?>
<p> You,The user, Think of a number, and I, the computer will try and guess it </p>
<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">
<?php echo "<br> <a href='Assignment6.php'>Reset Game</a>" ?>;

</form>

<form method = "post" action="" name= "start_game">
<input type ="Submit" name = "start_game" value = "Start Game" align ="MIDDLE">

<?php echo "<br> <a href='Assignment6.php'>Reset Game</a>" ?>;
</form>

 
<p>Guess: <?php print $myGuess;?>
</body>
</html>
Link to post
Share on other sites

Almost there, try this ..  

<?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>
 
<?php
   if (isset($_POST["start_game"])){
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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);
  }
//This is used for debugging - keeping track of what each value is when the submit button is pressed
  //if(isset($_POST['choice'])){
//print_r($_POST);
//}  
  
?>
<p> You,The user, Think of a number, and I, the computer will try and guess it </p>
<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">

<?php echo "<br> <a href='Assignment6.php'>Reset Game</a>" ?>;

</form>

<form method = "post" action="" name= "start_game">
<input type ="Submit" name = "start_game" value = "Start Game" align ="MIDDLE">

</form>

 
<p>Guess: <?php print $myGuess; ?>
</body>
</html>
Link to post
Share on other sites

Where's the if statement that will determine which part is showing? Right now everything is shown at once..

 

You want either the form with the submit button to show (which you want to show by default), or you want the form with the radio buttons and reset link showing (which will show only once the Start button has been pressed)

Link to post
Share on other sites

Ok I am stuck, 


 
<!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>
 
<?php
   if (isset($_POST["start_game"])){
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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);
  }
  else {
//This is used for debugging - keeping track of what each value is when the submit button is pressed

//if(isset($_POST['choice'])){
//print_r($_POST);
//}  
  
?>
<p> You,The user, Think of a number, and I, the computer will try and guess it </p>
<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">

<?php echo "<br> <a href='Assignment6.php'>Reset Game</a>" ?>;

</form>

<form method = "post" action="" name= "start_game">
<input type ="Submit" name = "start_game" value = "Start Game" align ="MIDDLE">

</form>

 
<p>Guess: <?php print $myGuess; ?>
}
</body>
</html>
Link to post
Share on other sites

Closer? 


 
<!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>
 
<?php
   if (isset($_POST["start_game"])){
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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);
  }
  else {
   echo "<br> <a href='http://localhost/Assignments php class/As6.php'>Reset Game</a>" ;
  }
//This is used for debugging - keeping track of what each value is when the submit button is pressed

//if(isset($_POST['choice'])){
//print_r($_POST);
//}  
  
?>
<p> You,The user, Think of a number, and I, the computer will try and guess it </p>
<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">

<?php echo "<br> <a href='http://localhost/Assignments php class/As6.php'>Reset Game</a>" ?>;

</form>

<form method = "post" action="" name= "start_game">
<input type ="Submit" name = "start_game" value = "Start Game" align ="MIDDLE">

</form>

 
<p>Guess: <?php print $myGuess; ?>

</body>
</html>
Link to post
Share on other sites

Ok Getting a Little closer Now when you start the game and hit submit, well it does not guess another number 


 
<!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>
 
<?php



   if (isset($_POST["start_game"])){
 
 
 if (isset($_POST["counter"])){
    $counter = $_POST["counter"];
  } else {
    $counter = 0;
  } // end if
  $counter++;
 
  //store new data in counter
  $_SESSION["counter"] = $counter;
 
$highest = isset($_POST['high']) ? $_POST['high'] : 100;
$lowest = isset($_POST['low']) ? $_POST['low'] : 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);
  }
  
//This is used for debugging - keeping track of what each value is when the submit button is pressed

//if(isset($_POST['choice'])){
//print_r($_POST);
//}  
  
?>

<form method="post" action="" name="choice">
<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="hidden" id="high" name="high" value="<?php echo $highest ?>"/>
<input type="hidden" id="low" name="low" value="<?php echo $lowest ?>"/>
<input type="hidden" id="myGuess" name="myGuess" value="<?php echo $myGuess ?>"/>
<input type="hidden" id="counter" name="counter" value="<?php echo $counter ?>"/>
 
<input type="Submit" value="Submit" align="MIDDLE">
<p>Guess: <?php print $myGuess; ?> </p>

<?php echo "<br> <a href='http://localhost/Assignments php class/As6.php'>Reset Game</a>" ?>;

</form>
<p> You,The user, Think of a number, and I, the computer will try and guess it </p>
<form method = "post" action="" name= "start_game">
<input type ="Submit" name = "start_game" value = "Start Game" align ="MIDDLE">

</form>

 


</body>
</html>
Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.