Let's make a game Hangman! Toward this goal, we will need to implement some functions.

Part 1: Writing helper functions

Download lab08.cpp as the starter file. The starter file contains only the main function and the prototype of the helper functions. You have to give the definition of each helper function. Your final program must output exactly as below right.
~/$./lab08
Testing printSpaced...
[w a s h]
[H e l l o W o r l d !]

Testing mkShadowString...
original: food
  shadow: ____
original: require
  shadow: _______

Testing uncover...
housepet
________
____e_e_ there were e's!
____e_e_ there were not q's!

Testing crossOut...
Go get good food!
G* get g**d f**d!
G* *et ***d f**d!

void printSpaced(string s);
This function prints a string with a space inserted in between consecutive characters. Thus, "wash" gets printed as if it were "w a s h".


string mkShadowString(string s);
Given a string, this function returns a string of the same length, but consisting solely of underscores (_'s). For example, "food" will be shadowed into "____".


string uncover(string original, string covered, char c);
All occurrences of the character c within string original will be uncovered from string covered. Here, original and covered have the same length. For example, we will have
originalcoveredc output
"housepet""_______"'e'"____e_e_"
"hello" "_e___" 'l' "_ell_"
It is easy to see why this function would be useful for implementing Hangman.


string crossOut(char c, string s);
This function crosses out the input character c from the input string s, and returns the result string. For example, if you cross out character 'e' from string "get", we will have string "g*t".

Submit: ~/bin/submit -c=IC210 -p=lab08 lab08.cpp

Part 2: Hangman

~/$ ./hangman
Welcome to hangman!
Enter a seed value: 17
Wrong guesses remaining: 8  abcdefghijklmnopqrstuvwxyz
_ _ _ _ _ _ _
: a
Wrong guesses remaining: 8  *bcdefghijklmnopqrstuvwxyz
_ a _ _ _ _ _
: s
There were no s's!
Wrong guesses remaining: 7  *bcdefghijklmnopqr*tuvwxyz
_ a _ _ _ _ _
: e
Wrong guesses remaining: 7  *bcd*fghijklmnopqr*tuvwxyz
_ a _ _ _ e _
: o
There were no o's!
Wrong guesses remaining: 6  *bcd*fghijklmn*pqr*tuvwxyz
_ a _ _ _ e _
: u
There were no u's!
Wrong guesses remaining: 5  *bcd*fghijklmn*pqr*t*vwxyz
_ a _ _ _ e _
: s
There were no s's!
Wrong guesses remaining: 4  *bcd*fghijklmn*pqr*t*vwxyz
_ a _ _ _ e _
: t
There were no t's!
Wrong guesses remaining: 3  *bcd*fghijklmn*pqr***vwxyz
_ a _ _ _ e _
: r
Wrong guesses remaining: 3  *bcd*fghijklmn*pq****vwxyz
_ a r r _ e r
: i
Wrong guesses remaining: 3  *bcd*fgh*jklmn*pq****vwxyz
_ a r r i e r
: b
You win!!!! The word was barrier
You'll now use all of the above to make Hangman!

Before you start:

  1. Download words07.txt containing a list of words of 7 characters.
  2. Make a copy of your lab08.cpp to hangman.cpp.
  3. Keep the helper functions, but delete everything in your main() function to start fresh.

On the right is an example run of what you need to create.

If the number of guesses hits zero, then instead print "You lose!!!!". It thus ends with two possible outputs:

You win!!!! The word was THEWORD
You lose!!!! The word was THEWORD

Your program must first choose a random word from the file words07.txt at the start of the program. You can hardcode this file name into your program.

How do you pick a random number? Recall that you first need a seed value that you will get from the user, and then get a random number like so:

int seed;
cin >> seed;
srand(seed);
int n = rand() % 1466;

We will choose one of the 1466 words from words07.txt. In other words, you must read the nth word from that file where n is your random number! Start counting the words in the file at 0, as usual. That is, if n is 0, it means that the first word has been chosen in the file. You'll also need to include cstdlib for these rand functions.

Other details that you must follow:

Submit: ~/bin/submit -c=IC210 -p=lab08 lab08.cpp hangman.cpp