A3 - Hangman Game, due Sun, Apr. 21
in CS 102 on C++, Assignments
For this assignment you will program the game of hangman in C++.
Game Requirements
- Choose the word to guess randomly from an array of preselected strings. For example,
string words[5]= {"strawberry", "lime", "orange", "lemon", "mango"}; // program randomly chooses a word from this hard coded list.
- Allow the user to guess only an alphabetic letter (
A
toZ
(uppercase) ora
toz
(lowercase)) - Each round, display the correctly guessed letters and their placement in the word. For example:
* * * *
may become* * m *
when the user guessesm
for the wordlime
. - Keep track of the number of points/the score. You can be creative with how points are allocated, but your method should be purposeful/reasonable.
- Instead of a display for the arms, legs, etc., display the number of attempts left. In hangman, there is a head, 2 legs, 2 arms, and a torso, so
6
wrong guesses are allowed. - Each round, display all previously guessed letters, regardless of whether they were right or wrong.
- After each game, regardless of win or lose, ask the user if he/she wants to play again.
Submission Requirements
- Prettify your code
- Cite your sources/references using comments (
//
or/* */
) - Use comments, where appropriate, to explain your code/logic
- How did you test your program? Track bugs and how you resolved them with comments.
- Consider how you will present your game’s code/logic without any PowerPoints/slides.
Note: if your program does not work, demonstrating how you tested/debugged your program will earn you credit.
In-Class Program
#include <iostream>
#include <vector>
using namespace std;
string answer;
string words[] = {"christmas", "thanksgiving", "presidents"};
vector <char> display(0);
int attempts = 6;
void printDisplay(){
for (int i = 0; i < display.size(); i++){
cout << display.at(i) << " ";
}
cout << endl;
}
void initDisplay(){
for (int i = 0; i < answer.size(); i++){
display.push_back('*');
}
}
// return instances of letter
int checkLetter(char guess){
int instances;
for (int i = 0; i < answer.size(); i++){
if (answer.at(i) == guess){
display.at(i) = guess;
instances++;
}
}
return instances;
}
char enterLetter(){
char guess;
cout << "Enter a letter: ";
cin >> guess;
cout << "You have entered: " << guess << endl;
// check if input is really a letter
while (!((guess >= 'A' && guess <= 'Z') || (guess >= 'a' && guess <= 'z'))){
cout << "Invalid guess. Try again!" << endl;
cout << "Enter a letter: ";
cin >> guess;
cout << "You have entered: " << guess << endl;
}
return guess;
}
bool isComplete(){
for (int i = 0; i < display.size(); i++){
if (display.at(i) == '*'){
return false;
}
}
return true;
}
int main() {
srand(time(0));
answer = words[rand() % 3] ;
initDisplay(); // hide the word
printDisplay();
while (!isComplete() && attempts > 0){
char guessedLetter = enterLetter();
cout << "The letter " << guessedLetter << " appears " << checkLetter(guessedLetter) << " times." << endl;
if (checkLetter(guessedLetter) <= 0) {
attempts--;
cout << "Wrong guess! You have " << attempts << " attempts left." << endl;
}
printDisplay();
} // game ended
if (attempts <= 0) {
// you lost
cout << "Oh no, you lost! The word is " << answer << endl;
} else {
// you won
cout << "Congrats!" << endl;
}
return 0;
}
Sample Output
Guessing the word "strawberry":
----------------------------
HANGMAN
----------------------------
* * * * * * * * * *
Guess a letter: s
Score: 1; Highscore: 0; Attempts: 7
Previous guesses: s
s * * * * * * * * *
Guess a letter: t
Score: 2; Highscore: 0; Attempts: 7
Previous guesses: s t
s t * * * * * * * *
Guess a letter: r
Score: 5; Highscore: 0; Attempts: 7
Previous guesses: s t r
s t r * * * * r r *
Guess a letter: p
Score: 5; Highscore: 0; Attempts: 6
Previous guesses: s t r p
s t r * * * * r r *
Guess a letter: o
Score: 5; Highscore: 0; Attempts: 5
Previous guesses: s t r p o
s t r * * * * r r *
Guess a letter: y
Score: 6; Highscore: 0; Attempts: 5
Previous guesses: s t r p o y
s t r * * * * r r y
Guess a letter: w
Score: 7; Highscore: 0; Attempts: 5
Previous guesses: s t r p o y w
s t r * w * * r r y
Guess a letter: a
Score: 8; Highscore: 0; Attempts: 5
Previous guesses: s t r p o y w a
s t r a w * * r r y
Guess a letter: b
Score: 9; Highscore: 0; Attempts: 5
Previous guesses: s t r p o y w a b
s t r a w b * r r y
Guess a letter: u
Score: 9; Highscore: 0; Attempts: 4
Previous guesses: s t r p o y w a b u
s t r a w b * r r y
Guess a letter: e
Score: 10; Highscore: 0; Attempts: 4
Previous guesses: s t r p o y w a b u es t r a w b e r r y
You won!
Want to play again?
----------------------------
HANGMAN
----------------------------
* * * *
Guess a letter: l
Score: 1; Highscore: 10; Attempts: 7
Previous guesses: l
l * * *
Guess a letter: m
Score: 2; Highscore: 10; Attempts: 7
Previous guesses: l m
l * m *
Guess a letter: g
Score: 2; Highscore: 10; Attempts: 6
Previous guesses: l m g
l * m *
Guess a letter: e
Score: 3; Highscore: 10; Attempts: 6
Previous guesses: l m g e
l * m e
Guess a letter: q
Score: 3; Highscore: 10; Attempts: 5
Previous guesses: l m g e q
l * m e
Guess a letter: e
Score: 3; Highscore: 10; Attempts: 4
Previous guesses: l m g e q
l * m e
Guess a letter: e
Score: 3; Highscore: 10; Attempts: 3
Previous guesses: l m g e q
l * m e
Guess a letter: g
Score: 3; Highscore: 10; Attempts: 2
Previous guesses: l m g e q
l * m e
Guess a letter: s
Score: 3; Highscore: 10; Attempts: 1
Previous guesses: l m g e q s
l * m e
Guess a letter: a
Score: 3; Highscore: 10; Attempts: 0
Previous guesses: l m g e q s a
l * m e
You lost! The word was l i m e
Want to play again?