Hello I'm new to this and I'd love some help here ,could anyone show me how to check if input is string so it doesn't crash if I enter a let's say a number instead. and other improvements are welcome as well. A big piece of the code is below, thanks in advance!
string[] wordBank = { "Red","Orange","Blue", "Black", "Yellow", "Orange", "Green", "Purple" };
string wordToGuess = wordBank[random.Next(0, wordBank.Length)];
string wordToGuessUppercase = wordToGuess.ToUpper();
StringBuilder displayToPlayer = new StringBuilder(wordToGuess.Length);
for (int i = 0; i < wordToGuess.Length; i++)
displayToPlayer.Append('_');
List<char> correctGuesses = new List<char>();
List<char> incorrectGuesses = new List<char>();
int lives = 5;
bool won = false;
int lettersRevealed = 0;
string input;
char guess;
while (!won && lives > 0)
{
Console.Write("Guess a letter: ");
input = Console.ReadLine().ToUpper();
guess = input[0];
if (correctGuesses.Contains(guess))
{
Console.WriteLine("You've already tried '{0}', and it was correct!", guess);
continue;
}
else if (incorrectGuesses.Contains(guess))
{
Console.WriteLine("You've already tried '{0}', and it was wrong!", guess);
continue;
}
if (wordToGuessUppercase.Contains(guess))
{
correctGuesses.Add(guess);
for (int i = 0; i < wordToGuess.Length; i++)
{
if (wordToGuessUppercase[i] == guess)
{
displayToPlayer[i] = wordToGuess[i];
lettersRevealed++;
}
}
if (lettersRevealed == wordToGuess.Length)
won = true;
}
else
{
incorrectGuesses.Add(guess);
Console.WriteLine("Nope, there's no '{0}' in the word!", guess);
lives--;
}
Console.WriteLine(displayToPlayer.ToString());
}
if (won)
{
Console.WriteLine("You won!");
Console.WriteLine("Congratz! ");
}
else
{
Console.WriteLine("You lost! It was '{0}'", wordToGuess);
}
DateTime myValue = DateTime.Now;
Console.WriteLine(myValue.ToString());
Console.Write("Want to play again? press 0 to continue and another random number to exit!");
p = Convert.ToInt16(Console.ReadLine());
}
Console.ReadLine();
}
}