C++ project will play the classic hangman game. The rules of hangman are: • Ask
ID: 3604418 • Letter: C
Question
C++ project will play the classic hangman game. The rules of hangman are: • Ask the user for what word number the user wants to play. There is a file, word.txt, which will contain a list of words. The file will have 5 words in it, so if the user enters 0 then use the first word in the file and if the user enters 4 use the last word in the file. If the user enters -1, then you should randomly pick a word from the file. The file will have one word per line. Not all words will be the same length. Read the word into a string (or a character array). o Sample word.txt words moon sweater laugh forty • Now you are ready to play the game, show the user ‘blanks’ and the current state of the ‘hangman’. o Note that the letters (blanks or already guessed letters) are separated by spaces o _ e _ _ _ not _e___ • Then let the user guess a letter. Everything in our game will be lowercase letters. If the user guesses a letter that is not in the word, then add another part to the ‘hangman’. If the user guesses correctly then change the ‘blank’ to that letter. • Continue the step above until either all letters in the word have been guessed or until you have completely drawn the ‘hangman’. • Display a message to the user indicating whether the user won or lost and show the word too. • Note - make sure you use plenty of functions!
Explanation / Answer
words.cpp
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
void findWords();
void usingLetter(int nob);
using namespace std;
void findWords()
{
char wrd[20];
char soln[20];
char guessings;
char posit;
int wrong=0;
int fin=0;
int ranging=0;
int cnt=0;
ifstream fl;
fl.open("word.txt");
cout << "Here's the word: ";
cout << " ";
fl >> wrd;
int sz = strlen(wrd);
for (int a=0; a<sz; a++)
{
soln[a]= '*';
}
for (int a=sz; a<20; a++)
{
soln[a]= ' ';
}
for(int a=0; a<20; a++)
{
cout << soln[a];
}
cout << " ";
cout << "It has " << sz << " letters. ";
cout << wrd << endl;
cout << " ";
while ((wrong <= 6) && (fin ==0))
{
cout << " ";
cout << "Guess a letter. ";
cout << " ";
cin >> guessings;
cout << " ";
ranging = 0;
for(int a=0; a<sz; a++)
{
if(guessings==wrd[a])
{
ranging = 1;
soln[a]=guessings;
}
}
if(ranging == 0) cout<< "The guessings is incorrect ";
wrong++;
for(int a=0; a<sz; a++)
{
cout << soln[a];
}
ranging =0;
for(int k=0; k<sz; k++)
{
if(soln[k] != wrd[k])
ranging=1;
}
if(ranging==0)
fin=1;
}
cout << " ";
cout << "Guess another letter. ob";
cout << " ";
cin >> guessings;
cout << " ";
cout << " ";
}
void usingLetter(int nob)
{
char usedOnes[10];
int a;
a=0;
nob=usedOnes[a];
cout << "You have used the following letters so far: ";
cout << usedOnes[a] << ", " << endl;
cout << " ";
a++;
}
int main()
{
char ranging = 'y';
while(ranging=='y' || ranging=='Y')
{
cout << "Welcome to Hangman!!!" << endl;
cout << "Siz Chances will be given to guess the correct answer" <<endl;
findWords();
cout << "Do you want to play agian? ob";
cout << " ";
cin >> ranging;
}
return 0;
}
Rate an upvote....Thankyou
Hope this helps....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.