I'm making a console hangman game and I have most of it done but there's 1 problem. Say if the word is "food" and the user guesses "o", the program is supposed to say "This is what you have so far: oo" but instead it says "This is what you have so far: o_" After that, it doesn't let you add the extra "o" in the next try or ever show that the second "o" is there. I know why it's happening -- because of the if statements, but I don't know how to fix it. Does anyone know how I can fix it?
This is what I have so far:
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The theme is food");
string[] words = new string[5];
words[0] = "potato";
words[1] = "radish";
words[2] = "raisin";
words[3] = "almond";
words[4] = "lychee";
Random random = new Random();
int randomNumber = random.Next(0, 5);
string[] food = { words[randomNumber] };
char[] hangman = words[randomNumber].ToCharArray();
for (int k = 0; k < 6; k )
{
Console.WriteLine(hangman[k]);
}
char[] userinput = { '_', '_', '_', '_', '_', '_' };
Console.WriteLine("Let's begin the game, you have 10 tries to get all the letters. Input 1 letter that you think is in the word.");
Console.WriteLine("Hint: The word has 6 letters");
for (int i = 0; i < 10; i )
{
char user = char.Parse(Console.ReadLine());
if (user == hangman[0])
{
userinput[0] = user;
Console.WriteLine("You got a letter! this is what you have so far ");
for (int l = 0; l < 6; l )
{
Console.Write(userinput[l]);
}
Console.WriteLine(". Try another letter ");
}
else if (user == hangman[1])
{
userinput[1] = user;
Console.WriteLine("You got a letter! this is what you have so far ");
for (int l = 0; l < 6; l )
{
Console.Write(userinput[l]);
}
Console.WriteLine("Try another letter ");
}
else if (user == hangman[2])
{
userinput[2] = user;
Console.WriteLine("You got a letter! this is what you have so far ");
for (int l = 0; l < 6; l )
{
Console.Write(userinput[l]);
}
Console.WriteLine("Try another letter ");
}
else if (user == hangman[3])
{
userinput[3] = user;
Console.WriteLine("You got a letter! this is what you have so far ");
for (int l = 0; l < 6; l )
{
Console.Write(userinput[l]);
}
Console.WriteLine("Try another letter ");
}
else if (user == hangman[4])
{
userinput[4] = user;
Console.WriteLine("You got a letter! this is what you have so far ");
for (int l = 0; l < 6; l )
{
Console.Write(userinput[l]);
}
Console.WriteLine("Try another letter ");
}
else if (user == hangman[5])
{
userinput[5] = user;
Console.WriteLine("You got a letter! this is what you have so far ");
for (int l = 0; l < 6; l )
{
Console.Write(userinput[l]);
}
Console.WriteLine("Try another letter ");
}
else
{
Console.WriteLine(" Whoops, thats not in the word. Try again ");
}
}
}
}
}
CodePudding user response:
If-Else-If, allow only one branch to be executed.
You will want to loop on the target word, and increment the number of letter found.
You wont have to hard code the index your at like user == hangman[4]
or userinput[4] = user;
as you will already have the index.
// Check matches
var found = 0;
for (int j = 0; j < target.Length; j )
{
if (target[j] == userChoice)
{
hangman[j] = userChoice;
found ;
}
}
CodePudding user response:
With a little bit of refactoring, you can change the number of words in your list without needing to change the random word selection code.
Then, using a loop as suggested by Drag and Drop already, your code becomes much smaller and works for any length word:
public static void Main (string[] args) {
string[] words = {
"potato",
"radish",
"raisin",
"almond",
"lychee",
"broccoli",
"asparagus"
};
Random random = new Random();
string word = words[random.Next(words.Length)].ToLower();
char[] hangman = word.ToCharArray();
char[] userinput = (new String('_', word.Length)).ToCharArray();
int numGuesses = 10;
Console.WriteLine("Let's begin the game, you have " numGuesses " tries to get all the letters.");
Console.WriteLine("Input 1 letter that you think is in the word.");
Console.WriteLine("Hint: The word has " word.Length " letters");
for (int i = 0; i < numGuesses; i )
{
Console.WriteLine();
Console.WriteLine("Guesses Left: " (numGuesses - i));
Console.WriteLine(String.Join(" ", userinput));
Console.Write("Letter Guess: ");
char user = char.Parse(Console.ReadLine().ToLower().Trim());
int matches = 0;
for(int index=0; index<hangman.Length; index ) {
if (hangman[index]==user && userinput[index]=='_') {
userinput[index] = user;
matches ;
}
}
if (matches > 0) {
Console.WriteLine("You got a letter!");
Console.WriteLine(String.Join(" ", userinput));
if (String.Join("", userinput) == word) {
break;
}
}
else {
Console.WriteLine("Whoops, thats not in the word, or was already found. Try again!");
}
}
Console.WriteLine();
if (String.Join("", userinput) == word) {
Console.WriteLine("You figured out the word!");
}
else {
Console.WriteLine("Sorry. You ran out of guesses!");
}
Console.WriteLine();
Console.WriteLine("The word was:");
Console.WriteLine(word);
}
A couple of sample runs:
Let's begin the game, you have 10 tries to get all the letters.
Input 1 letter that you think is in the word.
Hint: The word has 9 letters
Guesses Left: 10
_ _ _ _ _ _ _ _ _
Letter Guess: a
You got a letter!
a _ _ a _ a _ _ _
Guesses Left: 9
a _ _ a _ a _ _ _
Letter Guess: e
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 8
a _ _ a _ a _ _ _
Letter Guess: i
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 7
a _ _ a _ a _ _ _
Letter Guess: o
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 6
a _ _ a _ a _ _ _
Letter Guess: u
You got a letter!
a _ _ a _ a _ u _
Guesses Left: 5
a _ _ a _ a _ u _
Letter Guess: s
You got a letter!
a s _ a _ a _ u s
Guesses Left: 4
a s _ a _ a _ u s
Letter Guess: p
You got a letter!
a s p a _ a _ u s
Guesses Left: 3
a s p a _ a _ u s
Letter Guess: r
You got a letter!
a s p a r a _ u s
Guesses Left: 2
a s p a r a _ u s
Letter Guess: g
You got a letter!
a s p a r a g u s
You figured out the word!
The word was:
asparagus
Running out of guesses:
Let's begin the game, you have 10 tries to get all the letters.
Input 1 letter that you think is in the word.
Hint: The word has 6 letters
Guesses Left: 10
_ _ _ _ _ _
Letter Guess: a
You got a letter!
_ _ _ a _ _
Guesses Left: 9
_ _ _ a _ _
Letter Guess: b
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 8
_ _ _ a _ _
Letter Guess: c
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 7
_ _ _ a _ _
Letter Guess: d
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 6
_ _ _ a _ _
Letter Guess: e
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 5
_ _ _ a _ _
Letter Guess: f
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 4
_ _ _ a _ _
Letter Guess: g
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 3
_ _ _ a _ _
Letter Guess: j
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 2
_ _ _ a _ _
Letter Guess: i
Whoops, thats not in the word, or was already found. Try again!
Guesses Left: 1
_ _ _ a _ _
Letter Guess: j
Whoops, thats not in the word, or was already found. Try again!
Sorry. You ran out of guesses!
The word was:
potato