Beginner C# Good Practices - c#

I'm just beginning in c# and I made a little program here. I'm just wondering if goto is a valid way of returning to certain sections of my code or if there is a more appropriate and practical way of doing it.
namespace Section5Tests
{
class Program
{
static void Main(string[] args)
{
Start:
var number = new Random().Next(1, 10);
int secret = number;
Console.WriteLine("Secret Number is between 1 and 10. ");
for (var i = 0; i < 10; i++)
{
Console.WriteLine("Guess the secret number you only have 3 attempts!");
Middle:
var guess = Convert.ToInt32(Console.ReadLine());
if (guess == secret)
{
Console.WriteLine("WoW! You got it! Well done!");
goto Playagain;
}
else
{
Console.WriteLine("Incorrect! Try again");
goto Middle;
}
}
Console.WriteLine("Sorry you lost =(");
Playagain:
Console.WriteLine("Try Again? Y/N");
var answer = Console.ReadLine();
if (answer.ToLower() == "y")
{
goto Start;
}
else
{
Console.WriteLine("Thankyou for playing =)");
}
}
}
}

In C#, a better way of doing things like this would be to refactor the program into individual methods which have unique and descriptive names. This is a much better solution than using goto in 99.9% of cases.
You generally never want all of your code to be in a single main method. Instead, I would refactor the game itself into its own method. Then, in the main loop, you can only check if the user is playing.
static void Main (string[] args)
{
var isPlaying = true;
while (isPlaying)
{
isPlaying = PlayGame();
}
Console.WriteLine("Thankyou for playing =)");
}
That way, you can have the PlayGame method return a bool to specify if the user is still playing. Instead of using goto, you can control the program's flow using check variables and smart coding:
static bool PlayGame ()
{
int number = new Random().Next(1, 10);
var userWon = false;
Console.WriteLine("Secret Number is between 1 and 10. ");
for (var numOfAttempts = 10; numOfAttempts > 0; numOfAttempts--)
{
Console.WriteLine($"Guess the secret number you only have {numOfAttempts} attempts!");
var guess = Convert.ToInt32(Console.ReadLine());
if (guess == number)
{
userWon = true;
break;
}
Console.WriteLine("Incorrect! Try again");
}
if (userWon)
Console.WriteLine("WoW! You got it! Well done!");
else
Console.WriteLine("Sorry you lost =(");
Console.WriteLine("Try Again? Y/N");
var answer = Console.ReadLine();
return answer.ToLower() == "y";
}

If you have to ask, you're not ready for GOTO. Don't use it. Use loops and functions instead.
To get you started, instead of Start:, use this:
string answer = "y";
while (answer = "y")
{
Of course you'll need to close that loop up down near your goto Start; statement, but this looks like coursework so I'll stop here. This should get you going.

wondering if goto is a valid way of returning to certain sections
of my code
No, it never is.
Only jumping forward out of deeply nested loops where a break won't go far enough, is valid. (And even that is frowned upon by many..)
All other uses are way too hard to understand when reading and debugging the code.
Instead write meaningful conditions in your loops! And try to concentrate on what you want to achieve, not how..
Breaking down your goals top down into small and simple chunks of work will help avoid coming even close to think about 'program flow' and helps thinking about 'solving problems' instead.

I wanted to be a bit different from the other answers. So, I have added if else statements inside your if statements to replace the goto() function. It's a bit of a long way of doing it but I hope it is easy enough to understand.
static void Main(string[] args)
{
var number = new Random().Next(1, 10);
int secret = number;
bool playAgain = true;
while (playAgain == true)
{
Console.WriteLine("Secret Number is between 1 and 10. ");
for (var i = 10; i > 0; i--)
{
Console.WriteLine("Guess the secret number you only have " + i + " attempts!");
var guess = Convert.ToInt32(Console.ReadLine());
if (guess == secret)
{
Console.WriteLine("WoW! You got it! Well done!");
Console.WriteLine("Would you like Play Again ? Y/N");
var play = Console.ReadLine();
if (play == "Y")
{
break;
}
else
{
playAgain = false;
break;
}
}
else if(i == 1)
{
Console.WriteLine("Sorry you lost =(");
Console.WriteLine("Try Again? Y/N");
var play = Console.ReadLine();
if (play == "Y")
{
break;
}
else
{
Console.WriteLine("Thankyou for playing =)");
playAgain = false;
}
}
}
}
}

Related

console application where the user has 5 tries to guess number between 1 and 100

I have created a console application where the user has 5 tries to guess number between 1 and 100. After 5 guesses the game ends, but I don’t know how to introduce at the 5th wrong intent something like “you have achieved maximum of guesses! The answer was number (X). I have tried different ways ,but is not working. This is my program
using System;
namespace Guessing_Game_4
{
class Program
{
static void Main(string[] args)
{
var number = new Random().Next(1, 100);
Console.WriteLine("Try and guess any number between 1-100. You have 5 guesses Max!");
for (var i = 0; i < 5; i++)
{
int guess = Convert.ToInt32(Console.ReadLine());
if (guess == number)
{
Console.WriteLine("You got it!");
break;
}
else
{
Console.WriteLine(guess + " is not correct! Try again!");
}
}
}
}
}
Here some Sample code
This might help
for( i=10;i>0 ; i--) {
System.out.println(" === you have " + i +" Guesses left ===");
int guess = scanner.nextInt();
if (random_number < guess) System.out.println("Smaller than guess " + guess);
if (random_number > guess) System.out.println("Greater than guess " + guess);
if (random_number == guess)
{
result = true;
break;
}
}
if (result)
{
System.out.println("You WON in "+(10-i) +" tries ");
System.out.println("******* CONGRATULATIONS **************************************");
System.out.println("*********************** YOU WON **********************");
}
else
{
System.out.println("The random number was "+random_number);
System.out.println("************************** OPPS You loose **************************************** ");
System.out.println("You are near it TRY Next time ************ GOOD LUCK ");
System.out.println("You are near it TRY Nexttime***********NEXTTIME********");
}
}
If that's all your program does, you can do the following trick. Print your message after the for loop, but now the problem is that you get the message in all cases. The trick is to return from the Main (instead of breaking the loop) on a correct guess:
Console.WriteLine("You got it!");
return;
If you've some other code to execute that returning from Main won't be a good solution, you can do the following:
Create a variable before the for loop. Let's call it isCorrectAnswer and set it to false in the beginning.
At the point where he answers correctly, set isCorrectAnswer to true before breaking the loop.
After the loop, check for that variable:
if (!isCorrectAnswer)
{
Console.WriteLine($"you have achieved maximum of guesses! The answer was number {number}.");
}
You have to have an int outside of your loop like this : int wrongAnswersCount = 0;
When the user enter a wrong number you
should add one unit to your variable wrongAnswersCount++;
In the start of the loop, you should check if the user reached the maximum amount of gueses or not, if yes break the loop and say the answer.
Your code will be something like this :
using System;
namespace Guessing_Game_4
{
class Program
{
static void Main(string[] args)
{
var number = new Random().Next(1, 100);
Console.WriteLine("Try and guess any number between 1-100. You have 5 guesses Max!");
int wrongAnswersCount = 0;
for (var i = 0; i < 5; i++)
{
if(wrongAnswersCount == 5)
{
Console.WriteLine($"you have achieved maximum of guesses! The answer was number {number}");
break;
}
int guess = Convert.ToInt32(Console.ReadLine());
if (guess == number)
{
Console.WriteLine("You got it!");
break;
}
else
{
Console.WriteLine(guess + " is not correct! Try again!");
wrongAnswersCount++;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
var number = new Random().Next(1, 100);
Console.WriteLine("Try and guess any number between 1-100. You have 5 guesses Max!");
for (var i = 0; i < 5; i++)
{
int guess = Convert.ToInt32(Console.ReadLine());
if (guess == number && i!=5)
{
Console.WriteLine("You got it!");
break;
}
else
{
Console.WriteLine(guess + " is not correct! Try again!");
}
}
Console.WriteLine(" the maximam guse");
}
}
//Try this one

Made a guessing game, but I think my code is weirdly put

The code works perfectly, but I am not sure if this is good practice or not. I think it could be improved and made easier, but I don't know how. Maybe I could have used more variables to make it simpler. Or is it fine as long as it works? I also need to work on tidying my code up.
using System;
namespace Lesson_number_16_Building_a_guessing_game
{
class Program
{
static void Main(string[] args)
{
string secretWord = "monkey";
string guess = "";
int count = 3;
Console.WriteLine("Please type an animal name that starts with the letter M, you have 3 guesses: ");
guess = Console.ReadLine();
if (guess == secretWord)
{
Console.WriteLine("Nice, you got it right.");
} else if (guess != secretWord)
{
while (guess != secretWord && count > 1)
{
count--;
Console.WriteLine("Nope, try again! you have " + count + " guesses left");
guess = Console.ReadLine();
if (guess == secretWord)
{
Console.WriteLine("Nice, you got it right.");
} else if (count == 1)
{
Console.WriteLine("You have failed to guess it. It was " + secretWord);
break;
}
}
}
}
}
}
1.Instead of initializing the string guess="", you can always prefer string guess=string.Empty.
2.Try to align the curly braces using keys like Ctrl + K.(It depends on Visual Studio version).
Here's a couple of things I would change:
Get rid of the magic number '1' breaking out of the loop. Declare it
as a const with a meaningful name.
since you're breaking out of the while loop within the while loop when validating the retrycount, there is no point to put the
condition in the while loop as well.
Combine the initial question within the loop as well and break out of the loop when correct.
Combine counter substraction and lowerbound check in one call.
Use the equals method for string comparison with the right culture.
using System;
namespace Lesson_number_16_Building_a_guessing_game
{
internal class Program
{
private static void Main(string[] args)
{
const string secretWord = "monkey";
int count = 3;
const int lowerGuessBound = 0;
Console.WriteLine("Please type an animal name that starts with the letter M, you have 3 guesses: ");
while (true)
{
if (Console.ReadLine().Equals(secretWord, StringComparison.CurrentCulture))
{
Console.WriteLine("Nice, you got it right.");
break;
}
else if (--count == lowerGuessBound)
{
Console.WriteLine($"You have failed to guess it. It was {secretWord}");
break;
}
Console.WriteLine($"Nope, try again! you have {count} guesses left");
}
}
}
}
I think that you can shorten and tidy-up your code to something like:
const string secretWord = "monkey";
string guess = "";
const int count = 3;
Console.WriteLine($"Please type an animal name that starts with the letter M, you have {count} guesses: ");
while(count>0)
{
guess = Console.ReadLine();
count--;
if(guess != secretWord )
{
Console.WriteLine($"Nope, try again! you have {count} guesses left");
}
else
{
count=0;
}
}
if(guess != secretWord )
{
Console.WriteLine($"You have failed to guess it. It was {secretWord}");
}
else
{
Console.WriteLine("Nice, you got it right.");}
}
Using const declaration helps to protect against a code error that changes a value that shouldn't. It can also be used on variable input for functions/methods.
I try not to use == where possible as its easy to type = by mistake.
The string interpolation character $ makes for easier understanding of strings that include parameter values when reading code.

How to ignore a line/block of code if the condition is met?

Is there a way to ignore a line/block of code if the condition is met?
I'm doing a C# .NET tutorial, and the application is a number guessing game.
I added a hint option if the user enters a wrong number (else if part):
// While guess is not correct
while (guess != correctNumber)
{
//Get users input
string input = Console.ReadLine();
// Make sure it's a number
if (!int.TryParse(input, out guess))
{
// Print error message
PrintColorMessage(ConsoleColor.Red, "Please use an actual number");
// Keep going
continue;
}
// Cast to int and put in guess
guess = Int32.Parse(input);
// Check if guess is close to correct number
if(guess == correctNumber + 2 || guess == correctNumber - 2)
{
// Tell the user that he is close
PrintColorMessage(ConsoleColor.DarkCyan, "You are close!!");
}
// Match guess to correct number
else if (guess != correctNumber)
{
// Print error message
PrintColorMessage(ConsoleColor.Red, "Wrong number, please try again");
AskForAHint(correctNumber);
}
}
// Print success message
PrintColorMessage(ConsoleColor.Yellow, "You are CORRECT!");
Basically I am asking a user if he wants a hint, and if he writes Y, the hint will be displayed. However, is there an option to display this question only once since this if statement is included in a while loop?
It would be annoying if "Do you want a hint?" question keeps displaying even if the user says Y.
My AskForAHint function:
static void AskForAHint(int num)
{
// Ask user if he wants a hint
Console.WriteLine("Do you want a hint? [Y/N]");
// Take his answer
string ans = Console.ReadLine().ToUpper();
// If the user wants a hint
if (ans == "Y")
{
// First hint number
int beginning = (num - num % 10);
// Second hint number
int finish = beginning + 10;
// Give user a hint
Console.WriteLine("The correct number is somewhere betweer {0} and {1}", beginning, finish);
}
else if (ans == "N")
{
return;
}
}
Thanks
Another way to do it would be to make the number of hints configurable (allowing the caller to specify how many hints they want to let the user ask for), and then keep track of the number of hints given in the method itself.
This would require a slight change to the AskForAHint method, however, since we don't know if the user answered "Y" or "N" to the hint question. Since AskForHint has no return value, we could have it return a bool that indicates how the user responded to the question:
static bool AskForAHint(int num)
{
var answer = GetUserInput("Do you want a hint? [Y/N]: ", ConsoleColor.Yellow);
if (!answer.StartsWith("Y", StringComparison.OrdinalIgnoreCase))
{
return false;
}
var beginning = num - num % 10;
var finish = beginning + 10;
Console.WriteLine($"The correct number is somewhere between {beginning} and {finish}");
return true;
}
Now we can keep track of how many hints the user has received by incrementing a counter in our "Game" method:
// Only ask for a hint if they have any hints (and guesses) remaining
if (hintCount < maxHints && guessCount < maxGuesses)
{
// If they asked for a hint, increase the hint count
if (AskForAHint(correctNumber)) hintCount++;
// If they didn't want a hint, max out hint count so we don't ask again
else hintCount = maxHints;
}
To test out the sample code above, I used this method below, which also allows us to configure how many total guesses the user has, what the min and max values of the range should be, and if they should be given a "directional hint", like "too high!" or "too low!":
private static readonly Random Random = new Random();
private static void PlayGuessingGame(int maxHints = 1, int maxGuesses = 10,
int rangeMin = 1, int rangeMax = 100, bool giveDirectionalHint = true)
{
if (rangeMax < rangeMin) rangeMax = rangeMin;
var correctNumber = Random.Next(rangeMin, rangeMax + 1);
var guessCount = 0;
var hintCount = 0;
WriteMessage("Welcome to the guessing game!", ConsoleColor.White);
WriteMessage("-----------------------------\n", ConsoleColor.White);
WriteMessage($"I'm thinking of a number from {rangeMin} to {rangeMax}. ", ConsoleColor.Green);
WriteMessage("Let's see how many guesses it takes you to guess it!\n", ConsoleColor.Green);
do
{
WriteMessage($"(You have {maxGuesses - guessCount} guesses left)");
var input = GetUserInput("Enter the number I'm thinking of: ", ConsoleColor.White);
int guess;
if (!int.TryParse(input, out guess))
{
WriteMessage("Please enter a whole number", ConsoleColor.Red);
continue;
}
// Only increment guesses if they entered an actual number
guessCount++;
if (guess == correctNumber) break;
if (Math.Abs(guess - correctNumber) == 2)
{
WriteMessage("You are close!!", ConsoleColor.DarkCyan);
}
if (giveDirectionalHint)
{
WriteMessage("Wrong number - too " + (guess < correctNumber ? "low!" : "high!"),
ConsoleColor.Red);
}
else
{
WriteMessage("Wrong number, please try again", ConsoleColor.Red);
}
// Only ask for a hint if they have any hints (and guesses) remaining
if (hintCount < maxHints && guessCount < maxGuesses)
{
// If they asked for a hint, increase the hint count
if (AskForAHint(correctNumber)) hintCount++;
// If they didn't want a hint, max out hint count so we don't ask again
else hintCount = maxHints;
}
} while (guessCount < maxGuesses);
WriteMessage("You are CORRECT!", ConsoleColor.Yellow);
GetKeyFromUser("\nDone! Press any key to exit...");
}
This uses the helper functions:
public static void WriteMessage(string message, ConsoleColor color = ConsoleColor.Gray)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
}
private static string GetUserInput(string prompt, ConsoleColor color = ConsoleColor.Gray)
{
Console.ForegroundColor = color;
Console.Write(prompt);
Console.ResetColor();
return Console.ReadLine();
}
Output
You can see in the output below, I was only given a single hint. However that, combined with the directional hints, made the game easy to win:
I think you can do an "if" with a counter.
Try It
Int cont = 0; //global
// While guess is not correct
while (guess != correctNumber)
{
//Get users input
string input = Console.ReadLine();
// Make sure it's a number
if (!int.TryParse(input, out guess))
{
// Print error message
PrintColorMessage(ConsoleColor.Red, "Please use an actual number");
// Keep going
continue;
}
// Cast to int and put in guess
guess = Int32.Parse(input);
// Check if guess is close to correct number
if(guess == correctNumber + 2 || guess == correctNumber - 2)
{
// Tell the user that he is close
PrintColorMessage(ConsoleColor.DarkCyan, "You are close!!");
}
// Match guess to correct number
else if (guess != correctNumber)
{
// Print error message
PrintColorMessage(ConsoleColor.Red, "Wrong number, please try again");
if(cont == 0){
AskForAHint(correctNumber);
}
}
}
// Print success message
PrintColorMessage(ConsoleColor.Yellow, "You are CORRECT!");
And in the function add
static void AskForAHint(int num)
{
// Ask user if he wants a hint
Console.WriteLine("Do you want a hint? [Y/N]");
// Take his answer
string ans = Console.ReadLine().ToUpper();
// If the user wants a hint
if (ans == "Y")
{
cont = 1;
// First hint number
int beginning = (num - num % 10);
// Second hint number
int finish = beginning + 10;
// Give user a hint
Console.WriteLine("The correct number is somewhere betweer {0} and {1}", beginning, finish);
}
else if (ans == "N")
{
return;
}
}
Use a Member Variable Boolean, its similar to how you can avoid recursive calls.
private bool alreadyHinted = false;
static void AskForAHint(int num)
{
if (alreadyHinted) return;
alreadyHinted = true;
At some point you will need to set alreadyHinted back to false;

How do I allow the user more attempts as opposed to just restarting the game after catching a format exception?

So, I'm building this little guessing game and I recently added a try/catch to stop the console from crashing if the user inputted a wrong character (besides numbers of course). Right now it works well but it restarts the entire game, I've tried to just have the catch re-run the script starting at the "for" loop, but it breaks and the random number originally generated doesn't match.
public static void StartGame()
{
Console.Clear();
Random ran = new Random();
decimal returnValue = (ran.Next(1, 100));
Console.WriteLine("Welcome to my guessing game. I'm thinking of a number between 1 and 100, can you guess it?");
Console.WriteLine("Hint: 'Way too off' means you're greater than 25 from the answer!");
try
{
for (int guessnumber = 1; guessnumber < 6; guessnumber++)
{
var Guess = Convert.ToInt32(Console.ReadLine());
//The Guess variable is simply whatever the user inputs, which is read and used by the for loop each time it is inputed.
if (Guess == returnValue)
{
Console.WriteLine("Well done!");
Console.ReadKey();
Game.NewGame();
}
else if (Guess < returnValue)
{
Console.WriteLine("Guess higher!");
}
else if (Guess > returnValue)
{
Console.WriteLine("Guess lower!");
}
if (guessnumber == 5)
{
Console.WriteLine($"The correct answer was {returnValue}");
Console.ReadKey();
Game.NewGame();
//This if statement activates when the "guessnumber" counter has reached 5, in which case it will tell the user the correct answer, and end with a return statement.
}
//result is the absolute value of
decimal result = Math.Abs(returnValue - Guess);
if (result > 25)
Console.WriteLine("Way too off!");
if (result < 5)
{
Console.WriteLine("Very close!!");
}
if (result < 2)
{
Console.WriteLine("RED HOT!!!");
}
}
}
catch (System.FormatException)
{
Console.WriteLine("Incorrect character inputted, restarting game. . .");
System.Threading.Thread.Sleep(2000);
StartGame();
}
}
public static void NewGame()
{
Console.Clear();
string input = " ";
Console.WriteLine("Game over, would you like to play again? Y/N");
input = Console.ReadLine();
input = input.ToUpper();
if (input == "Y")
{
Console.WriteLine("Ok! Re-Initializing game...");
StartGame();
}
else
{
System.Environment.Exit(0);
}
}
}
}
As another answer stated, you can just move your try/catch inside the for loop, so the loop continues.
But you really shouldn't be relying on exception handing for something that isn't exceptional, like parsing user input.
Instead, we can use the int.TryParse method to try to parse a string to an int. The nice thing about this method is that it returns true if the parse was successful, and it sets an out parameter to the parsed value.
We can also put it in it's own method that takes in a string to be used to prompt the user, which makes the code more reusable:
public static int GetIntFromUser(string prompt)
{
int result;
bool askedOnce = false;
do
{
if (askedOnce) Console.WriteLine("Invalid input, please try again.");
Console.Write(prompt);
askedOnce = true;
} while (!int.TryParse(Console.ReadLine(), out result));
return result;
}
Now our main code is greatly simplified (also notice that the Random instance should be a class field, not created on every call to StartGame, and notice that the upper bound specified in the call to ran.Next is exclusive, so if you want to include 100 as a possible answer, make the max value 101):
private static Random ran = new Random();
public static void StartGame()
{
Console.Clear();
int randomNumber = ran.Next(1, 101);
Console.WriteLine("Welcome to my guessing game");
Console.WriteLine("I'm thinking of a number between 1 and 100, can you guess it?");
for (int guessCount = 1; guessCount < 6; guessCount++)
{
var guess = GetIntFromUser($"Guess a number (try #{guessCount}): ");
if (guess == randomNumber)
{
Console.WriteLine("Well done!");
Console.ReadKey();
Game.NewGame();
}
else if (guessCount == 5)
{
Console.WriteLine("That was your last guess.");
Console.WriteLine($"The correct answer was: {randomNumber}.");
Console.ReadKey();
Game.NewGame();
}
else
{
decimal result = Math.Abs(randomNumber - guess);
if (result > 25) Console.Write("You're way off! ");
else if (result < 5) Console.Write("Very close!! ");
else if (result < 2) Console.WriteLine("You're RED HOT!!! ");
Console.WriteLine(guess < randomNumber ? "Guess higher!" : "Guess lower!");
}
}
}
Move the Try Catch handling inside for loop. What this does is, that exceptions are handled during each guess iteration.
What you had originally was exception handling for all iterations.
Also, Dont call the StartGame Function again in your catch block. You want to allow 6 (per the for loop) Guesses and not Infinite.
Ex:
for (int guessnumber = 1; guessnumber < 6; guessnumber++)
{
try
{
}
catch (System.FormatException)
{
}
}
The simple answer is as follows:
You don't control application flow with exception handling. Catching exceptions are for exceptional circumstances, a user getting input wrong isn't exceptional... it's guaranteed.
Never ever ever ever ever (+1000) use Convert.... or int.Parse to take user input... Users make mistakes all the time, use the TryParse type methods instead, they return a bool to say if the data was successfully parsed so you can then deal with it.
Eg
var success = int.TryParse(Console.ReadLine(), out var guess);
However, you can take it one step further, and add a loop (add pepper and salt to taste)
int guess = 0;
while(!int.TryParse(Console.ReadLine(), out guess))
Console.WriteLine("Omg you had one job, just enter a number");
Additional Resources
Int32.TryParse Method
Converts the string representation of a number to its 32-bit signed
integer equivalent. A return value indicates whether the operation
succeeded.

How to allow user to try again or quit console program in C#?

I have created a game that gives a player 5 chances to play after which I would like to ask the player if they would like to play again or quit. I have seen it done in Python, but I do not know python. My code works perfectly fine, but I would like to add these two additional functions
How can I achieve these functionality in C#?
For reference this is what my code main class code looks like.
namespace NumBaseBall
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\t\t\t*************************************");
Console.WriteLine("\t\t\t* Let's Have Some Fun *");
Console.WriteLine("\t\t\t* Welcome To The *");
Console.WriteLine("\t\t\t* Number Baseball Game *");
Console.WriteLine("\t\t\t*************************************\n");
GameResults gameresults = new GameResults();
for (int trysCounter = 1; trysCounter <= 5; trysCounter++)
{
gameresults.Strikes = 0;
Random r = new Random();
var myRange = Enumerable.Range(1, 9);
var computerNumbers = myRange.OrderBy(i => r.Next()).Take(3).ToList();
Console.WriteLine("The Game's Three Random Integers Are: (Hidden from user)");
foreach (int integer in computerNumbers)
{
Console.WriteLine("{0}", integer);
}
List<int> playerNumbers = new List<int>();
Console.WriteLine("Please Enter Three Unique Single Digit Integers and Press ENTER after each:");
for (int i = 0; i < 3; i++)
{
Console.Write("");
int number = Convert.ToInt32(Console.ReadLine());
playerNumbers.Add(number);
}
gameresults.StrikesOrBalls(computerNumbers,playerNumbers);
Console.WriteLine("---> Computer's Numbers = {0}{1}{2}", computerNumbers[0], computerNumbers[1], computerNumbers[2]);
Console.WriteLine("---> Player's Numbers = {0}{1}{2}", playerNumbers[0], playerNumbers[1], playerNumbers[2]);
Console.WriteLine("---> Game Results = {0} STRIKES & {1} BALLS\n", gameresults.Strikes, gameresults.Balls);
Console.WriteLine("You have played this games {0} times\n", trysCounter);
gameresults.TotalStrikes = gameresults.TotalStrikes + gameresults.Strikes;
Console.WriteLine("STRIKES = {0} ", gameresults.TotalStrikes);
if (gameresults.TotalStrikes >= 3)
{
gameresults.Wins++;
Console.WriteLine("YOU ARE A WINNER!!!");
break;
}
}
if (gameresults.TotalStrikes <3)
Console.WriteLine("YOU LOSE :( PLEASE TRY AGAIN!");
}
}
}
Insert your code inside an loop which checks if the user wants to continue:
while(true) // Continue the game untill the user does want to anymore...
{
// Your original code or routine.
while(true) // Continue asking until a correct answer is given.
{
Console.Write("Do you want to play again [Y/N]?");
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
break; // Exit the inner while-loop and continue in the outer while loop.
if (answer == "N")
return; // Exit the Main-method.
}
}
But perhaps it would be better to split one big routine up into seperate routines.
Lets rename your Main-method to PlayTheGame.
Split up my routines into:
static public bool PlayAgain()
{
while(true) // Continue asking until a correct answer is given.
{
Console.Write("Do you want to play again [Y/N]?");
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
return true;
if (answer == "N")
return false;
}
}
And now the Main-method can be:
static void Main(string[] args)
{
do
{
PlayTheGame();
}
while(PlayAgain());
}
You'de have to move some local variables to the class as static fields. Or you could make an instance of a Game class, but I think that is one step to far right now.
There are two ways you could accomplish this:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill%28v=vs.110%29.aspx
System.Diagnostics.Process.GetCurrentProcess().Kill();
Or
https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx
int exitCode =1;
System.Environment.Exit(exitCode);
Environment.Exit is the preferred way to exit your program since the Kill command "causes an abnormal process termination and should be used only when necessary."[msdn]
First, take the suggestion of moving the code for the actual game playing into a separate function. It will clean things up a lot.
Something like
private static bool PlayGame()
{
// Win branch returns true.
// Loss branch returns false.
}
This then lets you greatly simplify the Main function allowing it to only handle the menu functionality.
For the actual menu functionality, I tend to prefer do/while loops. You have a bit of an extra stipulation that you only ask after 5 plays, but that's easy enough to deal with.
static void Main(string[] args)
{
int playCount = 0;
string answer = "Y";
bool winner;
do
{
if(playCount < 5)
{
playCount++;
}
else
{
do
{
Console.Write("Play again? (Y/N): ");
answer = Console.ReadLine().ToUpper();
} while(answer != "Y" && answer != "N");
}
winner = PlayGame();
} while(!winner && answer == "Y");
Console.WriteLine("Thanks for playing!");
}
You could simplify it a bit by moving the test for 5 games into the if conditional with the use of an increment operator. The only issue is if someone plays your game a billion or so times, things might get weird.
static void Main(string[] args)
{
int playCount = 0;
string answer = "Y";
bool winner;
do
{
if(playCount++ > 3)
{
do
{
Console.Write("Play again? (Y/N): ");
answer = Console.ReadLine().ToUpper();
} while(answer != "Y" && answer != "N");
}
winner = PlayGame();
} while(!winner && answer == "Y");
Console.WriteLine("Thanks for playing!");
}
Edit: Changed things a bit as it looks like in your original code the game ends after the person wins the game.
Per your question in your comment below, you could make a static instance of your GameResults class in your Program class. Your code would end up looking something like the following
class Program
{
private static GameResults results = new GameResults();
public static void Main(string[] args)
{
// Code
}
private static bool PlayGame()
{
// Code
}
}
In PlayGame you would just use the static results object instead of creating a new one every time PlayGame is called.

Categories