COMP 142 Program 6: Hangman

Assigned: Monday, March 30
Due: Monday, April 6, Thursday, April 9, 2015 by 11:55pm (on Moodle)

For this project you will create a simple game of Hangman. For those of you not familiar with hangman it's a guessing game where a player is given a word or phrase to guess represented by a row of dashes and the category of the word or phrase. The player guesses a letter that might be in the word or phrase. If the player guesses a letter that appears in the word, then the row of dashes is updated to show where that letter is in the puzzle. If the suggested letter does not occur in the puzzle, then the player receives one element of the hangman diagram. The game is over when the player guess the whole word correctly OR when the hangman diagram has been completed.

In our game, there will be 10 pieces that make up a hangman diagram. Thus, you have 10 chances to guess an incorrect letter. Guessing a correct letter does not change the hangman diagram. You won't actually have a hangman diagram in your game unless you do extra credit.

Here's how your game will work. The computer will read a file named hangman.txt that is in the same directory as the source code for this program. The first line of the file will contain a string indicating the puzzle and the second line of the file will contain the category of this puzzle. You can assume that this hangman.txt file will always contain these two things and you do not have to handle any file exceptions.

You will read the puzzle word and category into a struct. The struct MUST be defined as follows:

struct hangman
{
	string puzzle;
	string category;
	string board;
};
The board string will be used to display the puzzle to the user at game time.

To begin the game, you will provide the user with instructions. You will not have to implement a hangman diagram, but rather keep track of the number of "lives" that the user has left. The user starts the game with 10 lives and the game ends when the user wins and guesses all the correct letters or when the user loses and uses up all their lives.

Once you have explained the rules you will provide the user with the category of the puzzle. Then, you will display the number of lives that the player has left and the game board. The game board consists of '_' (underscores) for each of the letters in the string and ' '(spaces) for the spaces. For example, the phrase "hello there", is represented by: _ _ _ _ _    _ _ _ _ _

At each round of the game, the user will be asked to enter a letter. If the letter is in the puzzle, the board will be updated accordingly. If not, then the user will lose one of their lives. At each round you should display the updated puzzle and the number of lives.

The game stops when the user has used all their lives OR when the user has correctly filled in all the letters in the puzzle. The player wins when the puzzle string has the same value as the board string.

Hints

Here is some starter code and a sample hangman.txt file. In particular note that I show how to use some helpful string functions in this sample code. A useful reference for string functions: http://www.cplusplus.com/reference/string/string/ or you can refer to the string handout in class.

Sample Run

Welcome to the game of HANGMAN! The object of the game
is to guess the word represented by the dashes. You will
be asked to enter a letter. If you letter appears in
the puzzle, it will be added to the board. If it does
not, you will lose a life. You have 10 lives to start
the game. The winner will be declared when all of the
letters in the puzzle have been guessed.
GOOD LUCK!
Puzzle category: thing
Lives left: 10
_______ __ ___ _______
Enter character: r
Yes! That's in the puzzle
Lives left: 10
r______ __ ___ _______
Enter character: n
Yes! That's in the puzzle
Lives left: 10
r_nn_n_ __ ___ ___n___
Enter character: h
Yes! That's in the puzzle
Lives left: 10
r_nn_n_ __ _h_ ___n___
Enter character: v
Sorry, that letter is not in the puzzle. You have lost a life.
Lives left: 9
r_nn_n_ __ _h_ ___n___
Enter character: b
Sorry, that letter is not in the puzzle. You have lost a life.
Lives left: 8
r_nn_n_ __ _h_ ___n___
Enter character: q
Sorry, that letter is not in the puzzle. You have lost a life.
Lives left: 7
r_nn_n_ __ _h_ ___n___
Enter character: s
Yes! That's in the puzzle
Lives left: 7
r_nn_n_ __ _h_ ___n__s
Enter character: f
Yes! That's in the puzzle
Lives left: 7
r_nn_n_ _f _h_ ___n__s
Enter character: i
Yes! That's in the puzzle
Lives left: 7
r_nnin_ _f _h_ ___ni_s
Enter character: m
Sorry, that letter is not in the puzzle. You have lost a life.
Lives left: 6
r_nnin_ _f _h_ ___ni_s
Enter character: c
Sorry, that letter is not in the puzzle. You have lost a life.
Lives left: 5
r_nnin_ _f _h_ ___ni_s
Enter character: w
Yes! That's in the puzzle
Lives left: 5
r_nnin_ _f _h_ w__ni_s
Enter character: e
Yes! That's in the puzzle
Lives left: 5
r_nnin_ _f _he weenies
Enter character: o
Yes! That's in the puzzle
Lives left: 5
r_nnin_ of _he weenies
Enter character: p
Sorry, that letter is not in the puzzle. You have lost a life.
Lives left: 4
r_nnin_ of _he weenies
Enter character: t
Yes! That's in the puzzle
Lives left: 4
r_nnin_ of the weenies
Enter character: g
Yes! That's in the puzzle
Lives left: 4
r_nning of the weenies
Enter character: u
Yes! That's in the puzzle
GAME OVER
You win.

Coding style

Testing your program

You should test your program thoroughly to make sure it works correctly.

What to turn in

Through Moodle, turn in your code as a file called hangman_yourLastName_yourFirstName.cpp. You do not need to turn in anything other than the .cpp file; I do not need all the other files that Visual Studio creates.

Challenge Problems

  • Add more games to the text file and randomly select a puzzle.
  • Add an ASCII hangman art (something like the image shown at top of page).
  • Let the user guess the whole phrase of the word by typing it in.
  • Whatever you come up with that's cool! (include in the comments in header)