<!--
    Brian Platz
    CS85 Professor Geddes
    March 18 2012
    Assignment:  Write the classic "I'm thinking of a number" game. Have the computer randomly 
                generate a number and let the user guess its value. Tell the user if she is too 
                high, too low, or correct. When she guesses correctly, tell how many turns it took.
                No arrays are necessary, but you will need to have persistent data with hidden fields 
                or session variables.
                
                THIS SOLUTION USES HIDDEN FIELDS
    --> 
<!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" xml:lang="en">
   <head>
      <title>I'm thinking of a number...</title>
      <style type="text/css">
         body {background-color:#90EE90}
         h1 {color:black; font-family:Ariel; font-size:14pt; font-weight:bold}
         h2 {color:red; font-family:Ariel; font-size:14pt; font-weight:bold}
         h3 {color:black; font-family:Ariel; font-size:24pt; font-weight:bold}
      </style>    
   </head>
   <body> 
         <h3>***Guess My Number***</h3>
      <?php
      
extract
($_REQUEST); // create form variables 
error_reporting(E_ALL & ~E_NOTICE);
          global 
$guess$counter$randNum;
                
        
$guess filter_input(INPUT_POST"guess");
        
$counter filter_input(INPUT_POST"counter");
        
$randNum filter_input(INPUT_POST"randNum");
      
        if ( 
filter_has_var(INPUT_POST"guess") ) {
              if (
$guess == "") {                                 // if no guess, start new game
                  
startGame();
                  } else {                                        
// if valid selection, continue game
                 
continueGame();
                 }
            } else {
             
startGame();
      } 
           
      function 
startGame() {
            global 
$guess$counter$randNum;
            
$counter 1;
            
$randNum rand(1100); 
                   
// display data and create hidden fields
          
print <<<HERE
            <form method='post' action='' name='form1'> 
            <h3>Welcome to the game!</h3><br/><h1>I'm thinking of a number between 1 and 100...<br/>
            Enter your guess in the box...<br/><input type='text' id='guess' name='guess' value='
$guess' size='3'/>
            <button type='submit' >Submit</button><br/>
            <input type='hidden' name='counter' value='
$counter'><br/>
            <input type='hidden' name='randNum' value='
$randNum'></h1>
            </form>
HERE;
      }
            
      function 
continueGame() {
              global 
$guess$counter$randNum;
          
              
$ordinals = array('error''first''second''third''fourth''fifth''sixth''seventh''eighth''ninth''tenth''eleventh''twelfth');
            
                
$guess filter_input(INPUT_POST"guess");            // retrieve hidden variable
            
$counter filter_input(INPUT_POST"counter");      // retrieve hidden variable
            
$randNum filter_input(INPUT_POST"randNum");      // retrieve hidden variable
            
            
ECHO <<<HERE
                <form method='post' action='' name='form1'> 
                <h1>Your 
$ordinals[$counter] guess was $guess.</h1><br/>
HERE;
            if (
$guess == $randNum) {
                ECHO <<<HERE
                    <h1>THAT'S IT!! You guessed the number in $counter tries!</h1><br/>
                    <input type='button' id='myButton1' onclick="location.href='Unit4_part1_guessAnumber_hid_field.php'" value='Play Again'/>
HERE;
                } else { 
                if (
$guess $randNum) {
                    ECHO 
"<h1>That guess is TOO LOW! Try again...<br/></h1>";
                    } else {
                    ECHO 
"<h1>That guess is TOO HIGH! Try again...<br/></h1>";
                    }
                
$counter++;
                ECHO <<<HERE
                    <form method='post' name='form1'> Enter your guess in the box...<br/><input type='text' name='guess' size='3'/>
                    <button type='submit' >Submit</button>
                    <input type='hidden' name='counter' value='
$counter'><br/>
                    <input type='hidden' name='randNum' value='
$randNum'></h1>
                    </form>
HERE;
            }
      }  
        echo 
"<a href='../index.html#unit4' >Return to Index</a>";
      
?>
   </body>
</html>