implementation of the hangman game in c++ by: khaled alnuaimi and bader alotaibi

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

Upload: lorena-grant

Post on 03-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

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

Implementation of Implementation of the Hangman Game the Hangman Game

in C++in C++By:By:

Khaled AlnuaimiKhaled AlnuaimiAndAnd

Bader AlotaibiBader Alotaibi

Page 2: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

What is Hangman?What is Hangman?

Hangman is a simple game whereHangman is a simple game where– User1 chooses a word to guessUser1 chooses a word to guess– User1 sets the number of wrong guessesUser1 sets the number of wrong guesses– User2 tries to guess the word by User2 tries to guess the word by

entering lettersentering letters– If User2 enters too many wrong letters If User2 enters too many wrong letters

then User2 has lost the gamethen User2 has lost the game– If User2 has entered all the right letters If User2 has entered all the right letters

then User2 has won the gamethen User2 has won the game

Page 3: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Objectives of this projectObjectives of this project

Working on the project taught us how Working on the project taught us how to implement the theory and to implement the theory and structures we had learned in class structures we had learned in class e.g Vectorse.g Vectors

Page 4: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Implementation of Implementation of HangmanHangman The implementation of the hangman The implementation of the hangman

game was made using Vectors.game was made using Vectors. In addition we made use of IOSTREAM In addition we made use of IOSTREAM

library in visual C++ to allow us to library in visual C++ to allow us to ask for user input and send messages ask for user input and send messages to the screen.to the screen.

The program is designed recursively The program is designed recursively and ends when a user has won or lost.and ends when a user has won or lost.

Page 5: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program Header FileHeader File

#include <vector>#include <vector>using namespace std;using namespace std;

class HangmanClassclass HangmanClass{{public:public:

HangmanClass(void);HangmanClass(void);~HangmanClass(void);~HangmanClass(void);void hangman(vector<char> &wordToGuess,vector<char> void hangman(vector<char> &wordToGuess,vector<char>

&guessedRight,vector<char> &guessedWrong,int &guessedRight,vector<char> &guessedWrong,int numGuesses);numGuesses);

private:private:int numEnteredGuesses;int numEnteredGuesses;bool vectorFind(vector<char> &wordVector,char findValue);bool vectorFind(vector<char> &wordVector,char findValue);bool wonGame(vector<char> &wordVector,vector<char> bool wonGame(vector<char> &wordVector,vector<char>

&guessedRight);&guessedRight);};};

Page 6: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program Main Main

ProgramProgram Start

Initialize Variables

Get User Input

Copy chars from array to vector

Call hangman method

Page 7: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program HangmanClass - hangmanHangmanClass - hangman

hangman Methodhangman Method

Param1: Vector TypeParam1: Vector Type

Param2: Vector TypeParam2: Vector Type

Param3: Vector TypeParam3: Vector Type

Param4: int TypeParam4: int Type

Start

Display WON!!Check if user has won

Yes

NoFirst we check if the user has won the game.

Page 8: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program HangmanClass - hangmanHangmanClass - hangman

Display LOST!!Check if user has lost

Yes

No

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

Page 9: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program HangmanClass - hangmanHangmanClass - hangman

User Enters Guess

Add letter guessed to Right Guesses Vector

Display correctly guessed letters

Check if guess is in word

YesNo

User Entered Correct Guess

Page 10: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program HangmanClass - hangmanHangmanClass - hangman

User Entered Incorrect Guess

Display correctly guessed letters along

with incorrectly guessedletters

Add letter guessed to Wrong Guesses Vector

Call hangman method Recursively

No

Page 11: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program HangmanClass -vectorFindHangmanClass -vectorFind

vectorFind MethodvectorFind Method

Param1: Vector TypeParam1: Vector Type

Param2: Char TypeParam2: Char Type

Loops through the vector until Loops through the vector until the stated character is found the stated character is found and returns a true or falseand returns a true or false

Used by the Hangman method to Used by the Hangman method to check if a character entered check if a character entered by user is in the word that the by user is in the word that the user is to guessuser is to guess

//method that loops through the vector //method that loops through the vector that stores the word and that stores the word and

//returns true if the entered letter is found //returns true if the entered letter is found in the vectorin the vector

//if it is no found the method returns false//if it is no found the method returns falsebool bool

HangmanClass::vectorFind(vector<chaHangmanClass::vectorFind(vector<char> &wordVector,char findValue)r> &wordVector,char findValue)

{{//check to see if the specified //check to see if the specified character is //present in vectorcharacter is //present in vectorvector<char>::iterator findIter;vector<char>::iterator findIter;bool found = false;bool found = false;

for(findIter=wordVector.begin();findItefor(findIter=wordVector.begin();findIter !=wordVector.end();findIter++)r !=wordVector.end();findIter++){{

if(*findIter==findValue)if(*findIter==findValue)found=true;found=true;

}}return found;return found;

}}

Page 12: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program HangmanClass -vectorFindHangmanClass -vectorFind

Start

Boolean= trueLoop & Check if letter

Is In elements ofvector

YesBoolean= false

No

Return Boolean

Page 13: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program HangmanClass - wonGameHangmanClass - wonGame

wonGame MethodwonGame MethodParam1: Vector TypeParam1: Vector TypeParam2: Vector TypeParam2: Vector Type

Checks to see if the contents of Checks to see if the contents of one vector are contained in one vector are contained in the other vector. If the the other vector. If the contents of both vectors are contents of both vectors are equal then the method returns equal then the method returns true otherwise it returns falsetrue otherwise it returns false

Used in Hangman method to Used in Hangman method to determine if the correct letters determine if the correct letters the user enters are contained the user enters are contained in the vector containing the in the vector containing the wordword

//check to see if the user has won the game.//check to see if the user has won the game.// to win the game every element in the// to win the game every element in the//guessedRight vector must be//guessedRight vector must be//in the vector storing all the letters of the //in the vector storing all the letters of the

actual actual //word//wordbool bool

HangmanClass::wonGame(vector<char> HangmanClass::wonGame(vector<char> &wordVector, vector<char> &wordVector, vector<char> &guessedRight)&guessedRight)

{{//check to see if each element in the //check to see if each element in the word has been guessedword has been guessedvector<char>::iterator findIter;vector<char>::iterator findIter;for(findIter=wordVector.begin();findIter !for(findIter=wordVector.begin();findIter !=wordVector.end();findIter++)=wordVector.end();findIter++){{

if(!if(!vectorFind(guessedRight,*findIter))vectorFind(guessedRight,*findIter))

return false;return false;}}return true;return true;

}}

Page 14: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Hangman Program Hangman Program HangmanClass - wonGameHangmanClass - wonGame

Start

Boolean= true

Call VectorFind Method for each

element of vector2

If all elements of Vector 2 == Vector 1

Begin for loop until end of vector1

No

Return Boolean

yes

Boolean= false

Page 15: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Uses for such a programUses for such a program

A program like hangman is good for A program like hangman is good for teaching kids how to spell and also to teaching kids how to spell and also to help them with their vocabulary.help them with their vocabulary.

In order to do this the program could In order to do this the program could be made to read a random word from be made to read a random word from a dictionary file and allow the user to a dictionary file and allow the user to guess what the word is and also guess what the word is and also display the meaning of the worddisplay the meaning of the word

Page 16: Implementation of the Hangman Game in C++ By: Khaled Alnuaimi And Bader Alotaibi

Questions?Questions?