-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangmangameClass.cpp
137 lines (121 loc) · 4.11 KB
/
hangmangameClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
class HangmanGame{
public:
// Class Member Variables
string mWord;
int mPlayerLives; // Our hangman has 7 stages, so the player starts with 7 lives
bool mRunning;
char mKnownLetters[20] = ""; // the word could be of any length
int mAmountOfWords; // Replace 11 with the amount of words we have in our word array
//Constructors, Destructors, and Functions
HangmanGame();
HangmanGame(string, int, bool, int);
~HangmanGame();
string GenerateWord();
void PrintMan();
void PrintLetters();
void PlayerLose();
void PlayerWin();
};
//Constructor
HangmanGame::HangmanGame(string aWord, int aPlayerLives, bool aRunning, int aAmountOfWords) : mWord(aWord), mPlayerLives(aPlayerLives), mRunning(aRunning), mAmountOfWords(aAmountOfWords){
}
//Destructor
HangmanGame::~HangmanGame(){
}
//Function to randomly pick a word within our wordlist that we created
string HangmanGame::GenerateWord(){
srand(time(NULL)); // Use the system time as a seed
int wordID = rand() % mAmountOfWords; //mAmountOfWords variable will always be +1 to whatever your wordList array size is.
string wordList[10] = {"pineapple", "orange", "people", "computer", "fire", "water", "cheese", "bottle", "plank", "lightbulb" };
string word = wordList[wordID];
return word;
}
//shows user the guessed letters if any by looping through the main word
//and checking each position based off the guess
void HangmanGame::PrintLetters(){
for(int pos = 0; pos < mWord.length(); pos++) {
if(mKnownLetters[pos] == mWord[pos]) cout << mWord[pos];
else cout << "_";
}
cout << "\n";
return;
}
//Draw the Hangman for each case
void HangmanGame::PrintMan(){
switch(mPlayerLives) {
case 7:
cout << "\n\n\n\n\n";
break;
case 6:
cout << "\n\n\n\n______";
break;
case 5:
cout << "\n |\n |\n |\n |\n____|_";
break;
case 4:
cout << " ___\n | \\|\n |\n |\n |\n____|_";
break;
case 3:
cout << " ___\n | \\|\n O |\n |\n |\n____|_";
break;
case 2:
cout << " ___\n | \\|\n O |\n | |\n |\n____|_";
break;
case 1:
cout << " ___\n | \\|\n O |\n/|\\ |\n |\n____|_";
break;
case 0:
cout << " ___\n | \\|\n O |\n/|\\ |\n/ \\ |\n____|_";
break;
}
cout << "\n"; // For formatting purposes
return;
}
//Print to the user that they lost
void HangmanGame::PlayerLose(){
cout << "You lose! The word was: " << mWord << "\n";
cout << "Press ENTER to quit\n"; // at the time this was for Code::Blocks
cin.get();
mRunning = 0;
return;
}
//Print to the user that they won
void HangmanGame::PlayerWin(){
cout << "Congratulations, you guessed the word correctly!";
cout << "Press ENTER to quit\n"; // at the time this was for Code::Blocks
cin.get();
mRunning = 0;
return;
}
//The main program
int main() {
HangmanGame hg("", 7, 1, 11); // Instantiate class
hg.mWord = hg.GenerateWord(); // pick a random word from our word array
char userGuess;
while(hg.mRunning){
cout << "Guess a letter: ";
cin >> userGuess;
bool correct = 0;
//checker loop
for(int pos = 0; pos < hg.mWord.length(); pos++) {
if(userGuess == hg.mWord[pos]) {
hg.mKnownLetters[pos] = userGuess; // If the letter is in the word, mark it as guessed
correct = 1;
}
}
if(correct == 0) hg.mPlayerLives--; // If the letter was wrong, subtract a life
correct = 0;
hg.PrintMan(); //displays hangman
hg.PrintLetters();
if(hg.mPlayerLives <= 0) hg.PlayerLose();
if(hg.mKnownLetters == hg.mWord) hg.PlayerWin();
}
system("PAUSE");
return EXIT_SUCCESS;
}