Reputation: 141
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(" ");
}
}
}
Upvotes: 0
Views: 210
Reputation: 218798
Step back from the details of the code for a moment and consider the logic of the game and how the loop would be expressed in human language.
You essentially have two loops:
If we were to nest those loops within each other in human language pseudo-code, it might look like this:
while (player wishes to play again)
playGame()
and within playGame()
:
while (game isn't over)
playTurn()
If you already have the code to conduct a "turn" of the game (the player guesses, the guess is determined to be correct or incorrect, etc.) then all you really need to do is define those conditions.
game isn't over
You can maintain a simple boolean flag for this. Something like:
bool gameIsOver = false;
On any given turn, check the logic of the game. Has the player won? Display a message and set the flag to true
. Has the player lost? Same response, just a different message. So the loop ends up being something like:
bool gameIsOver = false;
while (!gameIsOver)
{
// perform turn logic
// when a turn ends the game, set gameIsOver to true
}
The same concept can then be applied to the outer loop. That one needs to define:
player wished to play again
So you might structure it something like:
bool continueWithAnotherGame = true;
while (continueWithAnotherGame)
{
// play a game
// ask the user if they would like to play again
// set continueWithAnotherGame based on the user's reply
}
The physical keywords used to construct the loop become a lot more straightforward once the semantic structure of the loop has been defined. From there you can focus more on the details. For example, you say that a game has 3 "strikes". That doesn't necessitate the use of a counter within a for
loop, you can use a separate counter. Something like:
bool gameIsOver = false;
int strikes = 0;
while (!gameIsOver)
{
// perform turn logic
// if the turn is a fail, increment strikes
// if strikes is greater than or equal to 3, set gameIsOver to true
// else if the turn is a pass, continue
// if the game is solved, set gameIsOver to true
}
Upvotes: 3