Components of the Program
The following functions are used in the program to work for the different components of the game:
- HangmanGame(): This Constructor is used for initializing the Game. This constructor randomly picks any word from the list already written in the code and initializes the number of attempts as 6.
- play(): This function manages different things such as player guesses, information, the correct state of the game and displaying the hangman drawing.
- getRandomWord(): This function is used to pick the random word from the predefined list. This function uses the rand() function to get the random index.
- already guessed checks(): This function returns true and false if the player has already guessed the letter or not.
- drawHangman(): This function draws step-by-step the picture of a hangman by considering the number of attempts left. If the number of attempts is 6 no picture will be shown and if the number of attempts is 0. Full Hangman is drawn.
Hangman Game in C++
With friends, many of us had played the hangman game where one player gave a hint to the other to make a correct guess of a particular thing. Here a simple representation of the game ‘Hangman’ is done using C++ programming language. The player will guess the fruit’s name and the Computer will give a hint to the player to make the correct guess.