S
shr1966
Guest
I am trying to program a game in C# that uses one array, to guess letters for a word in the second array. here is the code that I have so far and I am getting an out of bounds of the array exception when I try to run the code on line 34, underlined. any help would be greatly appreciated.
{
class Program
{
static void Main(string[] args)
{
//the word that the user will be guessing
char[] word = "holmes".ToCharArray();
char letter;
char[] guessed = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
int score = 0;
int index = 0;
//display greeting
Console.WriteLine("\n\nWelcome to Sam's incredible Hangman Game!");
//display message telling user to guess 10 letters
Console.Write("\n\nPick 10 letters to guess the word");
Console.Write("\n\n The word is ******");
for (int i = 0; i < 10; i++)
{
Console.WriteLine("\n\nEnter a letter please ");
letter = char.Parse(Console.ReadLine());
if (letter == word['h'] || letter == word['o'] || letter == word['l'] || letter == word['m'] || letter == word['e'] || letter == word['s'])
{
Console.WriteLine("\n\nYou Guessed correctly");
guessed[index] = letter;
index++;
}
else
{
Console.WriteLine("\n\nYou guessed incorrectly");
score++;
}
}
Console.WriteLine("\n\nYour Score is " + score);
Console.ReadLine();
}
}
}
Continue reading...
{
class Program
{
static void Main(string[] args)
{
//the word that the user will be guessing
char[] word = "holmes".ToCharArray();
char letter;
char[] guessed = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
int score = 0;
int index = 0;
//display greeting
Console.WriteLine("\n\nWelcome to Sam's incredible Hangman Game!");
//display message telling user to guess 10 letters
Console.Write("\n\nPick 10 letters to guess the word");
Console.Write("\n\n The word is ******");
for (int i = 0; i < 10; i++)
{
Console.WriteLine("\n\nEnter a letter please ");
letter = char.Parse(Console.ReadLine());
if (letter == word['h'] || letter == word['o'] || letter == word['l'] || letter == word['m'] || letter == word['e'] || letter == word['s'])
{
Console.WriteLine("\n\nYou Guessed correctly");
guessed[index] = letter;
index++;
}
else
{
Console.WriteLine("\n\nYou guessed incorrectly");
score++;
}
}
Console.WriteLine("\n\nYour Score is " + score);
Console.ReadLine();
}
}
}
Continue reading...