0% found this document useful (0 votes)
8 views2 pages

Hangman Report

The document describes a hangman game program with 9 levels. It initializes variables like level, arrays to store the words and guesses, and chance (number of incorrect guesses allowed). Each level stores a word in the arr1 array. A while loop gets the user's guess and uses linear search to check if it's in the word. If correct, it replaces dashes in the arr display. If incorrect, it deducts a chance and stores the wrong guess. The level completes if the word is guessed or chances reach 0, then asks to play again.

Uploaded by

ammarwaheed2244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Hangman Report

The document describes a hangman game program with 9 levels. It initializes variables like level, arrays to store the words and guesses, and chance (number of incorrect guesses allowed). Each level stores a word in the arr1 array. A while loop gets the user's guess and uses linear search to check if it's in the word. If correct, it replaces dashes in the arr display. If incorrect, it deducts a chance and stores the wrong guess. The level completes if the word is guessed or chances reach 0, then asks to play again.

Uploaded by

ammarwaheed2244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Hangman Game

At first, when the program starts it shows a welcome screen which is just printed using cout
and then it shows that here are total 9 levels and the level screen is printed using loop and
cout.

After that, here is 1 variable named level is initialized with 1 and three arrays initialized
named arr, arr1, wrong. Here, arr1 is used to store the word, which the player will have to
guess. No word is stored in arr1 at start. Here are 9 if else if conditions (for 9 levels) are
created and in each condition one more array named word is initialized and the word to be
guessed in that array. In each if condition (each level) the stored word from array word is
then stored in arr1 (after each level the word in arr1 is replaced). Also the number of dashes
equal to size of the word is stored in arr. At first level is equal to 1, so it will store the word
in arr1 in first if condition.

After that a char variable ch is initialized (in which user will input character to guess the
word). After that a bool condition cond is initialized to check that user entered right character
(of the word to be guessed). Another integer variable named chance is initialized with 6 (and
it gets decremented every time player enters wrong character till chance reaches equal to 0).

After that the length of the stored word is calculated and stored in an integer variable named
q.

After that level 1 gets started it prints dashes (in arr) and prints the structure where the man
will be hanged (in case of wrong guesses) using cout.

Correct guess case:

After that a while loop is started with condition as always true (1) and in that loop an integer
variable named terminator is initialized with 0. After that a while loop is used to check if the
entered character by player matches any index of the stored word array (arr1) (basically
linear search). If it finds the same character in any index of arr1, then it stores that character
at the same index in arr (character replaces dash) and arr is printed after every iteration. In
the linear search loop terminator is incremented (after each successful guess), and when
terminator becomes equal to the length of the word need to be guessed (q), means the player
has guessed the word and it increments level and it breaks the loop, after that it asks player
that if he/she wants to play another level or not. If user enters ‘Y’, then it starts the loop again
and next level gets started.
Wrong guess case:

If user enters character in ch which is not in the word, then it decrements chance (chance is
equal to 6 at start) and stores the incorrect word in another array named wrong and it prints
incorrect guesses (shows user that it’s incorrect and not to be used again). After each incorrect
guess, a part of man to be hanged is printed using cout. When chance becomes equal to 0
(means user ran out of chances) and loop gets break and prints “You ran out of chances”.
After that it asks player that if he/she want to play that level again or not (Y/N). If user enters
Y then it starts that level again as level only gets incremented when player guesses the correct
word i.e. terminator becomes equal to q.

Note:

Here the reason to take input after checking all conditions is that because if we take input first
and check conditions later then it will take an extra input before terminating the program.

You might also like