Jump to content

Php reverse guessing game


webdevdea

Recommended Posts

Ok so I got a little more work done, it still will not recycle when you click submit the second time, for loop maybe? 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($_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;
$start_game = isset($_POST['start_game']) ? $_POST['start_game'] : 0;
$myGuess=rand($lowest,$highest); 
 
 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;
else if ($_POST['choice']=="start_game"){

echo "My Guess: $myGuess "; 
   }


  
  
  
  
//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="radio" name="choice" value="start_game"><b>StartGame</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>

Sorry I've been out all day.. Anyway, I've gone from my code that I posted earlier, and implemented a start button. Compare this with your code to see where you've done things differently, so you know what to do in the future.

 

 

<?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']) || isset($_POST['myGuess'])){
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 = "showOptions";
 }else{
$form = "showStart";
 }
 
 
if($form == "showOptions"){
?>
<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;?>
<?php
} else{
?>
<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="submit" name="start_game" id="start_game" value="Start Game"/>
</form>
<?php
}
?>
</body>
</html>

Needless to say that I've tried this and it's working perfectly.

 

Denno

Well done getting it finished..

 

One thing I would like to mention though, I hope you're not getting graded on your styling.. It's rather difficult to read with that background and the font colours.

 

But at least the core of your program works.

  • 4 weeks later...

That's the reason I'm here, teaching people as they ask questions :). I hope you do get that A!

My grads as of now is 100, I am waiting on one assignment to be graded and I have to finish the final major project.. :-) Thanks guys and gals for all the help.. on to php2 next semester Here is my folder for the semester, assignment 1-10 were for class, 30-32 are major projects, all of the others are just codes I was working on 

http://www.ctcsports.org/upload/Summer2013/CIST2351/900104329/Assignments.php

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.