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 hangmans 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 hangmans gallows, and a string of asterisk representing the unknown word, and a prompt for the letter to be guest.

TEXT FILE IS ALREADY GIVEN, JUST NEED TO READ IN THE PROGRAM.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <ctime>
#include <vector>

using namespace std;

void printHangman(int errCount, string myWord);

string getRandWord(vector<string> wordList)
{
   int randPos = rand() % wordList.size(); // get random position index
   return wordList[randPos];// return word
}

vector<string> getWords()
{
    time_t t;
   srand((unsigned) time(&t)); /* initialize seed to generate random number */

   string fname;
   cout <<"Enter file name"<< endl;
   cin >> fname;
   fstream afile;
   afile.open(fname.c_str()); // open file
   if (!afile) {
           cerr << "File I/O error!" << endl;
           exit(0);
       }
   vector<string> wordList;
       string word;
       while(afile >> word) // read all words from file
           wordList.push_back(word);
       afile.close();
   return wordList;
}

int main()
{

  
    vector<string> wordList = getWords();
    int errCount = 0;
    char letter;
    string randWord = getRandWord(wordList); // select random word from list
    string ansWord = randWord;
    string myWord = "";
    for (int i = 0; i < randWord.size(); i++) myWord += "-"; // guessed word is initially all -

    while (errCount < 8) { // run till we have 8 miss
        printHangman(errCount, myWord); // print hangman
        cout << "guess a letter: ";
        cin >> letter;
        int pos = randWord.find(letter); // check if guest letter exist
        if (pos != string::npos) {
        randWord[pos] = '@';
            myWord[pos] = letter; // if it exist replace myWord "-" at that position with letter
   }
        else
            errCount++;   // else increase miss count
        if (myWord.find('-') == string::npos) break; // if there is no "-" in myWord, We have guessed the word correctly, break;
    }
    printHangman(errCount, myWord);
    if (errCount < 8) {
        cout << "Congratulations!!! You won!!!" << endl;
    } else {
        cout << "Sorry You have lost :( " << endl << "The test word was " << ansWord << endl;
    }

    return 0;
}

void printHangman(int errCount, string myWord)
{
     if (errCount < 1) cout << "+" << endl;
     else cout << "+----+"<< endl;
     if (errCount < 2)cout << "|"<< endl;
     else cout << "|    |"<< endl;
     if (errCount < 3)cout << "|"<< endl;
     else cout << "|    o"<< endl;
     if (errCount < 4)cout << "|"<< endl;
     else if(errCount == 4)cout << "|    |"<< endl;
     else if(errCount == 5)cout << "|   \\|"<< endl;
     else cout << "|   \\|/"<< endl;
     if (errCount < 4)cout << "|"<< endl;
     else cout << "|    |"<< endl;
     if (errCount < 7)cout << "|"<< endl;
     else if(errCount == 7)cout << "|   /"<< endl;
     else cout << "|   / \\"<< endl;
     cout << "|" << endl;
     cout << "+-------"<< endl;
     cout << endl << endl;

     for (int i = 0; i < myWord.size(); i++) cout << myWord[i] << " ";     // print space separated word guessed so far
     cout << endl;
     for (int i = 0; i < myWord.size(); i++) cout << "-" << " ";
     cout << endl;
}


sanjiv@sanjiv-VirtualBox:~$ g++ hangman.cpp -o hangman
sanjiv@sanjiv-VirtualBox:~$
sanjiv@sanjiv-VirtualBox:~$ ./hangman
Enter file name
hangman.txt
+
|
|
|
|
|
|
+-------


- - - - - - - -
- - - - - - - -
guess a letter: a
+
|
|
|
|
|
|
+-------


- a - - - - - -
- - - - - - - -
guess a letter: y
+
|
|
|
|
|
|
+-------


- a y - - - - -
- - - - - - - -
guess a letter: t
+
|
|
|
|
|
|
+-------


- a y - - - - t
- - - - - - - -
guess a letter: e
+----+
|
|
|
|
|
|
+-------


- a y - - - - t
- - - - - - - -
guess a letter: o
+----+
|    |
|
|
|
|
|
+-------


- a y - - - - t
- - - - - - - -
guess a letter: i
+----+
|    |
|
|
|
|
|
+-------


- a y - i - - t
- - - - - - - -
guess a letter: d
+----+
|    |
|
|
|
|
|
+-------


d a y - i - - t
- - - - - - - -
guess a letter: l
+----+
|    |
|
|
|
|
|
+-------


d a y l i - - t
- - - - - - - -
guess a letter: g
+----+
|    |
|
|
|
|
|
+-------


d a y l i g - t
- - - - - - - -
guess a letter: h
+----+
|    |
|
|
|
|
|
+-------


d a y l i g h t
- - - - - - - -
Congratulations!!! You won!!!
sanjiv@sanjiv-VirtualBox:~$
sanjiv@sanjiv-VirtualBox:~$ ./hangman
Enter file name
hangman.txt
+
|
|
|
|
|
|
+-------


- - - - - - - -
- - - - - - - -
guess a letter: a
+
|
|
|
|
|
|
+-------


a - - - - - - -
- - - - - - - -
guess a letter: e
+----+
|
|
|
|
|
|
+-------


a - - - - - - -
- - - - - - - -
guess a letter: i
+----+
|    |
|
|
|
|
|
+-------


a - - - - - - -
- - - - - - - -
guess a letter: o
+----+
|    |
|
|
|
|
|
+-------


a - - - o - - -
- - - - - - - -
guess a letter: m
+----+
|    |
|    o
|
|
|
|
+-------


a - - - o - - -
- - - - - - - -
guess a letter: n
+----+
|    |
|    o
|    |
|    |
|
|
+-------


a - - - o - - -
- - - - - - - -
guess a letter: p
+----+
|    |
|    o
|   \|
|    |
|
|
+-------


a - - - o - - -
- - - - - - - -
guess a letter: r
+----+
|    |
|    o
|   \|/
|    |
|
|
+-------


a - - - o - - -
- - - - - - - -
guess a letter: s
+----+
|    |
|    o
|   \|/
|    |
|   /
|
+-------


a - - - o - - -
- - - - - - - -
guess a letter: t
+----+
|    |
|    o
|   \|/
|    |
|   /
|
+-------


a - t - o - - -
- - - - - - - -
guess a letter: u
+----+
|    |
|    o
|   \|/
|    |
|   /
|
+-------


a - t - o u - -
- - - - - - - -
guess a letter: v
+----+
|    |
|    o
|   \|/
|    |
|   / \
|
+-------


a - t - o u - -
- - - - - - - -
Sorry You have lost :(
The test word was although
sanjiv@sanjiv-VirtualBox:~$

Add a comment
Know the answer?
Add Answer to:
​ TEXT FILE IS ALREADY GIVEN, JUST NEED TO READ IN THE PROGRAM. Learning objectives: The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put...

    can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put together everything you've learned to make the classic game "Hangman" in a webpage. If you are unfamiliar with the rules of Hangman, the game works like this: A gallows is drawn on a surface, and Player 1 chooses a word that player 2 must guess. Empty dashes for each letter in the word are drawn next to the gallows (so a 7-letter word means...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that...

    javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that lets a user play the hangman game. The user guesses a word by entering one letter at a time, as shown in Figure followings. If the user misses seven times, a hanging man swings, as shown in Figures. Once a word is finished, the user can press the Enter key to continue to guess another word. Guess a word: ***** God Missed letters Guess...

  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • Consider a class that could be used to play a game of hangman. The class has...

    Consider a class that could be used to play a game of hangman. The class has the following attributes: The secret word. The disguised word, in which each unknown letter in the secret word is replaced with a question mark (?). For example, if the secret word is abracadabra and the letters a, b, and e have been guessed, the disguised word would be ab?a?a?ab?a. The number of guesses made. The number of incorrect guesses. It will have the following...

  • C++ programming Submit assignment to Blackboard by the midnight on the due date listed above. (Verify...

    C++ programming Submit assignment to Blackboard by the midnight on the due date listed above. (Verify if your submission is completed.) The only people you are to discuss this project with are myself and the CS Department tutor. Duplicate code, if discovered, will result in a 0 grade. Your source file(s) should follow the Computer Science Coding Standards (available at the Computer Science Homepage) including proper indenting and spacing 1 Overview program, you will play a game of hangman. In...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT