Help with Handman C++ game? I have some written down but I am having trouble wit
ID: 3778141 • Letter: H
Question
Help with Handman C++ game?
I have some written down but I am having trouble with two things:
1.) Listing the already used letters
2.) Displaying, "The category is.." when some number is entered asking for a hint.
How do I go about implimenting those?
This is my code so far, Please and thank you,
(it probably needs a little cleaning up)
#include
#include
#include
#include
#include
#include
using namespace std;
const int MaxGuesses = 8;
int letterFill(char, string, string&);
int main()
{
string unknown, word;
int num_wrong_guesses = 0,i;
char letter;
cout << "Are you ready to Play Hangman?!" << endl;
int category;
cout << "Please, Choose a Category( 1, 2, or 3): " << endl;
cin >> category;
if (category == 1)
{
string one[] = { "pineapple" };
int num = rand() % 1;
word = one[num];
string unknown(word.length(), '-');
cout << "Rules:" << endl;
cout << "Type one letter at a time." << endl;
cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;
while (num_wrong_guesses < MaxGuesses)
{
cout << " " << unknown << endl;
cout << " Guess a letter:" << endl;
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Incorrect letter! :( Try again." << endl;
num_wrong_guesses++;
}
else
{
cout << endl << "Correct letter! :) " << endl;
}
cout << "You have " << MaxGuesses - num_wrong_guesses;
cout << " tries left." << endl;
//for (i=0; i<25; i++)
//cout << letter[i] << endl;
if (word == unknown)
{
cout << word << endl;
cout << "You Win!";
break;
}
}
if (num_wrong_guesses == MaxGuesses)
{
cout << "Sorry, you lost the game!" << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}
else if (category == 2)
{
string two[] = { "canada"};
int num = rand() % 1;
word = two[num];
string unknown(word.length(), '-');
cout << "Rules:" << endl;
cout << "Type one letter at a time." << endl;
cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;
while (num_wrong_guesses < MaxGuesses)
{
cout << " " << unknown;
cout << " Guess a letter: ";
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Incorrect letter! :( Try again " << endl;
num_wrong_guesses++;
system("cls");
}
else
{
cout << endl << "Correct Letter! :) " << endl;
}
cout << "You have " << MaxGuesses - num_wrong_guesses;
cout << " tries left." << endl;
if (word == unknown)
{
cout << word << endl;
cout << "You Win!";
break;
}
}
if (num_wrong_guesses == MaxGuesses)
{
cout << "Sorry, you lost the game." << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}
else if (category == 3)
{
string three[] = { "game_of_thrones" };
string word;
int num = rand() % 1;
word = three[num];
string unknown(word.length(), '-');
cout << "Rules:" << endl;
cout << "Type one letter at a time." << endl;
cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;
cout << "This word contains a space, Please use an underscore, '_' for spaces. " << endl;
while (num_wrong_guesses < MaxGuesses)
{
cout << " " << unknown;
cout << " Guess a letter:";
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Incorrect letter! :( Try again." << endl;
num_wrong_guesses++;
system("cls");
}
else
{
cout << endl << "Correct letter! :) " << endl;
}
cout << "You have " << MaxGuesses - num_wrong_guesses;
cout << " guesses left." << endl;
if (word == unknown)
{
cout << word << endl;
cout << "You Win!";
break;
}
}
if (num_wrong_guesses == MaxGuesses)
{
cout << "Sorry, you lost the game!" << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}
}
int letterFill(char guess, string secretword, string &guessword)
{
int i;
int matches = 0;
int len = secretword.length();
for (i = 0; i< len; i++)
{
if (guess == guessword[i])
return 0;
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
Explanation / Answer
Answer :
import java.util.Scanner;
import java.io.*;
public class Hangmangame
{
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
char repeat = 'n';
String unknown;
StringBuffer interupts;
final int MAXCOUNT = 6;
int parts_of_body;
boolean visit;
String guess;
String count_guess;
char alpha_char;
Scanner inputfile = new Scanner(new FileReader("words.txt"));
do {
unknown = inputfile.next();
count_guess = "";
visit = false;
parts_of_body = MAXCOUNT;
interupts = makeinterupts(unknown);
while (! visit)
{
System.out.println("Here is your word: " + interupts);
System.out.println("count_guess so far: " + count_guess);
System.out.print("enter a guess (alpha_char or word): ");
guess = sc.next();
if (guess.length() > 1)
{
if (guess.equals(unknown))
System.out.println("you win the game!");
else
System.out.println("you lost the game");
visit=true;
}
else
{
alpha_char = guess.charAt(0);
count_guess += alpha_char;
if (unknown.indexOf(alpha_char) < 0)
{ --parts_of_body;
System.out.print("bad guess - ");
}
else
{
matchalpha_char(unknown, interupts, alpha_char);
}
System.out.println(parts_of_body + "bodyparts are left");
if (parts_of_body == 0)
{ System.out.println("you lose");
visit = true;
}
if (unknown.equals(interupts.toString()))
{ System.out.println("you win!");
visit = true;
}
}
}
if (inputfile.hasNext())
{
System.out.print("play repeat (y/n)?: ");
repeat = sc.next().charAt(0);
}
else
System.out.println("thanks for playing (no more words)");
} while (inputfile.hasNext() && (repeat == 'Y' || repeat == 'y'));
}
public static void matchalpha_char(String unknown, StringBuffer interupts, char alpha_char)
{
for (int index = 0; index < unknown.length(); index++)
if (unknown.charAt(index) == alpha_char)
interupts.setCharAt(index, alpha_char);
System.out.print("nice guess - ");
}
public static StringBuffer makeinterupts(String s)
{
StringBuffer interupts = new StringBuffer(s.length());
for (int count=0; count < s.length(); count++)
interupts.append('-');
return interupts;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.