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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#include <iostream>
#include <array>
#include <time.h>
#include <cctype>
#include <string>
using namespace std;
int main()
{
bool restart = true;
while (restart == true) {
srand(time(0));
array <string, 20>listOfWords =
{
"phenomenal",
"insane",
"haunting",
"immaculate",
"duck",
"read",
"onomatopoeia",
"eloquent",
"ridiculous",
"jurassic",
"veni vidi vici",
"who you gonna call",
"ghostbusters",
"tremendous",
"variable",
"the game",
"blue",
"cuckoo",
"danny devito",
"oscar"
};
int randomWordNumber = rand() % 20;
string guessWord = listOfWords[randomWordNumber];
array <char, 20>guesses;
guesses.fill('_');
for (int count = 0; count < guessWord.length(); count++)
{
if (guessWord[count] == ' ')
{
guesses[count] = ' ';
}
}
for (int count = guessWord.length(); count < 20; count++)
{
guesses[count] = ' ';
}
bool running = true;
int lives = 5;
array <char, 20>lettersGuessed;
lettersGuessed.fill(' ');
char guess = 0;
array <char, 20>guess_history;and gave values to.
guess_history.fill(' ');
int y = 0;
bool trigger = false;
int m = 0;
char randnumber;
int iteration = 1;
bool allow = true;
bool trigger1 = true;
bool trigger2 = true;
string restart1;
cout << "Welcome to hangman!\nThe aim of the game is to guess letters in a word.\nEach underscore represents a letter!" << endl; // Line of code that explains the game to the user
for (int x = 0; x <= guessWord.length(); x++)
{ // this for loop prints out how many underscores there are for every letter
cout << guesses[x] << " ";
}
cout << endl << "You have 5 lives to start off with!" << endl;
cout << "PLEASE ONLY ENTER ONE LETTER AT A TIME!" << endl;
while (running == true)
{
trigger1 = true;
allow = true;
bool test = false;
trigger = false;
bool winFlag = false;
if (iteration == 20) {
goto label2;
}
if (iteration > 1 && isalpha(guess)) {
guess_history[y] = guess;
y++;
}
for (int x = 0; x < guessWord.length(); x++) {
if (iteration > 1 && isdigit(guess) || ispunct(guess) && guess_history[x] != guess)
{
}
}
if (iteration > 1 && lives >= 1 && winFlag == false) {
cout << "You have guessed the letters: ";
while (trigger == false)
{
cout << guess_history[m] << ", ";
m++;
if (guess_history[m] == ' ')
{
trigger = true;
}
}
}
m = 0;
cout << "\nPlease enter your guess : \n";
cin >> guess;
guess = tolower(guess);
cout << endl;
for (int x = 0; x < 20; x++)
{
if (guess == guess_history[x])
{
cout << "You have already guessed that! Enter a different letter please!\n";
cout << "\nPlease press enter to type in your next guess!\n";
while (trigger1 == true) {
cin.get();
if (cin.get() == '\n') {
system("cls");
trigger1 = false;
}
else
{
cout << "Please press the ENTER key\n";
}
}
goto label;
}
}
if (isalpha(guess)) // only executes if the input data for guess is a letter, i used isalpha as it makes my code more clear and readable compared to an if statement or switch statement
{
for (int x = 0; x < guessWord.length(); x++) {
if (guessWord[x] == guess)
{
for (int i = 0; i <= guessWord.length(); i++)
{
if (guessWord[i] == guess)
{
guesses[i] = guess; // A piece of code that prints out an underscore that represents each letter but now each letter the user guesses correct replaces the corresponding undersore in its place
}
}
for (int x = 0; x <= guessWord.length() && allow == true; x++)
{
cout << guesses[x] << " ";
}
allow = false;
test = true;
}
}
if (allow == false) {
cout << endl << endl << "Correct!" << endl << endl; // To let the user know that the letter they guessed is correct
cout << "You have " << lives << " lives remaining!\n" << endl; // Code shows how many lives the user has left every guess
}
static int counter = 0;
if (test == false)
{
for (int x = 0; x <= guessWord.length(); x++)
{
cout << guesses[x] << " ";
}
lives--;
lettersGuessed[counter] = guess;
counter++;
cout << "\n\nWrong!" << " You have " << lives << " lives remaining!" << endl; // Code shows how many lives the user has left after every guess
}
else
{
lettersGuessed[counter] = guess;
counter++;
}
if (lives < 1)
{
cout << "YOU LOSE! The word was '"; // I changed the output to make it more user freindly, it also makes more sense that printing out LOSE
for (int x = 0; x <= guessWord.length(); x++) // I made the program print out what the word was as if the user loses they won't know
{
cout << guessWord[x]; // This for loop prints out every element in the array next to each other
}
cout << "'" << endl;
cin.get();
running = false;
restart = false;
}
bool winFlag = true;
for (int count = 0; count < guessWord.length(); count++)
{
if (guesses[count] != guessWord[count])
{
winFlag = false;
}
}
if (winFlag == true)
{
cout << endl << endl << endl << "CONGRATULATIONS YOU WIN! The word was '"; // I changed what was printed to make it more user friendly.
for (int x = 0; x <= guessWord.length(); x++) // I made the program print out what the word was
{
cout << guessWord[x]; // This for loop prints out every element in the array next to each other
}
cout << "'" << endl;
cin.get();
running = false;
restart = false;
}
if (lives >= 1 && winFlag == false)
{
cout << "\nPlease press enter to type in your next guess!\n";
while (trigger1 == true) {
// I used this function as it only requires the user to input one button instead of two making it easier and quicker to use
cin.get();
if (cin.get() == '\n') {
system("cls");
trigger1 = false;
}
else
{
cout << "Please press the ENTER key\n";
}
}
}
}
else
{
if (lives >= 1 && winFlag == false) {
cout << "PLEASE ONLY ENTER A LETTER! EXAMPLE: h " << endl << "Please try again!" << endl; //Reminds the user of what they have to input
cout << "\nPlease press enter to type in your next guess!\n";
while (trigger1 == true) {
// I used this function as it only requires the user to input one button instead of two making it easier and quicker to use
cin.get();
if (cin.get() == '\n') {
system("cls");
trigger1 = false;
}
else
{
cout << "Please press the ENTER key\n";
}
}
}
}
label2:
if (iteration == 20) {
cout << "You have broken the program! Please type restart!" << endl;
while (trigger2 == true) {
cin >> restart1;
if (restart1 == "restart" || restart1 == "RESTART") {
running = false;
restart = true;
trigger2 = false;
system("cls");
}
else {
cout << "Please type the word restart!" << endl;
}
}
}
label:
iteration++;
}
}
return 1;
}
|