I'm building a guess the word game that breaks the random word the program chooses into individual letters. The player guesses by the letter instead of the whole word. Now I need to find a way to put those letters together so they make the whole word so the program can check it and break the loop
string[] secretWord = new string[5];
secretWord[0] = "Pong";
secretWord[1] = "Crash";
secretWord[2] = "Joust";
secretWord[3] = "1942";
secretWord[4] = "Tron";
int guessedRight = 0;
string name = "";
string guess = "";
char letter = 'A';
Random random = new Random();
int answer = random.Next(secretWord.Length);
char[] brokenword = secretWord[answer].ToCharArray();
//Game Starts
Console.WriteLine("Enter your name");
name = Console.ReadLine();
//Game Loop
while (guess != secretWord[answer])
{
Console.WriteLine("Enter Your Letter, {0}", name);
foreach (char c in brokenword)
{
if (c == letter)
{
Console.Write(letter);
}
Console.Write("- ");
}
letter = Convert.ToChar(Console.Read());
}
Console.WriteLine("You Win!");
Console.ReadKey();
CodePudding user response:
create a list:
var wordBuilt = new List<string>()
inside your if statement you can simply add the letter inside the list that you created. At the end you just need to call the .Join() method from the list and write the results in console.
CodePudding user response:
I think you're missing some logic in your code, you don't ever update guess
You could do something like this:
string[] secretWord = new string[5];
secretWord[0] = "Pong";
secretWord[1] = "Crash";
secretWord[2] = "Joust";
secretWord[3] = "1942";
secretWord[4] = "Tron";
int guessedRight = 0;
string name = "";
string guessString = string.Empty;
char[] guessArray = null;
char letter = 'A';
Random random = new Random();
int answer = random.Next(secretWord.Length);
char[] brokenword = secretWord[answer].ToCharArray();
guessString = new string('_', brokenword.Length);
guessArray = guessString.ToCharArray();
//Game Starts
Console.WriteLine("Enter your name");
name = Console.ReadLine();
//Game Loop
while (guessString != secretWord[answer])
{
Console.WriteLine("Enter Your Letter, {0}", name);
for (int i = 0; i < brokenword.Length; i )
{
char c = brokenword[i]
if (c == letter)
{
Console.Write(letter);
guessArray[i] = c;
guessString = new string(guessArray)
}
Console.Write("- ");
}
letter = Convert.ToChar(Console.Read());
}
Console.WriteLine("You Win!");
Console.ReadKey();