After debugging, I find myself in a "loop" of sorts. The program is not recognizing the letters in the gameWord enough, to compare to the letters inputed. Could someone please tell me what I've done wrong.
Thanks.
using System;
namespace Hangman
{
class Program
{
const string letters = "abcdefghijklmnopqrstuvwxyz";
static readonly string[] wordBank = {
"fishing","computer","tree","technician","location","work","butterfly","toddler","flowers"
};
static void Main(string[] args)
{
string gameWord;
bool[] boolLetters = new bool[26];
char[] wordArray;
string letters = " abcdefghijklmnopqrstuvwxyz";
// initializing all letters to false
boolLetters = new bool[26];
for (int i = 0; i < boolLetters.Length; i++)
{
boolLetters[i] = false;
}
// set wordArray to gameWord
wordArray = new char[1] { ' ' };
//displaying the game layout
Console.WriteLine(" ~*~*~*~*~*~*~*~*~*~*~*~ ");
Console.WriteLine(" ~*~Welcome to Hangman!~*~ ");
Console.WriteLine(" ~*~*~*~*~*~*~*~*~*~*~*~ ");
//Ask user to Enter Name
Console.Write("Please enter your name: ");
string Name = Convert.ToString(Console.ReadLine());
//display user greeting message
Console.WriteLine(Name + ", Welcome to Hangman");
Console.WriteLine(" ~*~*~*~*~*~*~*~*~*~*~*~ ");
Console.WriteLine(" ~*~*~Hangman!~*~*~ ");
Console.WriteLine(" ~*~*~*~*~*~*~*~*~*~*~*~ ");
Console.WriteLine();
// creates a random number to choose a word from word bank
Random random = new Random();
int randomNumber = random.Next(wordBank.Length);
//selects the word from the wordBank using randomNumber
gameWord = wordBank[random.Next(wordBank.Length)];
// wordArray is set to wordArray
wordArray = new char[gameWord.Length];
wordArray = gameWord.ToCharArray();
string displayWord;
bool fFound;
int cFound;
displayWord = "*";
displayWord = displayWord.PadRight(gameWord.Length, '*');
cFound = 0;
while (cFound < gameWord.Length)
{
Console.WriteLine("The word is {0}", displayWord);
bool repeat = false;
char guess;
do
{
Console.Write("Please enter a new letter: " );
guess = Console.ReadKey().KeyChar;
if (letters.IndexOf(guess) != -1)
{
guess = char.ToUpper(guess);
repeat = false;
}
else
{
Console.WriteLine("You can only enter letter values, please re-enter choice.");
repeat = true;
}
fFound = false;
for (int i = 0; i < gameWord.Length; i++)
{
if (gameWord[i] == guess)
{
displayWord = Convert.ToString(guess);
fFound = true;
cFound++;
}
}
if (fFound)
Console.WriteLine("Letter found");
else
Console.WriteLine("Letter not found");
} while (repeat);
}
}
}
}