C# tutorial — Guess the number game

Hi everyone, here I am with another article showing you how to develop a basic program in C#. This time I will show you how to create a Guess the Number game.

In this tutorial I am using Rider as my IDE. So you will need to create a C# console application on your chosen IDE.

Once you have done you should see a “static void main” method and this is where the majority of our code will go.

First things first, we need to create 4 variables. One which will contain the number the user is trying to guess between 1 and 50 you could set it to whatever, we will use the .Next() method and place a minimum and maximum value, another variable to contain the number of tries the user is allowed which in this case will be 10, thirdly, there will be a variable that contains the number of tries the user has made to guess the number, finally, the last variable will be a boolean variable to be used in the while loop.

int randomNumber = new Random().Next(1, 50);
int allowedTries = 10;
int numberOfTries = 0;
bool gameLoop = true;

The next step is to prompt the user to enter a number so we will write a simple Console.WriteLine.

Console.WriteLine("Please enter a number between 1 and 100...");

We then will create a while as we want this program to be looped as long as the user still has enough tries to guess the number, if the number of tries the user has made equals the max tries and the user has not guessed the number then the user loses.

So write the below

while (gameLoop)

We now need to write a method which will return the guess that the user has entered into console and wrap a try catch around it incase the user accidentally enters a character which is not a number.

So outside of the “static void main” method enter the below.

public static int GetUserGuess()
{

The method is static and return type is int.

Now inside of this method create an int variable named “guess” and set its default value to 0.

We will now need to write a “try catch” statement and inside the “try” set the variable “guess” equal to the variable the user has entered into the console and also convert that variable into an int.

We want to convert it to an int because we want the method to return that value, however, if the user entered a letter it wouldn’t be able to be converted and hence why we put this code inside a try catch so we can catch this error of the letter not being able to be converted to an int and add a catch incase this situation occurs.

public static int GetUserGuess()
{
int guess = 0;

try
{
guess = Convert.ToInt32(Console.ReadLine());
}
catch (Exception e)
{

}


}

Your code should now look like the above.

Inside the catch we can write a simple Console.WriteLine statement.

Console.WriteLine("You did not enter a valid guess");

You can even enter the error message returned.

Console.WriteLine($"Error: {e.Message}");

Also, we will set the guess variable equal to the return value of GetUserGuess which will be 0.

Outside of the try/catch we will now return the guess variable so our full method now looks like the below.

public static int GetUserGuess()
{
int guess = 0;

try
{
guess = Convert.ToInt32(Console.ReadLine());
}
catch (Exception e)
{
Console.WriteLine("You did not enter a valid guess");
Console.WriteLine($"Error: {e.Message}");
guess = GetUserGuess();
}

return guess;
}

Now back to our initial code inside “static void main” set the variable “guess” equal to the method “GetUserGuess”.

Since the user has now successfully had one try at guessing the number we need to increment the “numberOfTries” variable.

We will then do 4 different checks with the guess variable against the random number, we will first check if the guess the user entered is greater than the number. To do this we can use a simple if statement and print out a console message.

if (guess > randomNumber)
{
Console.WriteLine("Your guess was too high please try again!");
}

We will then do the same for if the guess was less and write a similar console message.

if (guess < randomNumber)
{
Console.WriteLine("Your guess was too low");
}

Furthermore, we will now do a check to see if the guess is equal to the random number generated and if so set gameLoop to false as we can now exit the loop.

if (guess == randomNumber)
{
Console.WriteLine("Your guess was correct!");
gameLoop = false;
}

We finally have to do a check to see if the number of tries variable is equal to the allowed tries and if so then exit the loop and write a message to show what the number was.

if (numberOfTries == allowedTries)
{
Console.WriteLine($"The number was: {randomNumber}");
gameLoop = false;
}

Finally, we will now write a console message that explains to the user how many tries they have left.

Console.WriteLine($"You have {allowedTries - numberOfTries} tries left. Please enter another number:");

Below are screenshots for how the program looks.

Thank you very much for reading my article I hope you have learned something from it or you just take the code and show off to your friends and family about the basic game haha!

I will try to look up and develop more complicated programs to write in a console application but it is a bit more difficult since there is GUI.

Have a blessed day and stay safe :)

Software Developer in the UK. With a keen passion for React Native & C#. Blog — https://cowboycode.co.uk/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store