CODE
Controller_H
#ifndef Controller_h
#define Controller_h
#include "Dictionary.h"
#include "Menu.h"
class Controller
{
public:
virtual void Start();
Menu myMenu;
Dictionary myDictionary;
};
#endif
_____________________________
Controller.cpp
#include <iostream>
#include "Controller.h"
using namespace std;
void Controller::Start()
{
while ((hmenu.getChoice() != 'Q') && (hmenu.getChoice() != 'q')) // Main loop
{
switch (hmenu.getChoice())
{
case 'A':
case 'a':
cout << "Please enter a new word: ";
cin >> word;
cout << endl << "The added word is: " << word << endl; // Just to see if it works
break;
case 'D':
case 'd':
cout << "Which word would you like to delete? ";
cin >> word;
cout << endl << "The deleted word is: " << word << endl; // Just to see if it works
break;
case 'L':
case 'l':
cout << endl << "List under construction... " << endl; // Just to see if it works
break;
case 'S':
case 's':
cout << "Which word are you looking for? " << endl; // Just to see if it works
cin >> word;
cout << endl << "Searching..." << endl << "Database under construction..." << endl;
break;
case 'P':
case 'p':
cout << "User selected play game!" << endl; // Just to see if it works
break;
default:
cout << "Invalid selection." << endl;
}// End switch
hman.displayMenu();
hmenu.displayChoice();
cin >> inChar;
hmenu.setChoice(inChar);
}// End while
}
___________________________
Dictionary_h
#ifndef Dictionary_h
#define Dictionary_h
#include <vector>
#include <string>
#include "Word.h"
using namespace std;
class Dictionary
{
public:
//C++ virtual function is a member function of a class.
//A virtual function is declared with virtual keyword, and it
//usually has a different functionality in the derived class.
//A function call is resolved at run-time (dynamic binding)
//Virtual functions are used to have a different functionality in the derived class.
virtual void addWord();
virtual void deleteWord();
virtual void findWord();
virtual void getWord();
string words;
Word myWord;
vector< Word > myWord;
};
#endif
___________________________
Dictionary.cpp
#include <iostream>
#include "Dictionary.h"
void Dictionary::addWord()
{
{
myGame.loadFile();
ofstream Datfile("words.txt");
try
{
cout << "\tEnter a new word, keep it below " << MAX_WORD_SIZE << " characters: ";
cin >> Word;
Size = strlen(Word);
if(Size == 0 || Size > MAX_WORD_SIZE)
throw Word;
strcpy_s(Words[Count++],MAX_WORD_SIZE,Word);
for (int i = 0; i < Count; ++i)
Datfile << Words[i] << endl;
}
catch (int word)
{
cout << "An error was found incorrect key entered\n";
}
}
}
void Dictionary::deleteWord()
{
//
}
void Dictionary::findWord()
{
//
}
void Dictionary::getWord(string skipWord, char hmWord[], int &letAmount)
{
ifstream fin; // A fstream is created and it's called "fin", like "cin".
int randNum; // local variable that will be used to randomize which word
// gets pick.
srand( GetTickCount() ); // seeds are randomizer
randNum = rand()%11; // assigns "randNum" to a random number 0 - 11
while( (randNum < 1) || (randNum > 10) ) // This for loop makes sure that "randNuM"
randNum = rand()%11; // stay's between 1 - 10
fin.open("words.txt"); // open's up "words.txt"
for(int i = 0; i < randNum; i++) // a for loop that reads through the words list
{ // and skips over the words that aren't in the location of
fin >> skipWord; // where are "randNum" stops at.
}
fin >> hmWord; // reads in the random word and stores in "hmWord[]"
fin.close(); // closes file
fin.clear(); // clears flags used
letAmount = strlen(hmWord);
}
___________________________
Game_H
#ifndef Game_h
#define Game_h
struct BODY // this is my structure that holds all the pieces of Hangman for when
{ // he get's drawed to the screen.
char head, head2, headCLT, headMT, headCRT, headCLB, headMB, headCRB,
neck,
shoulders, shouldersL, shouldersM, shouldersR,
arm1, arm2,
body,
hips, hipsL, hipsM, hipsR,
leg1, leg2;
}; // eof structure BODY
class Game
{
//Overload the stream insertion and extraction operators
friend ostream& operator<< (ostream&, const Game &);
friend istream& operator>> (istream&, Game &);
public:
virtual void play();
Game(); // constructor
Game(const Game&); // copy constructor
protected:
string skipWord, plyrName;
char *hmWord, guess, startLet;
int letAmount, guessAmount, correct,
score, letValue;
bool gameOver;
};
#endif
__________________________________
Game.cpp
#include "Game.h"
#include <iostream>
using namespace std;
ostream& operator<< (ostream& osObject, const Game& theGame)
{
osObject << "Player's name:" << theGame.plyrName; //Display player's name
return osObject;
}
istream& operator>> (istream& isObject, Game& theGame)
{
isObject >> theGame.plyrName; //Gets player's name
return isObject;
}
Game::Game()
{
letAmount = 0; // letAmount is amount of letters that are in our hangman word
guessAmount = 0; // guessAmount is the amount of guesses that the player as made wrong
correct = 0; // correct is the amount of correct guesses the player as made
score = 0; // score is what holds the total points scored by the player
letValue = 0; // letValue is what holds the value of the current letter guessed and how
// it effects the players score
startLet = 97; // startLet is just a "char" used to signify what ASCII code for the
// first letter in the alphabet is (lowercase) 97 = 'a' && 122 = 'z'
gameOver = false; // gameOver is our boolean which gets returned to the method "isGameOver" to
// see if our game is over or not. = ]
for(int abc = 0; abc < 27; abc++) // Loop to store lowercased alphabet
{ //alpha[] just holds the alphabet (size of array is 27)
alpha[abc] = startLet; // this asigns the first subscript of alpha to a lowercased 'a'
startLet++; // this increases startLet by one which will make it a lowercase 'b'
}
for(int letG = 0; letG < 27; letG++)
letGuessed[letG] = 32; // letGuessed holds all the letters that the player has guessed.
for(int hmlt = 0; hmlt < 7; hmlt++)
hmLetters[hmlt] = 32; // hmLetters holds all the letters that the player as guessed correctly
// that were part of the word
hmWord = new char[letAmount];
}
Game::Game(const Game &game)
{
letAmount = game.letAmount;
guessAmount = game.guessAmount;
correct = game.correct;
score = game.score;
letValue = game.letValue;
gameOver = game.gameOver;
for(int abc = 0; abc < 27; abc++)
alpha[abc] = game.alpha[abc];
for(int letG = 0; letG < 27; letG++)
letGuessed[letG] = game.letGuessed[letG];
for(int hmlt = 0; hmlt < letAmount; hmlt++)
hmLetters[hmlt] = game.hmLetters[hmlt];
for(int hm = 0; hm < letAmount; hm++)
hmWord[hm] = game.hmWord[hm];
}
void Game::play()
{
bool guessLet(char [], char &, int, int, int, bool &);
{
if(correct == letAmount) // checks to see if the player has made the correct
{ // amount of guesses and if the player as, then display
cout << endl; // a "Winner" message and end game!
cout << "\t\t\tCongratulations! You Saved Hangman! You Win!\n";
cout << endl;
gameOver = true;
return gameOver;
}
if(guessAmount == 9) // checks to see if the player has made to many incorrect
{ // guesses, and if the player as made 9 incorrect guesses then
cout << endl; // display a "Lost" message and end game!
cout << "\t\t\tGame Over! Better Luck Next Time!\n";
cout << endl;
gameOver = true;
return gameOver;
}
// Ask the user to guess a letter = ]
cout << endl << "\t\tGuess a letter that you think is in the word? ";
cin >> guess; // asigns "guess" to the letter the player guessed
guess = tolower(guess); // "tolower()" takes the value of that variable
// and makes it lower case.
return 0; // returns false
}
void checkGuess(char, char [], char [], char [], char [], int &, int &, int);
{
// If guess is correct:
for(int i = 0; i < letAmount; i++)
{
if(guess == hmWord[i])
{
correct++;
hmLetters[i] = hmWord[i];
for(int s = 0; s < 27; s++)
{
if(guess == alpha[s])
{
letValue = s + 1;
score += letValue;
letGuessed[s] = alpha[s];
} // eof if(guess == alpha[s])
} // eof for(int s = 0; s < 27; s++)
} // eof if(guess == hmWord[i])
} // eof for(int i = 0; i < letAmount; i++)
// If the word was incorrect:
if( (guess != hmWord[0]) && (guess != hmWord[1]) && (guess != hmWord[2]) && (guess != hmWord[3]) && (guess != hmWord[4]) && (guess != hmWord[5]) && (guess != hmWord[6]) )
{
guessAmount++;
for(int s = 0; s < 27; s++)
{
if(guess == alpha[s])
{
letValue = s + 1;
score -= letValue;
if(score < 0) score = 0;
letGuessed[s] = alpha[s];
} // eof if(guess == alpha[s])
} // eof for(int s = 0; s < 27; s++)
} // eof if( (guess != hmWord[0]) && (guess != hmWord[1]) && (guess != hmWord[2]) && (guess != hmWord[3]) && (guess != hmWord[4]) && (guess != hmWord[5]) && (guess != hmWord[6]) )
}
}
________________________________________
HangmanGame_H
#ifndef HangmanGame_h
#define HangmanGame_h
#include "Game.h"
class HangmanGame : public Game
{
public:
virtual guess();
virtual void showHangMan();
virtual void showScreen();
};
#endif
___________________________________
HangmanGame.cpp
#include "HangmanGame.h"
#include <iostream>
#include <string>
using namespace std;
BODY body; //Global instance of Hangman's body
HangmanGame::guess()
{
Game::play();
}
void HangmanGame::showHangMan(BODY &body, int guessAmount)
{
if(guessAmount == 0) // this is asigns all of hangman's body parts to equal space
{
body.head, body.head2, body.headCLT, body.headMT, body.headCRT,
body.headCLB, body.headMB, body.headCRB,
body.neck,
body.shoulders, body.shouldersL, body.shouldersM, body.shouldersR,
body.arm1, body.arm2,
body.body,
body.hips, body.hipsL, body.hipsM, body.hipsR,
body.leg1, body.leg2 = (char)32;
}
else if(guessAmount == 1) // when one miss guess is made this draws his head
{
body.head = (char)196;
body.head2 = (char)179;
body.headCLT = (char)218;
body.headMT = (char)193;
body.headCRT = (char)191;
body.headCLB = (char)192;
body.headMB = (char)196;
body.headCRB = (char)217;
}
else if(guessAmount == 2) // when another miss is made this draws in his neck
{
body.headMB = (char)194;
body.neck = (char)179;
}
else if(guessAmount == 3) // then next drawn in is his shoulders
{
body.shoulders = (char)196;
body.shouldersL = (char)196;
body.shouldersM = (char)193;
body.shouldersR = (char)196;
}
else if(guessAmount == 4) // after that is his left arm to be drawn in
{
body.shouldersL = (char)218;
body.arm1 = (char)179;
}
else if(guessAmount == 5) // next missed guess would then drawn in his right arm
{
body.shouldersR = (char)191;
body.arm2 = (char)179;
}
else if(guessAmount == 6) // after that, his body gets drawn in
{
body.shouldersM = (char)197;
body.body = (char)179;
}
else if(guessAmount == 7) // then hangman's hips are drawn in
{
body.hips = (char)196;
body.hipsL = (char)196;
body.hipsM = (char)193;
body.hipsR = (char)196;
}
else if(guessAmount == 8) // then his left leg is drawn in
{
body.hipsL = (char)218;
body.leg1 = (char)179;
}
else if(guessAmount == 9) // then last would be his right leg to be drawn in
{
body.hipsR = (char)191;
body.leg2 = (char)179;
}
}
void HangmanGame::showScreen(BODY body, string plyrName, char hmLetters[], char letGuessed[], int score, int letAmount)
{
system("CLS");
cout << endl << endl;
cout << "\t\t Hangman \n";
cout << "\t\t " << (char)218 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)191 << " \n";
cout << "\t\t " << (char)179 << " " << (char)179 << " \n";
cout << "\t\t " << (char)179 << " " << (char)179 << " \n";
cout << "\t\t " << (char)179 << " " << body.headCLT << body.head << body.headMT << body.head << body.headCRT << " Players Name: " << plyrName << "\n";
cout << "\t\t " << (char)179 << " " << body.head2 << " " << body.head2 << " Score: " << score << "\n";
cout << "\t\t " << (char)179 << " " << body.head2 << " " << body.head2 << " \n";
cout << "\t\t " << (char)179 << " " << body.headCLB << body.head << body.headMB << body.head << body.headCRB << "\n";
cout << "\t\t " << (char)179 << " " << body.neck << " " << hmLetters[0] << " " << hmLetters[1] << " " << hmLetters[2] << " " << hmLetters[3] << " " << hmLetters[4] << " " << hmLetters[5] << " " << hmLetters[6] << " \n";
if(letAmount == 7)
cout << "\t\t " << (char)179 << " " << body.shouldersL << body.shoulders << body.shoulders << body.shouldersM << body.shoulders << body.shoulders << body.shouldersR << " - - - - - - -\n";
else if(letAmount == 6)
cout << "\t\t " << (char)179 << " " << body.shouldersL << body.shoulders << body.shoulders << body.shouldersM << body.shoulders << body.shoulders << body.shouldersR << " - - - - - -\n";
else if(letAmount == 5)
cout << "\t\t " << (char)179 << " " << body.shouldersL << body.shoulders << body.shoulders << body.shouldersM << body.shoulders << body.shoulders << body.shouldersR << " - - - - -\n";
else if(letAmount == 4)
cout << "\t\t " << (char)179 << " " << body.shouldersL << body.shoulders << body.shoulders << body.shouldersM << body.shoulders << body.shoulders << body.shouldersR << " - - - -\n";
else if(letAmount == 3)
cout << "\t\t " << (char)179 << " " << body.shouldersL << body.shoulders << body.shoulders << body.shouldersM << body.shoulders << body.shoulders << body.shouldersR << " - - -\n";
cout << "\t\t " << (char)179 << " " << body.arm1 << " " << body.body << " " << body.arm2 << " \n";
cout << "\t\t " << (char)179 << " " << body.arm1 << " " << body.body << " " << body.arm2 << " Letters Guess:\n";
cout << "\t\t " << (char)179 << " " << body.body << " \n";
cout << "\t\t " << (char)179 << " " << body.hipsL << body.hips << body.hipsM << body.hips << body.hipsR << " " << letGuessed[0] << " " << letGuessed[1] << " " << letGuessed[2] << " " << letGuessed[3] << " " << letGuessed[4] << " " << letGuessed[5] << " " << letGuessed[6] << " " << letGuessed[7] << "\n";
cout << "\t\t " << (char)179 << " " << body.leg1 << " " << body.leg2 << " " << letGuessed[8] << " " << letGuessed[9] << " " << letGuessed[10] << " " << letGuessed[11] << " " << letGuessed[12] << " " << letGuessed[13] << " " << letGuessed[14] << " " << letGuessed[15] << "\n";
cout << "\t\t " << (char)179 << " " << body.leg1 << " " << body.leg2 << " " << letGuessed[16] << " " << letGuessed[17] << " " << letGuessed[18] << " " << letGuessed[19] << " " << letGuessed[20] << " " << letGuessed[21] << " " << letGuessed[22] << " " << letGuessed[23] << "\n";
cout << "\t\t " << (char)179 << " " << letGuessed[24] << " " << letGuessed[25] << " " << letGuessed[26] << "\n";
cout << "\t\t" << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)193 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << "\n";
cout << endl;
}
________________________________
HangmanMenu_H
#ifndef HangmanMenu_H
#define HangmanMenu_H
#include "Menu.h"
#include "Dictionary.h"
class Hangmanmenu : public Menu
{
private:
Dictionary dictionary;
public:
void displayMenu();
// Overrides the function displayMenu() of the base class
// Postcondition: First, the user is requested to press a key to continue.
// Then, the screen is cleared.
// Finally, it displays the menu of the game.
};
#endif
______________________________________
HangmanMeu.cpp
#include <iostream>
#include "HangmanMenu.h"
#include "Dictionary.h"
using namespace std;
void Hangmanmenu::displayMenu()
{
system("pause"); // Gives user some time to see the results
system("cls"); // Clear screen
Menu::displayMenu(); // Calls displayMenu function from base class
// Postcondition: See definition in Menu.h
}
_______________________________
MENU_H
#ifndef MENU_H
#define MENU_H
#include <iostream>
using namespace std;
class Menu
{
public:
void displayTitle();
// Function to output the name of the game
// Postcondition: Outputs
// Welcome to Hangman!
void displayHeader();
// Function to output the header of the game
// Postcondition: Outputs
// Please follow the instructions...
void displayMenu();
// Function to output the menu of the game
// Postcondition: Outputs
//*****************************************************************
//* M E N U *
//* Type 'q' to Quit *
//*****************************************************************
// (a)dd a word
// (d)elete a word
// (l)ist all word
// (s)earch for a word
// (p)lay game
//*****************************************************************
void displayChoice();
// Function to output a message to the user asking for a choice.
// Postcondition: Outputs
// Please enter your choice:
char getChoice();
// Accessor method (or getter)
void setChoice(char sel);
// Mutator method (or setter)
protected:
char choice; // Variable to store choice given by user
};
#endif // Matches the define above
___________________________________
Menu.cpp
#include <iostream>
#include "Menu.h"
using namespace std;
void Menu::displayTitle()
{
cout << endl << endl << "Welcome to hangman!";
}
void Menu::displayHeader()
{
cout << endl << "Please follow the instructions...";
}
void Menu::displayMenu()
{
cout << "*****************************************************************" << endl;
cout << "* M E N U *" << endl;
cout << "* Type 'q' to Quit *" << endl;
cout << "*****************************************************************" << endl;
cout << " (a)dd a word" << endl;
cout << " (d)elete a word" << endl;
cout << " (l)ist all word" << endl;
cout << " (s)earch for a word" << endl;
cout << " (p)lay game" << endl;
cout << "*****************************************************************" << endl;
}
void Menu::displayChoice()
{
cout << endl << "Please enter your choice: " << flush;
}
char Menu::getChoice() { return choice; }
void Menu::setChoice(char sel) { choice = sel; }
_______________________________
Word_H
#ifndef Word_h
#define Word_h
#include "Dictionary.h"
class Word
{
public:
Dictionary myDictionary;
};
#endif
________________________________
Main.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "Controller.h"
#include "Dictionary.h"
#include "Game.h"
#include "HangmanGame.h"
#include "HangmanMenu.h"
#include "Menu.h"
#include "Word.h"
using namespace std;
int main ()
{
Hangmanmenu hman = Hangmanmenu();
Menu hmenu = Menu();
Game game1("Alex");
Game game2; //Used to demonstrate overloading operator
string word;
char inChar;
hmenu.displayTitle(); // Call to display title
hmenu.displayMenu(); // Call to display menu
cout << "Today's player name: " << game1 << endl; //Overloading operator example
cout << "Enter a different name: ";
cin >> game2;
cout << endl;
cout << "Now the player is " << game1 << endl; // End of example
hmenu.displayChoice(); //Call to ask user for a choice
cin >> inChar;
hmenu.setChoice(inChar);
Controller myController;
myController.Start();
return 0;
}