0% found this document useful (0 votes)
49 views8 pages

CP Project

This document presents the code for a Hangman game. It includes functions to greet the player, display the hangman as misses occur, display the status of incorrect guesses and revealed letters, and end the game based on whether the player won or lost. The main function runs the game logic, taking player guesses, updating the hangman drawing and status displays until the word is guessed or the player reaches 6 misses.

Uploaded by

Arham zahid
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)
49 views8 pages

CP Project

This document presents the code for a Hangman game. It includes functions to greet the player, display the hangman as misses occur, display the status of incorrect guesses and revealed letters, and end the game based on whether the player won or lost. The main function runs the game logic, taking player guesses, updating the hangman drawing and status displays until the word is guessed or the player reaches 6 misses.

Uploaded by

Arham zahid
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/ 8

COMPUTER PRORAMMING

PROJECT DELIVERABLE-2
PRESENTED BY:
Husnain Mirza 01-135222-060
Haris Nayyab 01-135222-006
Ali Qureshi 01-135222-012
HangMan Game Code:
Code:
#include <iostream>

#include <vector>

using namespace std;

//define functions

void greet()

cout << "=====================\n";

cout << "Hangman: The Game\n";

cout << "=====================\n";

cout << "Instructions: Save your friend from being hanged by guessing the letters in the
codeword.\n";
}

void display_misses(int misses)

if (misses == 0)

cout << " +---+ \n";

cout << " | | \n";

cout << " | \n";

cout << " | \n";

cout << " | \n";

cout << " | \n";

cout << " ========= \n";

else if (misses == 1)

cout << " +---+ \n";

cout << " | | \n";

cout << " O | \n";

cout << " | \n";

cout << " | \n";

cout << " | \n";

cout << " ========= \n";

else if (misses == 2)

cout << " +---+ \n";

cout << " | | \n";

cout << " O | \n";

cout << " | | \n";

cout << " | \n";


cout << " | \n";

cout << " ========= \n";

else if (misses == 3)

cout << " +---+ \n";

cout << " | | \n";

cout << " O | \n";

cout << " /| | \n";

cout << " | \n";

cout << " | \n";

cout << " ========= \n";

else if (misses == 4)

cout << " +---+ \n";

cout << " | | \n";

cout << " O | \n";

cout << " /|\\ | \n";

cout << " | \n";

cout << " | \n";

cout << " ========= \n";

else if (misses == 5)

cout << " +---+ \n";

cout << " | | \n";

cout << " O | \n";

cout << " /|\\ | \n";

cout << " / | \n";

cout << " | \n";


cout << " ========= \n";

else if (misses == 6)

cout << " +---+ \n";

cout << " | | \n";

cout << " O | \n";

cout << " /|\\ | \n";

cout << " / \\ | \n";

cout << " | \n";

cout << " ========= \n";

void display_status(vector<char> incorrect, string answer)

cout << "Incorrect Guesses: \n";

for (int i = 0; i < incorrect.size(); i++)

cout << incorrect[i] << " ";

cout << "\nCodeword:\n";

for (int i = 0; i < answer.length(); i++)

cout << answer[i] << " ";

}
}

void end_game(string answer, string codeword)

if (answer == codeword)

cout << "Hooray! You saved the person from being hanged and earned a medal of honor!\n";

cout << "Congratulations!\n";

else

cout << "On no! The man is hanged!\n";

int main()

greet();

string codeword = "codingcleverly";

string answer = " ";

int misses = 0;

vector<char> incorrect;

bool guess = false;

char letter;

while (answer != codeword && misses < 7)

display_misses(misses);

display_status(incorrect, answer);
cout << "\n\nPlease enter your guess: ";

cin >> letter;

for (int i = 0; i < codeword.length(); i++)

if (letter == codeword[i])

answer[i] = letter;

guess = true;

if (guess)

cout << "\nCorrect!\n";

else

cout << "\nIncorrect! Another portion of the person has been drawn.\n";

incorrect.push_back(letter);

misses++;

guess = false;

end_game(answer, codeword);

return 0;

}
OutPut:

You might also like