vendredi 23 janvier 2015

How do I create a loop for a c# hangman game?

I've made this hangman game but the only problem is I can't figure out which simple loops (For,While, or Do While) I should use to make the program give the user three chances to answer correctly or keep on repeating if they do answer correctly. I was wondering if anyone could guide me in the right direction?



Console.WriteLine("Do you want to play a game?");
string answer = Console.ReadLine();

if (answer == "no")
{
Console.WriteLine("Please Exit Program");
}


System.Threading.Thread.Sleep(1000);
if (answer == "yes")
{
Console.WriteLine(" ");
Console.WriteLine("Welcome to Hangman");

string[] array1 = new string[10];
array1[0] = "hello";
array1[1] = "swami";
array1[2] = "zebra";
array1[3] = "rainbow";
array1[4] = "camp";
array1[5] = "unicycle";
array1[6] = "trivia";
array1[7] = "hockey";
array1[8] = "charlie";
array1[9] = "canada";

Random word = new Random();

int mynumber = word.Next(0, 10);

char[] array2 = array1[mynumber].ToCharArray();


char[] array3 = new char[array2.Length];

Console.WriteLine(array2);

for (int i = 0; i < array2.Length; i++)
{
array3[i] = '*';
}

Console.WriteLine(" ");
Console.WriteLine(array3);

System.Threading.Thread.Sleep(1500);

Console.WriteLine(" ");
int number = 1;
number = number + 1;

Console.WriteLine(" Guess a letter");
char letter = Char.Parse(Console.ReadLine());
Console.WriteLine(" ");

for (int i = 0; i < array2.Length; i++)
{
if (letter == array2[i])
{
Console.Write(letter);
}
else
{
Console.Write("*");
}
}

Console.WriteLine(" ");
}
}
}

Aucun commentaire:

Enregistrer un commentaire