170 likes | 261 Views
Implementation of the Hangman Game in C++. By: Khaled Alnuaimi And Bader Alotaibi. What is Hangman?. Hangman is a simple game where User1 chooses a word to guess User1 sets the number of wrong guesses User2 tries to guess the word by entering letters
E N D
Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi
What is Hangman? • Hangman is a simple game where • User1 chooses a word to guess • User1 sets the number of wrong guesses • User2 tries to guess the word by entering letters • If User2 enters too many wrong letters then User2 has lost the game • If User2 has entered all the right letters then User2 has won the game
Objectives of this project • Working on the project taught us how to implement the theory and structures we had learned in class e.g Vectors
Implementation of Hangman • The implementation of the hangman game was made using Vectors. • In addition we made use of IOSTREAM library in visual C++ to allow us to ask for user input and send messages to the screen. • The program is designed recursively and ends when a user has won or lost.
Hangman Program Header File #include <vector> using namespace std; class HangmanClass { public: HangmanClass(void); ~HangmanClass(void); void hangman(vector<char> &wordToGuess,vector<char> &guessedRight,vector<char> &guessedWrong,int numGuesses); private: int numEnteredGuesses; bool vectorFind(vector<char> &wordVector,char findValue); bool wonGame(vector<char> &wordVector,vector<char> &guessedRight); };
Hangman Program Main Program Start Initialize Variables Get User Input Copy chars from array to vector Call hangman method
Hangman Program HangmanClass - hangman • hangman Method Param1: Vector Type Param2: Vector Type Param3: Vector Type Param4: int Type Start Check if user has won Display WON!! Yes First we check if the user has won the game. No
Hangman Program HangmanClass - hangman Check if user has lost Display LOST!! Yes If the user has not won the game then we check if the user has lost the game and has reached the maximum number of guesses No
Hangman Program HangmanClass - hangman User Entered Correct Guess User Enters Guess Yes No Check if guess is in word Add letter guessed to Right Guesses Vector Display correctly guessed letters
Hangman Program HangmanClass - hangman User Entered Incorrect Guess No Add letter guessed to Wrong Guesses Vector Display correctly guessed letters along with incorrectly guessed letters Call hangman method Recursively
vectorFind Method Param1: Vector Type Param2: Char Type Loops through the vector until the stated character is found and returns a true or false Used by the Hangman method to check if a character entered by user is in the word that the user is to guess //method that loops through the vector that stores the word and //returns true if the entered letter is found in the vector //if it is no found the method returns false bool HangmanClass::vectorFind(vector<char> &wordVector,char findValue) { //check to see if the specified character is //present in vector vector<char>::iterator findIter; bool found = false; for(findIter=wordVector.begin();findIter !=wordVector.end();findIter++) { if(*findIter==findValue) found=true; } return found; } Hangman Program HangmanClass -vectorFind
Hangman Program HangmanClass -vectorFind Start Boolean= false Loop & Check if letter Is In elements of vector Boolean= true No Yes Return Boolean
wonGame Method Param1: Vector Type Param2: Vector Type Checks to see if the contents of one vector are contained in the other vector. If the contents of both vectors are equal then the method returns true otherwise it returns false Used in Hangman method to determine if the correct letters the user enters are contained in the vector containing the word //check to see if the user has won the game. // to win the game every element in the //guessedRight vector must be //in the vector storing all the letters of the actual //word bool HangmanClass::wonGame(vector<char> &wordVector, vector<char> &guessedRight) { //check to see if each element in the word has been guessed vector<char>::iterator findIter; for(findIter=wordVector.begin();findIter !=wordVector.end();findIter++) { if(!vectorFind(guessedRight,*findIter)) return false; } return true; } Hangman Program HangmanClass - wonGame
Hangman Program HangmanClass - wonGame Start Begin for loop until end of vector1 Call VectorFind Method for each element of vector2 yes Boolean= false No If all elements of Vector 2 == Vector 1 Boolean= true Return Boolean
Uses for such a program • A program like hangman is good for teaching kids how to spell and also to help them with their vocabulary. • In order to do this the program could be made to read a random word from a dictionary file and allow the user to guess what the word is and also display the meaning of the word