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 273 274 275 276 277 278 279 280 281 282 283
|
/*----------------------------------------------------------*/
/* Program Hangman.cpp
/*----------------------------------------------------------*/
#include <iostream> // Required for cin, cout endl
#include <fstream> // required for ifstream
#include <string> // required for string functions
#include <cctype> // required for toupper
#include <time.h> // required for srand
#include <cstring> // required for strcpy
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
string THE_WORD; // word to guess
const int MAX_WRONG = 7; // maximum number of incorrect guesses allowed
bool match(char letter, string &word);
void askGuess(string& usedLettersStr, string &soFar, int &correct, int &incorrect);
bool playAgain();
void DrawGallows (int wrong);
string goal(string file_name);
int main()
{
// declare variables
int wrng(0), cor(0);
string soFar, used, filename, word;
vector<string>words;
cout << "Welcome to Hangman! \n";
bool done = false;
cout << "\nFilename you want to play, hangman1.txt, hangman2.txt: ";
cin >> filename;
// call function to retrive word from file
word = goal(filename);
words.push_back(word);
do
{
THE_WORD = words[0]; // word to guess
soFar = string(THE_WORD.size(), '-'); // word guessed so far
used = ""; // letters already guessed
cout << THE_WORD <<"\n" << soFar << "\n";// added for debug
// loop for current word
while ((wrng < MAX_WRONG) && (soFar != THE_WORD))
{
DrawGallows (wrng);
cout << "\n\nYou have " << (MAX_WRONG - wrng) << " incorrect guesses left." << wrng << "\n";
cout << "\nYou've picked the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;
askGuess(used, soFar, cor, wrng);
// end of while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) shut down
}
if (wrng == 7)
DrawGallows(wrng);
cout << "\nThe word was " << THE_WORD << endl;
} while (playAgain());
return 0;
}
/*----------------------------------------------------------*/
/* function askGuess */
/*----------------------------------------------------------*/
void askGuess(string& usedLettersStr, string &soFar, int &correct, int &incorrect)
{
// declare local variables
bool cor;
char guess;
cout << "\n\nEnter your guess : ";
cin >> guess;
guess = toupper(guess); //make uppercase since secret word in uppercase
usedLettersStr = usedLettersStr + guess;
cor = match(guess, soFar);
if (cor == 1)
correct++;
else
incorrect++;
}
/*----------------------------------------------------------*/
/*----------------------------------------------------------*/
/* function match */
/*----------------------------------------------------------*/
bool match(char letter, string &word)
{
// declare local variables
string soFar;
int i(0), j(0), pos, num;
cout<< THE_WORD <<"\n";//touble shooting find if the letter entered is in the word
if (THE_WORD.find(letter) <= THE_WORD.length())
{
num = 0;
while (num < THE_WORD.length())
{
if (THE_WORD[num] == letter)
word[num] = letter;
num++;
}
cout << "That's right! " << letter << " is in the word.\n";
return 1;
}
else
{
cout << "Sorry, " << letter << " isn't in the word.\n";
return 0;
}
}
/*----------------------------------------------------------*/
/* function playAgain */
/*----------------------------------------------------------*/
bool playAgain()
{
char again;
cout << "\n\nWould you like to play again? <y/n>: ";
cin >> again;
cin.clear(); //clear and ignore cin
cin.ignore();
again = toupper(again);
system("cls");
return (again == 'Y');
}
/*----------------------------------------------------------*/
/*----------------------------------------------------------*/
/* function DrawGallows */
/*----------------------------------------------------------*/
void DrawGallows (int wrong)
{
switch (wrong)
{
case 0:
cout << " Hangman" << endl << endl
<< " *------*" << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " *---------" << endl;
break;
case 1:
cout << " *------*" << endl
<< " | 0" << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " *---------" << endl;
break;
case 2:
cout << " *------*" << endl
<< " | 0" << endl
<< " | | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " *---------" << endl;
break;
case 3:
cout << " *------*" << endl
<< " | 0" << endl
<< " | | " << endl
<< " | | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " *---------" << endl;
break;
case 4:
cout << " *------*" << endl
<< " | 0" << endl
<< " | \\| " << endl
<< " | | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " *---------" << endl;
break;
case 5:
cout << " *------*" << endl
<< " | 0" << endl
<< " | \\|/ " << endl
<< " | | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " *---------" << endl;
break;
case 6:
cout << " *------*" << endl
<< " | 0" << endl
<< " | \\|/ " << endl
<< " | | " << endl
<< " | / " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " *---------" << endl;
break;
case 7:
cout << " *------*" << endl
<< " | 0" << endl
<< " | \\|/ " << endl
<< " | | " << endl
<< " | / \\ " << endl
<< " | " << endl
<< " | " << endl
<< " | " << endl
<< " *---------" << endl;
break;
}
}
/*----------------------------------------------------------*/
/*----------------------------------------------------------*/
/* function goal */
/* read words from file and pick one for game */
/*----------------------------------------------------------*/
string goal(string file_name)
{
//declare local variable
int cnt(0), index(0);
string temp;
ifstream input;
input.open(file_name.c_str());
// if file fails to open ouput error message
if (input.fail())
{
cerr << "Error opening the requsested file!\n";
exit (-1);
}
// random seed and random genrator
srand(time(0));
index = rand() % 10 + 1;
while (cnt < index)
{
getline (input, temp);
cnt++;
}
// close file
input.close();
return temp;
}
/*----------------------------------------------------------*/
|