I'm trying to make a Hangman game in C#. It has a 5 tries limit, then the user will get a "you lose" message. If the user gets a letter wrong, it was supposed to get them a "wrong letter -- try again"
class Program
{
static void Main(string[] args)
{
string word;
int correct = 0;
int errors = 0;
Console.WriteLine("Enter a word");
word = Console.ReadLine();
char[] letters = word.ToCharArray();
char[] hanged = word.ToCharArray();
char digits;
for (int i = 0; i < word.Length; i )
{
if (letters[i] == ' ')
{
hanged[i] = ' ';
}
else
{
hanged[i] = '_';
}
}
Console.Clear();
do
{
Console.Write(" ________\n"
"| |\n"
"| |\n"
"|\n"
"|\n"
"|\n"
"|\n"
"|\n"
"|\n\n");
Console.SetCursorPosition(2, Console.CursorTop - 2);
for (int i = 0; i < word.Length; i )
{
Console.Write(hanged[i] " ");
}
Console.WriteLine("\n\n\nEnter a letter");
digits = Convert.ToChar(Console.Read());
for (int i = 0; i < word.Length; i )
{
if (digits == hanged[i])
{
Console.WriteLine("\nletter already in there -- press enter");
Console.ReadKey();
}
else if (digits == letters[i])
{
hanged[i] = digits;
correct ;
}
else if (digits != letters[i])
{
Console.WriteLine("\nwrong letter -- try again");
errors ;
Console.ReadKey();
}
if (errors == 5)
{
Console.WriteLine("\nYou lose");
Console.ReadKey();
break;
} else if (correct == word.Length)
{
Console.WriteLine("\nYou win");
Console.ReadKey();
break;
}
}
Console.Clear();
} while (correct < word.Length);
Console.ReadKey();
}
}
However, my program is stuck in a loop when counting the losses. The code thats messing it up is this section
else if (digits != letters[i])
{
Console.WriteLine("\nwrong letter -- try again");
errors ;
Console.ReadKey();
}
When I delete this section and run the code, in case of a wrong letter it just runs over it infinitely and the player will get infinite chances to try when they were supposed to have only 5 chances. How can I fix this?
CodePudding user response:
The problem is that the loop for (int i = 0; i < word.Length; i )
that is responsible for finding a letter should not perform other logic in there. E.g., if the user types a letter matching the third position, then this will count 2 errors before counting one correct letter.
I suggest using Array.IndexOf
to get the index of a letter. This returns -1 if it is not found. Also, I found that the different kinds of Console.ReadXY
can interfere with each other. E.g. calling Console.Read()
and pressing a letter plus Enter makes the next Console.ReadLine()
just return without waiting for user input.
My attempt (content of static void Main(string[] args)
):
string word;
int correct = 0;
int errors = 0;
Console.WriteLine("Enter a word");
word = Console.ReadLine();
char[] letters = word.ToCharArray();
char[] hanged = word.ToCharArray();
char digits;
for (int i = 0; i < word.Length; i ) {
if (letters[i] == ' ') {
hanged[i] = ' ';
} else {
hanged[i] = '_';
}
}
Console.Clear();
do {
Console.Write(" ________\n"
"| |\n"
"| |\n"
"|\n"
"|\n"
"|\n"
"|\n"
"|\n"
"|\n\n");
Console.SetCursorPosition(2, Console.CursorTop - 2);
for (int i = 0; i < word.Length; i ) {
Console.Write(hanged[i] " ");
}
Console.Write("\n\n\nEnter a letter: ");
ConsoleKeyInfo key = Console.ReadKey();
digits = key.KeyChar;
int hangedIndex = Array.IndexOf(hanged, digits);
int lettersIndex = Array.IndexOf(letters, digits);
if (hangedIndex >= 0) {
Console.WriteLine("\nletter already in there -- press enter");
Console.ReadLine();
} else if (lettersIndex >= 0) {
hanged[lettersIndex] = digits;
correct ;
} else {
Console.Write("\nwrong letter -- try again (press enter) ");
Console.ReadLine();
errors ;
}
if (errors == 5) {
Console.WriteLine("\nYou lose");
break;
} else if (correct == word.Length) {
Console.WriteLine("\nYou win");
break;
}
Console.Clear();
} while (correct < word.Length);
Console.ReadKey();