------------------------------------------------------------------------------
MC logo
CSc 231 Assignment 3
[^] PHP Programming Assignments
------------------------------------------------------------------------------

90 pts

Hangman

Due: Nov 1

Use PHP to produce an on-line hangman game like this one. The game chooses a word of at least five characters at random, and the player tries to figure out what word has been chosen by guessing letters. They player must find the word with no more than six incorrect guesses.

The word is initially shown as a sequence of blanks, one for each letter. When a letter is guessed, if it occurs in the chosen word, the blank(s) where that letter resides are replaced with the letter. If it does not appear in the word, it counts as a wrong guess, and one part is added to the stick figure on the stick gallows. The figure has six parts. If a wrong guess is made after the figure is fully drawn, the player loses. If all the letters are guessed, the player wins.

The Example Solution

The example solution uses a PHP session to keep track of the word selected, along with other variables that change during a game. All game actions are formed using ordinary links back to the same script. These links a single parameter thelet. This parameter is either the letter guessed, or it indicates the new game or surrender actions. Of course, if there is no thelet, the reference is treated as a new game. The session id is passed back through a cookie.

The words are chosen from a list available at here. The file is simply a list of words, one per line. It does include names. You will need to download a copy and store it somewhere in your own account; the security settings used for PHP prevent us from sharing a copy.

My code to choose a word at random performs the following steps:

  1. Open the file using fopen.
  2. Find the length of the file using fstat.
  3. Use rand to choose a random offset within the file, and use fseek to move to that place in the file.
  4. Since the random location might be in the middle of a line, move ahead to the start of the next line.
  5. Read words until finding one with at least five letters.
  6. If it reaches the end of the file without finding such a word, it starts back at the top.

The stick figures are a sequence of GIF images. You may use mine, located at http://sandbox.mc.edu/~bennet/php/leccode/imgs/hang0.gif through http://sandbox.mc.edu/~bennet/php/leccode/imgs/hang6.gif, or you may make prettier ones.

Submission

Simply leave your page online on sandbox. Call it a3.php, and store it in a directory named csc231 under local_html under your own account on Sandbox. I'll run it and collect a copy of the source from there. Please leave the file in place at least until you receive a grade.