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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
//--------------------------------
//------------Globals-------------
//--------------------------------
using namespace std;
const int MAX_TRIES = 7;
int letterFill(char, string, string&);
void gameboardskeleton(void);
//--------------------------------
//-----------ProtoTypes-----------
//--------------------------------
void PrintWelcome(void);
void gameboard1(void);
void gameboard2(void);
void gameboard3(void);
void gameboard4(void);
void gameboard5(void);
void gameboard6(void);
void gameboard7(void);
//--------------------------------
//--------------Main--------------
//--------------------------------
int main()
{
string name;
char letter;
int num_of_wrong_guesses = 0;
string word;
srand(time(NULL));
// Ask user for for a category
string category;
cout << "\nChoose a Category!\n 1) Tv Characters\n 2) Animals\n 3) Video Games:" << endl;
cin >> category;
PrintWelcome();
// compare category
if (category == "1") //TV Characters
{
//put all the string inside the array here
string tvchars[] = { "Archer", "Bender", "Morty", "Rick", "Murderface", "Zoidberg", "Quagmire", "Meatwad", "Squanchy" };
string word;
int n = rand() % 9;
word = tvchars[n];
//call the function here for guessing game
// Initialize the secret word with the - character.
string unknown(word.length(), ' -'); //called unknown because I already had a name of secretword.
// Loop until the guesses are used up*/
gameboardskeleton();
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n\t\t\t" << unknown;
cout << "\n\nGuess a letter: \n";
cout << word << " is the word you're looking for\n"; //used for debug
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Try again." << endl;cout << "The word was : " << word << endl;
num_of_wrong_guesses++;
}
if (num_of_wrong_guesses == 0)
{
gameboardskeleton();
}
if (num_of_wrong_guesses == 1)
{
gameboard1();
}
if (num_of_wrong_guesses == 2)
{
gameboard2();
}
if (num_of_wrong_guesses == 3)
{
gameboard3();
}
if (num_of_wrong_guesses == 4)
{
gameboard4();
}
if (num_of_wrong_guesses == 5)
{
gameboard5();
}
if (num_of_wrong_guesses == 6)
{
gameboard6();
}
if (num_of_wrong_guesses == 7)
{
gameboard7();
}
else
{
cout << endl << "You found a letter." << endl; //lettertrue function
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses << " guesses left." << endl;
// Check if user guessed the word.
if (word == unknown)
{
cout << word << endl;
cout << "you win"; //youwin congrats function
break;
}
}
if (num_of_wrong_guesses == MAX_TRIES)
{
cout << "\nSorry, you lose...you've been hanged." << endl; //youlose function
cout << "The word was : " << word << endl; //tbdeleted
}
cin.ignore();
cin.get();
return 0;
}
return 0;
}
}
//--------------------------------
//---------Implementation---------
//--------------------------------
int letterFill(char guess, string secretword, string &guessword)
{
int i;
int matches = 0;
int len = secretword.length();
for (i = 0; i< len; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;
// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void PrintWelcome(void)
{
cout << " ________________________________________________________________\n";
cout << "| Each letter is represented by a dash. |\n";
cout << "| You have to type only one letter in one try. |\n";
cout << "| You have " << MAX_TRIES << " tries to try and guess the word. |\n";
cout << "|________________________________________________________________|\n";
}
void gameboardskeleton(void)
{
cout << "\t ____________ " << endl;
cout << "\t | | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t ___________|_________" << endl;
}
void gameboard1(void)
{
cout << "\t ____________ " << endl;
cout << "\t | | " << endl;
cout << "\t ( ) | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t ___________|_________" << endl;
}
void gameboard2(void)
{
cout << "\t ____________ " << endl;
cout << "\t | | " << endl;
cout << "\t ( ) | " << endl;
cout << "\t | | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t ___________|_________" << endl;
}
void gameboard3(void)
{
cout << "\t ____________ " << endl;
cout << "\t | | " << endl;
cout << "\t ( ) | " << endl;
cout << "\t |_ | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t ___________|_________" << endl;
}
void gameboard4(void)
{
cout << "\t ____________ " << endl;
cout << "\t | | " << endl;
cout << "\t ( ) | " << endl;
cout << "\t _|_ | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t ___________|_________" << endl;
}
void gameboard5(void)
{
cout << "\t ____________ " << endl;
cout << "\t | | " << endl;
cout << "\t ( ) | " << endl;
cout << "\t _|_ | " << endl;
cout << "\t | | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t ___________|_________" << endl;
}
void gameboard6(void)
{
cout << "\t ____________ " << endl;
cout << "\t | | " << endl;
cout << "\t ( ) | " << endl;
cout << "\t _|_ | " << endl;
cout << "\t | | " << endl;
cout << "\t \\ | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t ___________|_________" << endl;
}
void gameboard7(void)
{
cout << "\t ____________ " << endl;
cout << "\t | | " << endl;
cout << "\t ( ) | " << endl;
cout << "\t _|_ R.I.P. | " << endl;
cout << "\t | | " << endl;
cout << "\t / \\ | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t ___________|_________" << endl;
}
|