Jump to content

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.

Yeah, the styling is up to us, I could not get any colors that I could see with that background, but I liked it lol, so Im pretty sure I should get an A lol.. thanks again for the help, You should be teaching this stuff lol.. 

  • 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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.