Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in PYTHON that will let the user play Hangman as shown in the ex

ID: 3678430 • Letter: W

Question

Write a program in PYTHON that will let the user play Hangman as shown in the examples below.

The user will guess letters until either:

They have correctly guessed all the letters in the word.

They have incorrectly guessed 6 times.

At each turn, display the player’s progress by showing the word they’re guessing in all capital letters, with all non-guessed characters replaced by dashes (“-”).

Suggested Approach

Here’s one possible way to approach this program:

Prompt the user to enter a word

Create a display string with the right number of hyphens (loop and add one for each letter in the target word).

For each letter they guess,

If the letter is not in the target word, count a miss

Otherwise, make a new display string byloop through the target word’s letters

if the letter matches their guess, add it to the new display string

if the letter does not match their guess, copy whatever was in that spot of the old display string over

replace the old display string with the new one

If they have too many misses, tell them they lose; if there are no - left in the display string, tell them they win.

The specific output format is shown in the examples below:

Enter a word: HANGMAN
[-------] You have 6 left, enter a letter: A
Correct!
[-A---A-] You have 6 left, enter a letter: B
Incorrect!
[-A---A-] You have 5 left, enter a letter: C
Incorrect!
[-A---A-] You have 4 left, enter a letter: D
Incorrect!
[-A---A-] You have 3 left, enter a letter: E
Incorrect!
[-A---A-] You have 2 left, enter a letter: F
Incorrect!
[-A---A-] You have 1 left, enter a letter: G
Correct!
[-A-G-A-] You have 1 left, enter a letter: K
You lose! The word was “HANGMAN”

Enter a word: COMPUTER
[--------] You have 6 left, enter a letter: E
Correct!
[------E-] You have 6 left, enter a letter: B
Incorrect!
[------E-] You have 5 left, enter a letter: C
Correct!
[C-----E-] You have 5 left, enter a letter: A
Incorrect!
[C-----E-] You have 4 left, enter a letter: P
Correct!
[C--P--E-] You have 4 left, enter a letter: E
Correct!
[C--P--E-] You have 4 left, enter a letter: R
Correct!
[C--P--ER] You have 4 left, enter a letter: M
Correct!
[C-MP--ER] You have 4 left, enter a letter: A
Incorrect!
[C-MP--ER] You have 3 left, enter a letter: T
Correct!
[C-MP-TER] You have 3 left, enter a letter: O
Correct!
[COMP-TER] You have 3 left, enter a letter: U
You win! The word was “COMPUTER”

Enter a word: Aa
[--] You have 6 left, enter a letter: a
You win! The word was “AA”

Notes

We will only grade you on words and guesses made up of letters in the alphabet.

Convert both the word and the guesses to upper case; word Aa and guess a should say You win! The word was "AA".

If the user guesses a letter twice, handle it the same as the first time. If it initially matched a letter in the string, it should still count as a match and not as a miss. If it was initially a miss, it will count as another miss. This is shown in the examples above.

You may assume that the user only types single-letter guesses.

plz email the program to hx7xd@outlook.com

Explanation / Answer

Hangman.cpp : Defines the entry point for the console application.

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <string>

#include <stdlib.h>

#include <cmath>

#include <conio.h>

#include <time.h>

using namespace std;

//Ramon Herrera

//Hangman Game

string LoadFile() //To load the file

{

      

       string W; //Declare Varibles

       string words[20];

       int count = 0;

      

       ifstream txtfile; //the in file

      

       txtfile.open("words.txt"); //opens file

       for (count=0; !txtfile.eof(); count++)

       {

              getline(txtfile,W);

              words[count] = W;

       }

       int k=(rand()%count); //Picks a random word from file.

       txtfile.close(); //closes file

      

       return words[k];

}     

void RunGame(string word) //Runs the game

{

       int k;

       int wrong = 0;

       char guess;  

       string letters;

       char x;

       string incorrect;

       string correct;

      

       for (k=0; k<word.length();k++) //Blanks for each letter of your word

       {

              letters+= '_';

       }

    int count = 0;

       for (x=0; x<15; x++) //15 Guesses

       {

                    cout << "Make a Guess" << endl;

                    cin >> guess;

                   

                    bool found = false;

            for (k=0; k<word.length();k++) //Compare you guess to the word

            {

                   if (guess == word[k])

                   {

                          cout << "Good Guess!" << endl;

                           letters[k] = guess;

                          found = true;

                           count++;     

                   }

            }

            if(found)

                correct += guess;

            else

               incorrect += guess;

            if(count == word.length())

            {

                cout << "YOU HAVE WON!" << endl;

                break;

            }

      

                    if (!found) //You lose when you get 6 wrong letters

                    {

                           cout << "Wrong Letter!" << endl;

                          

                           wrong++;

                                  if (wrong==6)

                                  {

                                         cout << "You are Dead! GameOver!" << endl;

                                         cout << "Your word was " << word << endl;

                                         cout << endl;

                                         break;

                                  }

                    }

                   

                    cout << "Word:" << letters << endl;

                    cout << "Correct:" << correct << endl;

                    cout << "Incorrect:" << incorrect << endl;

                    cout << endl;

                    cout << endl;

                   

       }

             

}

If ( (char) tolower(guess) == (char) tolower (word[k]) )

{

Cout<< “ Good guess!” <<endl;

Letters [k]=guess;

Found=true;

Count ++;

}

int _tmain(int argc, _TCHAR* argv[]) //main

{

       cout << "Let's Play HangMan!" << endl;

      

       srand(time(NULL));

       char playagain = 'Y';

       while (playagain == 'Y')

       {

              string word = LoadFile();

              RunGame(word);

              cout << endl;

              cout << "Play Again? (Y/N)" << endl;

              cin >> playagain;

              playagain = toupper(playagain);

       }

      

       cout << "Thank You for Playing" << endl;

      

      

       _getch();

       return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote