<?php session_start() ?>     
    <!--
    Brian Platz
    CS85 Professor Geddes
    December 25, 2011
    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 VERSION USES SESSION VARIABLES
    --> 
<!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:#FFE4E1}
         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);
      
      if ( isset( 
$_SESSION["counter"] ) ) {                    // return visit
              
$guess filter_input(INPUT_POST"guess" );
              if (
$guess == "") {                                 // if no guess, start new game
                  
startGame();
                  } else {                                        
// if valid selection, continue game
                 
$counter $_SESSION["counter"];
                 
continueGame();
                 }
            } else {                                            
// new session
             
startGame();
              }   
              
      function 
startGame() {
            
$counter 0;                            // initialize counter
           
$_SESSION["counter"] = $counter;
            
$randNum rand(1100);                 // get random number
           
$_SESSION["randNum"] = $randNum;
           
          print <<<HERE
            <form method='post' action=''> 
            <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' value='
$guess' name='guess' size='3'/>
            <button type='submit' value='Submit'>Submit</button> </h1>
            </form>
HERE;
            }
            
      function 
continueGame() {
              
$guess filter_input(INPUT_POST"guess" );
              
$ordinals = array('zeroth''first''second''third''fourth''fifth''sixth''seventh''eighth''ninth''tenth''eleventh''twelfth');
      
            
$counter $_SESSION["counter"];    // retrieve session variable
            
$randNum $_SESSION["randNum"];    // retrieve session variable
            
$counter++;        // increment guess count
            
ECHO <<<HERE
                <form method='post' action='' > 
                <h1>Your 
$ordinals[$counter] guess was $guess.</h1><br/>
HERE;
            if (
$guess == $randNum) {        // correct guess
                
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_ses_var.php'" value='Play Again'/>
HERE;
                
session_destroy(); 
                } else {                     
// incorrect guess
                
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>";
                    }
                ECHO <<<HERE
                    <form method='post' action=''> Enter your guess in the box...<br/><input id='guess' type='text' name='guess' size='3'/>
                    <button type='submit' >Submit</button></h1>
                    </form>
HERE;
                  
$_SESSION["counter"] = $counter;    // save session variable
                  
$_SESSION["randNum"] = $randNum;    // save session variable
                  
                
}
            }  
        echo 
"<a href='../index.html#unit4' >Return to Index</a>";
      
?>
   </body>
</html>