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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
I need some help. I am working on creating a game with three games in them.
I have most/all the code in the CPP file.
However, I would like to move some of the code from the CPP file into Header files,
to make it look nicer.
Below are the files used in the game. Only Hangman has code, so far,
so that I can do the same for the other games later.
Any help would be greatly appreciated! Thanks!
Game.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <chrono>
#include <thread>
#include <stdio.h>
#include "BlackJack.h"
#include "Hangman.h"
#include "HangmanEasy.h"
#include "HangmanHard.h"
#include "Snake.h"
using namespace std;
void ClearScreen()
{
int n;
for (n = 0; n < 10; n++)
printf("\n\n\n\n\n\n\n\n\n\n");
}
int main()
{
bool MenuRepeat = true;
int UserSelection;
do
{
cout << endl;
cout << "Welcome! Please pick one of the following options.
\n1. Play BlackJack \n2. Play Hangman \n3. Play Snake \n4. Quit \nOption: ";
cin >> UserSelection;
cout << endl;
switch (UserSelection)
{
case 1:
{
blackjack bl;
bl.BlackJack();
break;
}
case 2:
{
hangman hm;
hm.Hangman();
break;
}
case 3:
{
snake sk;
sk.Snake();
break;
}
case 4:
{
MenuRepeat = false;
break;
}
default:
{
cout << "Incorrect Option. Please either play one of the games or Quit." << endl;
break;
}
}
} while (MenuRepeat);
return 0;
}
void blackjack::BlackJack()
{
cout << "Welcome to BlackJack!";
/*
Code goes here.
*/
cout << "Returning to Menu...";
int frameWait = 5000;
std::this_thread::sleep_for(std::chrono::milliseconds(frameWait));
ClearScreen();
}
void hangman::Hangman()
{
{
bool HangmanMenuRepeat = true;
int HangmanDifficultySetting;
do
{
cout << endl;
cout << "Welcome to Hangman! Please Select A Difficulty Setting
or Quit the Game.\n(1 for Easy | 2 for Hard | 3 to Return to Main Menu)\n";
cin >> HangmanDifficultySetting;
cout << endl;
switch (HangmanDifficultySetting)
{
case 1:
{
hangmaneasy es;
es.HangmanEasy();
break;
}
case 2:
{
hangmanhard hr;
hr.HangmanHard();
break;
}
case 3:
{
HangmanMenuRepeat = false;
break;
}
default:
{
cout << "Invalid Option." << endl;
break;
}
}
} while (HangmanMenuRepeat);
return;
}
}
void hangmaneasy::HangmanEasy()
{
const int MAX_WRONG_EASY = 8;
vector<string> easywords;
easywords.push_back("ALGORITHM");
easywords.push_back("POINTER");
easywords.push_back("COMPILER");
easywords.push_back("SYNTAX");
easywords.push_back("VARIABLE");
easywords.push_back("FUNCTION");
easywords.push_back("PUBLIC");
easywords.push_back("PRIVATE");
easywords.push_back("PROTECTED");
easywords.push_back("HEADER");
srand(static_cast<unsigned int>(time(0)));
random_shuffle(easywords.begin(), easywords.end());
const string THE_EASY_WORD = easywords[0];
int easywrong = 0;
string EasySoFar(THE_EASY_WORD.size(), '-');
string EasyUsed = " ";
cout << "Welcome to Beginner Hangman!\n\nBefore we begin, all letters entered
MUST be in capitals, other than that...\nGood Luck and have fun!";
while ((easywrong < MAX_WRONG_EASY) && (EasySoFar != THE_EASY_WORD))
{
cout << "\n\nSo far, the word is:\n\n" << EasySoFar << endl;
cout << "\nYou've used the following letters:\n" << EasyUsed << endl;
cout << "You have " << (MAX_WRONG_EASY - easywrong) << " incorrect guesses left.\n";
char EasyGuess;
cout << "\nEnter your guess: ";
cin >> EasyGuess;
EasyGuess = toupper(EasyGuess);
while (EasyUsed.find(EasyGuess) != string::npos)
{
cout << "\nYou've already guessed " << EasyGuess << endl;
cout << "Enter your guess: ";
cin >> EasyGuess;
EasyGuess = toupper(EasyGuess);
}
EasyUsed += EasyGuess;
if (THE_EASY_WORD.find(EasyGuess) != string::npos)
{
cout << "\nThat's Correct! " << EasyGuess << " is in the word!";
for (int i = 0; i < THE_EASY_WORD.length(); i++)
{
if (THE_EASY_WORD[i] == EasyGuess)
{
EasySoFar[i] = EasyGuess;
}
}
}
else
{
cout << "\nSorry, " << EasyGuess << " isn't in the word.";
++easywrong;
}
}
if (easywrong == MAX_WRONG_EASY)
{
cout << "\n\nSorry! You've been hanged!\n";
}
else
{
cout << "\nGood job! You've guessed it!\n";
}
cout << "\nThe word was: " << THE_EASY_WORD << endl;
cout << "Returning to Menu...";
int frameWait = 5000;
std::this_thread::sleep_for(std::chrono::milliseconds(frameWait));
ClearScreen();
return;
}
void hangmanhard::HangmanHard()
{
const int MAX_WRONG_HARD = 5;
vector<string> hardwords;
hardwords.push_back("DHTML");
hardwords.push_back("INHERITANCE");
hardwords.push_back("POLYMORPHISM");
hardwords.push_back("PSEUDOCODE");
hardwords.push_back("XHTML");
hardwords.push_back("METALANGUAGE");
hardwords.push_back("ENCAPSULATION");
hardwords.push_back("DECLARATION");
hardwords.push_back("BACKPROPAGATION");
hardwords.push_back("ARITHMETIC");
srand(static_cast<unsigned int>(time(0)));
random_shuffle(hardwords.begin(), hardwords.end());
const string THE_HARD_WORD = hardwords[0];
int hardwrong = 0;
string HardSoFar(THE_HARD_WORD.size(), '-');
string HardUsed = " ";
cout << "Welcome to Expert Hangman!\n\nBefore we begin, all letters entered
MUST be in capitals, other than that...\nGood Luck and have fun!";
while ((hardwrong < MAX_WRONG_HARD) && (HardSoFar != THE_HARD_WORD))
{
cout << "\n\nSo far, the word is:\n\n" << HardSoFar << endl;
cout << "\nYou've used the following letters:\n" << HardUsed << endl;
cout << "You have " << (MAX_WRONG_HARD - hardwrong) << " incorrect guesses left.\n";
char HardGuess;
cout << "\nEnter your guess: ";
cin >> HardGuess;
HardGuess = toupper(HardGuess);
while (HardUsed.find(HardGuess) != string::npos)
{
cout << "\nYou've already guessed " << HardGuess << endl;
cout << "Enter your guess: ";
cin >> HardGuess;
HardGuess = toupper(HardGuess);
}
HardUsed += HardGuess;
if (THE_HARD_WORD.find(HardGuess) != string::npos)
{
cout << "\nThat's Correct! " << HardGuess << " is in the word!";
for (int i = 0; i < THE_HARD_WORD.length(); i++)
{
if (THE_HARD_WORD[i] == HardGuess)
{
HardSoFar[i] = HardGuess;
}
}
}
else
{
cout << "\nSorry, " << HardGuess << " isn't in the word.";
++hardwrong;
}
}
if (hardwrong == MAX_WRONG_HARD)
{
cout << "\n\nSorry! You've been hanged!\n";
}
else
{
cout << "\nGood job! You've guessed it!\n";
}
cout << "\nThe word was: " << THE_HARD_WORD << endl;
cout << "Returning to Menu...";
int frameWait = 5000;
std::this_thread::sleep_for(std::chrono::milliseconds(frameWait));
ClearScreen();
return;
}
void snake::Snake()
{
cout << "Welcome to Snake!";
/*
Code goes here.
*/
cout << "Returning to Menu...";
int frameWait = 5000;
std::this_thread::sleep_for(std::chrono::milliseconds(frameWait));
ClearScreen();
}
Hangman.h
#ifndef _HANGMAN_H_
#define _HANGMAN_H_
class hangman
{
public:
void Hangman();
};
#endif
HangmanEasy.h
#ifndef _HANGMANEASY_H_
#define _HANGMANEASY_H_
class hangmaneasy
{
public:
void HangmanEasy();
};
#endif
HangmanHard.h
#ifndef _HANGMANHARD_H_
#define _HANGMANHARD_H_
class hangmanhard
{
public:
void HangmanHard();
};
#endif
BlackJack.h
#ifndef _BLACKJACK_H_
#define _BLACKJACK_H_
class blackjack
{
public:
void BlackJack();
};
#endif
Snake.h
#ifndef _SNAKE_H_
#define _SNAKE_H_
class snake
{
public:
void Snake();
};
#endif
|