YCP Logo Assignment 3: Hangman

Due: Wednesday, October 22nd by 11:59 PM

Your task

Write a program that allows the user to play a game of Hangman. The program starts with a quotation which is obscured by displaying an asterisk (*) in the place of each letter of the quotation.

The game proceeds with the user trying to guess the letters in the quotation one at a time. Each correctly guessed letter results in all occurrences of that letter being revealed. Each incorrectly guessed letter reduces the total number of incorrect guesses the player is allowed. After 7 incorrect guesses, the user loses the game. The player wins by correctly guessing all of the letters that are part of the quote.

At each step, the program should remind the player of the letters he or she has already guessed (displaying them in alphabetical order.)

Here is an example session:

You have 7 guesses remaining.
**** ** *****, *** ***** *****. (Ken Thompson)
Letters used:
Your guess: a
Wrong!

You have 6 guesses remaining.
**** ** *****, *** ***** *****. (Ken Thompson)
Letters used: A
Your guess: w
Correct!

You have 6 guesses remaining.
W*** ** *****, *** ***** *****. (Ken Thompson)
Letters used: A W
Your guess: n
Correct!

You have 6 guesses remaining.
W**n *n *****, *** ***** *****. (Ken Thompson)
Letters used: A N W
Your guess: x
Wrong!

You have 5 guesses remaining.
W**n *n *****, *** ***** *****. (Ken Thompson)
Letters used: A N W X
Your guess: m
Wrong!

You have 4 guesses remaining.
W**n *n *****, *** ***** *****. (Ken Thompson)
Letters used: A M N W X
Your guess: e
Correct!

...game continues - some output deleted for brevity...

You have 4 guesses remaining.
When in d*ubt, use brute **r*e. (Ken Thompson)
Letters used: A B D E H I M N R S T U W X
Your guess: b
You guessed b already
Your guess: o
Correct!

You have 4 guesses remaining.
When in doubt, use brute *or*e. (Ken Thompson)
Letters used: A B D E H I M N O R S T U W X
Your guess: f
Correct!

You have 4 guesses remaining.
When in doubt, use brute for*e. (Ken Thompson)
Letters used: A B D E F H I M N O R S T U W X
Your guess: c
Correct!

You win!

Your program should use the following quote:

When in doubt, use brute force. (Ken Thompson)

The name in parentheses at the end of the quote is always displayed, and is not considered to be part of the quote for guessing purposes.

Getting Started

Download CS200_Assign3.zip. Import the contents of the zip file using Marmoset. You will modify the code in the file Hangman.c.

Hints

This program is difficult, so I recommend you start early and develop the code in an incremental fashion, i.e. in stages. For example,

  • Correctly print the random “game” quote with letters hidden by ‘*’.
  • Get a “guess” letter from the user and determine the occurrences within the quote.
  • After each user guess, print an updated quote revealing any correctly guessed letters, noting the number of incorrect guesses remaining, and listing the letters guesses so far.
  • Embed the previous two steps into a loop that terminates when either all of the letters in the quote are guessed or the maximum number of incorrect guesses is reached.

The user should be allowed to enter both upper and lower case letters. If a duplicate guess is made, the program should inform the user and ask for another guess.

Use an array of 26 elements (one for each letter) to keep track if a particular letter has been used (set the value to 0 for not used and 1 for used).

You can define an array containing your quote, and determine the number of characters in the quote, as follows:

char quote[100] = "When in doubt, use brute force. (Ken Thompson)";
int quote_len;

quote_len = strlen(quote);

You will need to add

#include <string.h>

to the top of your source file to be able to use the strlen function.

Grading criteria

Implementing the basic features above will earn a maximum grade of 80.

Up to 10 points may be deducted for poor coding style. You must use consistent indentation, and make sure you give meaningful names to variables and functions.

Extra credit

You may choose to implement one or both of the following extra credit options.

Option 1. For up to 10 additional points, draw a picture (using text characters) of a body hanging from a gallows at each step. Each incorrect guess adds another body part to the gallows. The body parts should be generated in the following order:

  1. head
  2. neck
  3. left arm
  4. right arm
  5. torso
  6. left leg
  7. right leg

Your gallows should look something like the following:

You have 4 guesses remaining.
 /----
 |   O
 |  \|
 |
 |
_|_
When in doubt, use brute *or*e. (Ken Thompson)
Letters used: A B D E H I M N O R S T U W X

The gallows above shows the head, neck, and left arm, since the user has made three incorrect guesses.

Option 2. For up to 10 additional points, read a list of quotes from a text file and choose one randomly when the program starts.

Submit

When you are done, choose Tools->Submit to submit your completed assignment to the server. You will need to enter your Marmoset username and password.

If the Tools->Submit option does not work, create a zip file containing the files in your project, and upload it to the submission server as assign3. The server URL is

https://camel.ycp.edu:8443/