Let's make a simple game using strings! Toward this goal, we will need to implement some
functions.
Part 1: Writing helper functions
Download lab07.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.
~/$./lab07
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
original | covered | c | output
|
"housepet" | "_______" | 'e' | "____e_e_"
|
"hello" | "_e___" | 'l' | "_ell_"
|
If you look at how the game is played (below), it's easy to see why this function would be useful for implementing StringGame.
string crossOut(char c, string s);
This function "crosses out" (with an asterisk) all occurrences of 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=lab07 lab07.cpp
Part 2: StringGame
~/$ ./game
Welcome to StringGame!
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 StringGame!
Before you start:
- Download words07.txt containing a
list of words of 7 characters.
- Make a copy of your lab07.cpp to game.cpp.
- 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. You'll also need
to include cstdlib for these rand functions.
Other details that you must follow:
- The output must match the example run exactly.
- You must use all the helper functions. If you do not use all of them, you will not receive full credit.
- Tip: Create a string with "abcdefghijklmnopqrstuvwxyz" as its value. Then use your crossOut() function with the user's guesses to update it and show on the screen.
- Tip: You can create and fill the "_____" string with your mkShadowString() and uncover() functions as appropriate.
- Feel free to write any other functions you think would be helpful to you.
Submit: ~/bin/submit -c=IC210 -p=Lab07 lab07.cpp game.cpp
Part3: Going further, extra challenge
TBA
Note: If you get a nice working game, please demo it to your instructor.