1 / 16

Implementation of the Hangman Game in C++

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

paki-irwin
Download Presentation

Implementation of the Hangman Game in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

  2. 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

  3. 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

  4. 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.

  5. 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); };

  6. Hangman Program Main Program Start Initialize Variables Get User Input Copy chars from array to vector Call hangman method

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Hangman Program HangmanClass -vectorFind Start Boolean= false Loop & Check if letter Is In elements of vector Boolean= true No Yes Return Boolean

  13. 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

  14. 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

  15. 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

  16. Questions?

More Related