I'm having issues with my Hangman game. Could you please help me out??

  • Thread starter Thread starter OhhBnertt
  • Start date Start date
O

OhhBnertt

Guest
Hello everyone, I am trying to make a hangman game using a string array that will randomly choose a word out of a txt file that I have made up. There's a bunch of animal names in that file and then it will use a couple methods in order to disguise the word and check if the letters that the user's entering are a part of that word. In the main method, I have all my IF statements inside a do-while loop that uses a boolean (isPlaying = true) that will break the loop when the user finishes the word. These IF statements check to see if the letter is correct, wrong or if they've entered the same letter again. It also checks to see if the word is filled correctly where it will ask them if they want to play again. There is no amount of lives that the user has. They keep playing until the word is correctly guessed. It will keep count of the wrong guesses though and display them at the end. The issue I'm having is that it works great the first time but then if I enter "y" to play again, the second time the word will automatically fill itself if I guess a couple of the correct letters. There will still be a lot of letters that are disguised but it says "good game the word is (word)" even though I haven't finished. I think it's because I'm not calling on my main method properly when the user wants to play again but I'm still pretty new to C# so I was going to see if any of you could point me in the right direction. I'm not asking you to complete my program but I'm just really stuck and I'm not entirely sure what to do. Any help would be greatly appreciated. Thanks! Here's the code:

PS: I think it's messing up because if I enter the last letter in the word correctly and one of the first letters it thinks that it's filled correctly and displays the word. It only happens sometimes. Usually it works perfectly but this is really bugging me lol


static void Main(string[] args)
{
bool isPlaying = true;
string[] words = File.ReadAllLines("word.txt");//reading word.txt file that includes all 10 secret words
Random rand = new Random();//random num generator
string secretWord = (words[rand.Next(words.Length)]);//string for random word to be guessed each round
int countWrongGuess = 0;//amount of guesses until the user loses the game
List<string> guessLetter = new List<string>();//string to hold the letters that have already been guessed

Console.WriteLine("|--------------------------------------|");
Console.WriteLine("| Hangman Game |");
Console.WriteLine("|--------------------------------------|");

Console.WriteLine("I have picked a random word on Animals.");
Console.WriteLine("Your job is to guess the word!");
Console.WriteLine("The length of the word is {0} digits! Good luck :) ", secretWord.Length);
Console.WriteLine();
do
{

wordLetter(secretWord, guessLetter);

string initialLetters = wordLetter(secretWord, guessLetter);
Console.WriteLine("(Guess) Enter a letter for the word: {0}", initialLetters);

string userInput = Console.ReadLine();

if (guessLetter.Contains(userInput))//if the user enters a letter that's already been guessed
{
Console.WriteLine("You already entered {0}", userInput);
isPlaying = true;

}
guessLetter.Add(userInput);//adds the users input to the string of guessed letters

if (secretWord.Contains(userInput))//if the user enters a correct letter in the secret word
{

Console.WriteLine("Great guess! {0} is in the word", userInput);
isPlaying = true;

}
if (correctWord(secretWord, guessLetter))//if the word is filled completely
{

Console.WriteLine($"Great game! The word is {secretWord}. You missed {countWrongGuess} times!");
Console.WriteLine("Did you want to play again? y/n");

string userChoice = Console.ReadLine();

if (string.Equals(userChoice, "y"))
{
Main(args);
}
}
else if (!secretWord.Contains(userInput))//if the user doesn't enter a correct letter in the secret word
{
isPlaying = true;
Console.WriteLine("Sorry! {0} isn't in the word :(", userInput);
countWrongGuess++;//increases the count of wrong guesses and displays them at the end of the game

}


}
while (isPlaying == true);
}
static string wordLetter(string secretWord, List<string> guessLetter)//finds if letter guessed is in secret word
{
string correctGuessedLetters = "";//empty string to hold correct letters that are guessed

for (int i = 0; i < secretWord.Length; i++)//loop that loops through the secret word index length
{
string a = Convert.ToString(secretWord);//new string to check each letter in the word to see if correct

if (guessLetter.Contains(a))
{
correctGuessedLetters += a;//if correct the letter is added to string
}
else
{
correctGuessedLetters += "*";//if incorrect the * stays the same
}
}
return correctGuessedLetters;//return the correct guessed letters to be used in the program
}
static bool correctWord(string secretWord, List<string> guessLetter)
{
bool correctWord = false;//boolean to see if the correct word has been guessed or not

for (int i = 0; i < secretWord.Length; i++)
{
string a = Convert.ToString(secretWord);//initialized string for the element value of the secretWord array

if (guessLetter.Contains(a))//checks to see if the string is in the list of letters guessed
{
correctWord = true;//if it contains the string value then we have the correct word
}
else
{
correctWord = false;//if it doesn't contain the string value then we don't have the correct word
}
}
return correctWord;
}

Continue reading...
 
Back
Top