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

Learning objectives: The intent of this programing project is to allow you the o

ID: 3835940 • Letter: L

Question

Learning objectives: The intent of this programing project is to allow you the opportunity to demonstrate your ability to solve problems using procedural C++ programming. This project HANG MAN will focus on file I/O and string manipulation in the implementation of the game Hangman. Program Description: In this project, you will write a C++ program that simulates playing the game Hangman. This version of the game will be limited to words with up to eight letters. The program should operate as follows: 1. The program must use a function that displays the hangman's gallows (see below). This function accepts an integer for the total number of incorrect guesses. This number will range from zero, representing the start of the game, to eight representing the end of the game. L L L L L L L L L Game Two Three Four Five Six Seven Game Start Incorrect Incorrect Incorrect Incorrect Incorrect Incorrect Incorrect Over Guess Guesses Guesses Guesses Guesses Guesses Guesses 2. Each time a game is played, the program will randomly select an unknown word from a file called words txt. Each of the thirty words in the file will be... Limited to a maximum of eight letters in length. No letter will ever appear more than once in any word. All words will be in lower case. 3. The program should then display the hangman's gallows, and a string of asterisk representing the unknown word, and a prompt for the letter to be guest.

Explanation / Answer

Save as name:hangman.cpp


#include "stdafx.h" //this file will reduce compile time

#include <iostream>

#include <fstream>

#include <string>

#include <stdlib.h>

#include <cmath>

#include <conio.h>

#include <time.h>

using namespace std;

string LoadFile() //To load the file

{

              

               string W; //Declare Varibles

               string words[25];//array of 25 words

               int count = 0;

              

               ifstream txtfile; //file

              

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

//loop will continue until the end of 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;

               loop will continue until the length of the word

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

               {

                               letters+= '_';

               }

    int count = 0;

               for (x=0; x<20; x++) //20 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 7 wrong letters

                                              {

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

                                                            

                                                             wrong++;

                                                                            if (wrong==7)

                                                                            {

                                                             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;

                                             

               }

                              

}

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