<? session_start(); 

# ------------ register the session, pick the word, initialise "count" ------------
   
if (session_is_registered("word")) {
   } else { 
     
session_register("word"); 
     
$dictionary file('/usr/share/lib/dict/words'); 
     
$chooser rand(0sizeof($dictionary)); 
     
$word $dictionary[$chooser];
     
session_register("guesses");
     
$guesses "";
     
session_register("count");
     
$count 6;
   }

$images = array(=> "http://www.cs.indiana.edu/classes/a348-dger/sum2002/notes/h0.gif",
                
=> "http://www.cs.indiana.edu/classes/a348-dger/sum2002/notes/h1.gif",
                
=> "http://www.cs.indiana.edu/classes/a348-dger/sum2002/notes/h2.gif",
                
=> "http://www.cs.indiana.edu/classes/a348-dger/sum2002/notes/h3.gif",
                
=> "http://www.cs.indiana.edu/classes/a348-dger/sum2002/notes/h4.gif",
                
=> "http://www.cs.indiana.edu/classes/a348-dger/sum2002/notes/h5.gif",
                
=> "http://www.cs.indiana.edu/classes/a348-dger/sum2002/notes/h6.gif"); 

# ------------------ form the output variables ---------------
   
if ($reset) { 
      
$guesses ""
      
$dictionary file('/usr/share/lib/dict/words');
      
$chooser rand(0sizeof($dictionary));
      
$word $dictionary[$chooser];
      
$count 6
   } 
   
   
$masked "";
   
$already "";

  if (
strlen($inputChar)>&& strlen($guesses)>=0) { 
     if (!
ereg($inputChar$guesses)) { 
       
$chars preg_split('//',  $inputChar, -1PREG_SPLIT_NO_EMPTY);
       for (
$i 0$i sizeof($chars); $i++) { 
         if (!
ereg($chars[$i], $guesses)) {
          
$guesses .= $chars[$i]; 
         }
       }
       if (!
ereg($inputChar$word)) $count--;
     } else 
$already "You already picked \"" $inputChar "\"";
  } 

   
$chars preg_split('//',  $word, -1PREG_SPLIT_NO_EMPTY); 
   for (
$i 0$i sizeof($chars)-1$i++) {
     if (
ereg($chars[$i], $guesses)) {
       
$masked .= $chars[$i]; 
     } else {
       
$masked .= "-"
     }
   }
   
$masked .= $chars[$i];
 if (
strcmp($masked,$word)== 0) echo "Yes, the word is \" " $masked "\".";
 
?>

<html><head><title>Hangman.</title></head><body bgcolor=white>
<table>
<tr><td>

<? echo "<img src=\"" $images[6-$count] . "\"><p>";
   if (
strcmp($masked,$word)== 0) {
     echo 
"Congratulations, you won!<p>";
     echo 
"<form method=\"POST\" action=$SCRIPT_NAME>";
     echo 
"To reset please press <input type=\"submit\" name=\"reset\" value=\"Reset\">";
   } else if (
$count <= 0) {
            echo 
"Congratulations, you lost!";
            echo 
"<form method=\"POST\" action=$SCRIPT_NAME>";
            echo 
"To reset please press <input type=\"submit\" name=\"reset\" value=\"Reset\">";
   } else print <<<END
       <tr> <td><h3>Welcome to Hangman Game!</h3></td>
       <tr> <td> The word is: <td> <font color="brown">$word</font>
<tr> <td> Your guesses thus far: <td> $guesses
<tr> <td> You have $count wrong guesses left
<tr> <td> The word for you at this stage: <td> $masked
</table>
<form method="POST" action=$SCRIPT_NAME>
Please enter a character: <input type="text" name="inputChar"
                                 size=1 maxlength=1> $already<p>
       Then push <input type="submit" value="Proceed">
       To reset please press <input type="submit" name="reset" value="Reset"> <p>
       </form>
END;
?>
</table></body></html>