<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Guessing Game</title>
    <link rel="stylesheet" href="../lit/genstyle.css" type="text/css">
    <style>
     body { text-align: center; }
     p { width: 80%; margin: auto; }
     p.first { font-style: italic; }
     p.win {
	 background-color: rgb(30%,100%,30%);
	 color: dark-green;
	 padding: 0.3em;
     }
     p.lose { background-color: red; padding: 0.3em; }
     form { margin-top: 0.5em; }
     input[type="text"] { text-align: right; }
    </style>
  </head>
  <body>
    <h1>Guessing Game</h1>
    <h2>This page is the home of the wonderful guessing game.
      How exciting!</h2>
    <?php
       define("GUESS_LIMIT", 6);
       define("MAX_NUM", 100);

       // This tells if the code should generate the start again form
       // as opposed to the guess again form.
       $start_form = 0;

       // Do this when someone comes here by a plain link.
       if(!$_POST) {
           /* This runs if there is no post data, so it was not run from a
              form. */
           $start_form = 1;
           echo <<<END
		       <p class="first">This page is the home of the 
				  exciting number guessing game, where I pick a 
				  number and you try to figure out what it is.
				  Would you like to play?</p>
END;
       } else {
           $resend = 1;
           if($_POST['initial'] === "1") {
               // First load.  Choose the number.
               $randno = rand(1,MAX_NUM);
	           $guesscount = 0;
		       $comment = '';
               echo "<p>I'm thinking of a number between 1 and ", MAX_NUM,
                   ".  Can you guess it?</p>";
           } else {
               // Not first load.  Need to evaluate.
               $randno = $_POST['randno'];
               $guess = $_POST['guess'];
               $guesscount = $_POST['guesscount'];
               if($randno == $guess) {
                   $start_form = 1;
                   echo "<p class='win'>Congratulations! Your guess of $guess ",
                       "is correct!  Would you like to play again?</p>";
               }
               else {
                   if($guess < $randno - MAX_NUM / 2) {
                       $comm = "way too small";
                       $comcl = "wts";
                   } elseif($guess < $randno) {
                       $comm = "too small";
                       $comcl = "ts";
                   } elseif($guess > $randno + MAX_NUM / 2) {
                       $comm = "way too large";
                       $comcl = "wtl";
                   } else {
                       // Already checked if it matches exactly. 
                       $comm = "too large";
                       $comcl = "tl";
                   }
                   echo "<p class='wrong'>No, your guess of $guess is $comm.  ";

                   // See if the game will continue.
                   if($guesscount < GUESS_LIMIT) {
                       echo "Care to try again?  You have ", 
                           GUESS_LIMIT-$guesscount, " guesses left.</p>";
                   } else {
                       echo "</p><p class='lose'>I'm sorry, you have used all ",
                           "of your ", GUESS_LIMIT, " guesses.  The number ",
                           "I was thinking of was $randno.  Would you like ",
                           "to try again?</p>";
                       $start_form = 1;
                   }
               }
           }
       }

       // Game continues.  Generate the correct form.
       $me = $_SERVER['PHP_SELF'];
       if($start_form) {
           echo <<<END

	       <form method="post" action="$me">
	  	  <input type="submit" value="YES">
		  <input type="hidden" name="initial" value="1">
	       </form>
END;
        } else {
            ++$guesscount;
            echo <<<END

		<form method="post" action="$me">
                   <input type="text" name="guess" size="3">
		   <input type="submit" value="GO">
		   <input type="hidden" name="randno" value="$randno">
   		   <input type="hidden" name="guesscount" value="$guesscount">
		 </form>
END;
        } 
    ?>
  </body>
</html>