In: Computer Science
Hey, please write in c++. This a two-part lab but I did not put them on a separate question then it would be confusing to the who is answering it. The first part is already done and the code is down below but I wanted to provide the instructions for it just in case.
Please follow the instructions and provide comments helps me understand if you could, make sure the code meets all the criteria, It should be an easy assignment it should be creating a hangman game.
Please Help and thank you.
use the same code for part 2 use the code I provide, don't make
separate codes for each part.
If you have any questions please let me know
-----------------------------------------------------------------
First part
I already have the file don't worry about it
moon racecar program cat word
Sample Run #1 (bold, underlined text is what the user types):
Game? 3 L----+---- | | | X L----+---- | O | | X L----+---- | O | | | X L----+---- | O | /| | X L----+---- | O | /|\ | X L----+---- | O | /|\ | / X L----+---- | O | /|\ | / \ X You lost Answer: program
This the code for this first part, use it to do the second part, please.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int hangman_level = 0;
int game_no;
int guess_char;
int word_len;
int match_count = 0;
string word;
/*
*
* Getting initial inputs from user
*
*/
game_no = getGameNumberFromUser();
word = getWordFromFile(game_no);
word_len = word.length();
/*
* Drawing initial hangman figure
*/
drawHangman(hangman_level);
while (true)
{
/*
* Iterate until win or loose
*/
guess_char = getGussedCharFromUser();
/*
* Compare guessed char with word char
*/
if (guess_char == word[match_count])
{
match_count++;
}
else
{
hangman_level++;
}
/*
* Draw next level hangman figure
*/
drawHangman(hangman_level);
/*
* Checking if hangman drwan completely
* if so the user will loose geme
*/
void drawHangman(int level)
{
/*
* Drawing hangman levels
*/
switch (level)
{
case 0:
cout << 'L' << "----+----" << endl;
cout << '|' << endl << "|" << endl <<
"|" << endl << "X" << endl;
break;
case 1:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "|" << endl
<< "|" << endl << "X" << endl;
break;
case 2:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| |" << endl
<< "|" << endl << "X" << endl;
break;
case 3:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| /|" << endl
<< "|" << endl << "X" << endl;
break;
case 4:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| /|\\" << endl
<< "|" << endl << "X" << endl;
break;
case 5:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| /|\\" << endl
<< "| /" << endl << "X" << endl;
break;
case 6:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| /|\\" << endl
<< "| / \\" << endl << "X" << endl;
break;
}
}
/*
* Getting a word from file
*/
string getWordFromFile(int pos)
{
string word;
ifstream ifstr;
ifstr.open("word.txt");
for (int i = 0; i < pos - 1; i++)
{
ifstr >> word;
}
ifstr >> word;
ifstr.close();
return word;
}
/*
* Getting game number from user
*/
int getGameNumberFromUser()
{
int game_no;
cout << "Enter game number: ";
cin >> game_no;
return game_no;
}
int getGussedCharFromUser()
/*
* Getting a guessed character from user
*/
{
char guess_char;
cout << "Enter ypur guess: ";
cin >> guess_char;
return guess_char;
}
if (hangman_level == 6)
{
cout << "You lost !!!" << endl;
cout << "Correct word: " << word << endl;
break; // break statement is encountered inside a loop, the loop is
immediately terminated and program control resumes at the next
statement following the loop
}
/*
* checking if whole word guessed correctly
* if so, user win
*/
if (match_count == word_len)
{
cout << "Congrats you won !!!" << endl;
break; // break statement is encountered inside a loop,
the loop is immediately terminated and program control resumes at
the next statement following the loop
}
}
cout << "Game Over" << endl;
return 0;
}
-------------------------------------------------------
Part 2
Sample Run #1 – user wins (bold, underlined text is what the user types):
Game? 3 L----+---- | | | X ******* ? a L----+---- | | | X *****a* ? b Oops L----+---- | O | | X *****a* ? c Oops L----+---- | O | | | X *****a* ? p L----+---- | O | | | X p****a* ? r L----+---- | O | | | X pr**ra* ? o L----+---- | O | | | X pro*ra* ? g L----+---- | O | | | X progra* ? m L----+---- | O | | | X Congrats! Answer: program
Sample Run #2 – user loses (bold, underlined text is what the user types):
Game? 3 L----+---- | | | X ******* ? a L----+---- | | | X *****a* ? b Oops L----+---- | O | | X *****a* ? c Oops L----+---- | O | | | X *****a* ? d Oops L----+---- | O | /| | X *****a* ? e Oops L----+---- | O | /|\ | X *****a* ? f Oops L----+---- | O | /|\ | / X *****a* ? g L----+---- | O | /|\ | / X ***g*a* ? h Oops L----+---- | O | /|\ | / \ X You lost Answer: program
screenshot
-------------------------------------------------------------------
Program
//Header files
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
//Function prototypes
void drawHangman(int);
string getWordFromFile(int);
int getGameNumberFromUser();
int getGussedCharFromUser();
string makeWord(string);
string changeMakeWord(string, string, char);
int checkCorrectness(string, string, char);
int main()
{
//Variable declaration
int hangman_level = 0;
int game_no;
int guess_char;
int word_len;
int match_count = 0;
string word,secretWord;
/*
*
* Getting initial inputs from user
*
*/
game_no = getGameNumberFromUser();
//Game number less tham 1 then select random
if (game_no < 1) {
game_no = rand() % 5 + 1;
}
word = getWordFromFile(game_no);
word_len = word.length();
secretWord = makeWord(word);
/*
* Drawing initial hangman figure
*/
drawHangman(hangman_level);
while (true)
{
cout << secretWord <<
endl;
/*
* Iterate until win or loose
*/
guess_char =
getGussedCharFromUser();
secretWord = changeMakeWord(word,
secretWord, guess_char);
/*
* game repetition check
*/
if (checkCorrectness(word,
secretWord, guess_char) == 2) {
match_count++;
}
else if (checkCorrectness(word,
secretWord, guess_char) == 1) {
/*
* Draw next level hangman
figure
*/
drawHangman(hangman_level);
cout <<
"Congrats you won !!!" << endl;
break;
}
else
{
cout <<
"\noops" << endl;
hangman_level++;
}
/*
* Draw next level hangman
figure
*/
drawHangman(hangman_level);
/*
* Checking if hangman drwan
completely
* if so the user will loose
geme
*/
if (hangman_level == 6)
{
cout <<
"You lost !!!" << endl;
cout <<
"Correct word: " << word << endl;
break; // break
statement is encountered inside a loop, the loop is immediately
terminated and program control resumes at the next statement
following the loop
}
}
cout << "Game Over" << endl;
return 0;
}
void drawHangman(int
level)
{
/*
* Drawing
hangman levels
*/
switch
(level)
{
case 0:
cout << 'L' << "----+----" <<
endl;
cout << '|' << endl << "|"
<< endl << "|" << endl << "X" <<
endl;
break;
case 1:
cout << 'L' << "----+----" <<
endl;
cout << "| O" << endl << "|"
<< endl << "|" << endl << "X" <<
endl;
break;
case 2:
cout << 'L' << "----+----" <<
endl;
cout << "| O" << endl << "| |"
<< endl << "|" << endl << "X" <<
endl;
break;
case 3:
cout << 'L' << "----+----" <<
endl;
cout << "| O" << endl << "|
/|" << endl << "|" << endl << "X" <<
endl;
break;
case 4:
cout << 'L' << "----+----" <<
endl;
cout << "| O" << endl << "|
/|\\" << endl << "|" << endl << "X"
<< endl;
break;
case 5:
cout << 'L' << "----+----" <<
endl;
cout << "| O" << endl << "|
/|\\" << endl << "| /" << endl << "X"
<< endl;
break;
case 6:
cout << 'L' << "----+----" <<
endl;
cout << "| O" << endl << "|
/|\\" << endl << "| / \\" << endl << "X"
<< endl;
break;
}
}
/*
* Getting a word from file
*/
string getWordFromFile(int
pos)
{
string
word;
ifstream
ifstr;
ifstr.open("words.txt");
for (int i = 0;
i < pos - 1; i++)
{
ifstr>>word;
}
ifstr>>word;
ifstr.close();
return
word;
}
/*
* Getting game number from
user
*/
int getGameNumberFromUser()
{
int
game_no;
cout <<
"Enter game number: ";
cin >>
game_no;
return
game_no;
}
int
getGussedCharFromUser()
/*
* Getting a
guessed character from user
*/
{
char
guess_char;
cout << "?
";
cin >>
guess_char;
return
guess_char;
}
/*
Function to create guess word
corresponding * word
*/
string makeWord(string word)
{
string
secretWord = "";
for (int i = 0;
i < word.length(); i++) {
secretWord += '*';
}
return
secretWord;
}
/*
Function to change into correct
letter instead of *
When the guess char correct
*/
string changeMakeWord(string word,
string secretWord, char guess) {
for (int i = 0;
i < word.length(); i++) {
if (word[i] == guess) {
secretWord[i] = guess;
}
}
return
secretWord;
}
/*
Function to check the word correct
or not
If word and secret word same then
win and return 1
If match found
then return 2
else return 0
*/
int checkCorrectness(string word,
string secretWord, char guess) {
if (word ==
secretWord) {
return 1;
}
else {
for (int i = 0; i < word.length(); i++)
{
if (word[i] == guess) {
return
2;
}
}
}
return 0;
}
-------------------------------------------------------------------------------------
Output
Enter game number: 3
L----+----
|
|
|
X
*******
? a
L----+----
|
|
|
X
*****a*
? b
oops
L----+----
| O
|
|
X
*****a*
? c
oops
L----+----
| O
| |
|
X
*****a*
? d
oops
L----+----
| O
| /|
|
X
*****a*
? e
oops
L----+----
| O
| /|\
|
X
*****a*
? f
oops
L----+----
| O
| /|\
| /
X
*****a*
? g
L----+----
| O
| /|\
| /
X
***g*a*
? h
oops
L----+----
| O
| /|\
| / \
X
You lost !!!
Correct word: program
Game Over