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

Hi, this is another homework question that i cant wrap my head around programmin

ID: 664911 • Letter: H

Question

Hi, this is another homework question that i cant wrap my head around

programming using C++ (we use visual basic in class)

homework #9

part 3-

Hangman game... There is a C++ "shell" program in another topic for you to complete.

Study the main body of the shell
Provide the C++ code for each of the three given functions

         createTemplate,updateTemplate and matchTemplate.


See comments below for help in setting up the logic for each of these three functions.
Also you can download "hangman.exe" file to run the hangman game to see how it works.

void createTemplate ( const char secretWord[], char guessTemplate[] )

guessTemplate char array is to be filled with dashes including the NULL char at the end of the dashes
The number of cells filled with dashes will be determined by the actual numbers of characters in secretWord
For example if secretWord is the C++ string "monday" which has only 6 chars
Then guessTemplate will be "------" which will have 6 dashes in it plus the '' added after the last dash
NOTE: An array of char is NOT a PROPER C++ string until the '' character has been added after the last character.
You will only need one loop to move one dash at a time into each guessTemplate[i] position i
The loop will terminate if secretWord[i] is equal to ''


void updateTemplate ( const char secretWord[], char guessLetter, char guessTemplate[])

If secretWord is "kansas" and guessLetter is 's' and guessTemplate (after an 'n' guess) is "--n---"
then guessTemplate would be changed to "--ns-s"

Your loop will be comparing guessLetter with secretWord[i]
If any such ith position matches then move guessLetter into guessTemplate[i]

bool matchTemplate ( const char secretWord[], const char guessTemplate[] )

This function will return true if secretWord and guessTemplate contain the same
C++ string(s) and will return false otherwise

You will need a loop to compare secretWord[i] with guessTemplate[i] returning false if unequal

oops i forgot the shell:
#include
using namespace::std;
// Function prototypes
void createTemplate ( const char secretWord[], char guessTemplate[] );
void updateTemplate ( const char secretWord[], char guessLetter, char guessTemplate[]);
bool matchTemplate ( const char secretWord[], const char guessTemplate[] );
//---------------------------------------------------------------------------------------------
const int MAX_SIZE = 15;
int main()
{
const int NUM_LINES = 25;      // Number of lines on the screen
char secretWord[MAX_SIZE],   // Secret word to be guessed
guessTemplate[MAX_SIZE];    // Template showing correct guesses
char guessLetter;                   // Letter guessed
int numGuesses;                    // Number of letters guessed
int j;                                     // Counter
cout << endl << "Enter the secret word: ";   cin >> secretWord;
// Scroll secrect word off the screen...to hide it
for ( j = 0 ; j < NUM_LINES ; j++ ) cout << endl;
// Create an empty guess template.
createTemplate(secretWord,guessTemplate);
// Play the game
//...keep looping(i.e. guessing until secretWord and guessTemplate are identical)
numGuesses = 0;
while ( !matchTemplate(secretWord,guessTemplate))
{
    numGuesses++;
    cout << guessTemplate << endl;
   cout << "Guess a letter: ";   cin >> guessLetter;
   // Place CORRECTLY guessed letter in template
   updateTemplate(secretWord,guessLetter,guessTemplate);
}
// Display the secret word and the number of guesses.
cout << guessTemplate << "=" << secretWord << endl;
cout << "You guessed the word in " << numGuesses << " guesses" << endl;
return 0;
}
//--------------------------------------------------------------------
// Insert your function implementations here.
//--------------------------------------------------------------------
void createTemplate ( const char secretWord[], char guessTemplate[] )
{
//ONLY ONE LOOP IS ALLOWED IN THIS FUNCTIO
}
void updateTemplate ( const char secretWord[], char guessLetter,
char guessTemplate[])
{
// ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION
}
bool matchTemplate ( const char secretWord[], const char guessTemplate[] )
{
// ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION
}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

const int MAX_TRIES=5;

int letterFill (char, string, string&);

int main ()
{
string name;
char letter;
int num_of_wrong_guesses=0;
string word;
string words[] =
{
"india",
"kansas",
"pakistan",
"nepal",
"malaysia",
"philippines",
"australia",
"iran",
"ethiopia",
"oman",
"indonesia"
};

//choose and copy a word from array of words randomly
srand(time(NULL));
int n=rand()% 10;
word=words[n];

// Initialize the secret word with the * character.
string unknown(word.length(),'*');

// welcome the user
cout << " Welcome to hangman...Guess a country Name";
cout << " Each letter is represented by a star.";
cout << " You have to type only one letter in one try";
cout << " You have " << MAX_TRIES << " tries to try and guess the word.";
cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << " " << unknown;
cout << " Guess a letter: ";
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown)==0)
{
cout << endl << "Whoops! That letter isn't in there!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" << endl;
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if user guessed the word.
if (word==unknown)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}

}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << " Sorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}

/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */

int letterFill (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;

// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

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