Jeremy Shanks
Spread the Knowledge!

How to Create a Number Guessing Game in C#

In today’s tutorial, we are going to make a number guessing game in C#. We will learn how to generate random numbers, get user input, and decide if the user guesses the correct number. This tutorial will be relatively short compared to some of my other tutorials.

Requirements

Difficulty: Easy

We want to create a console application that will generate a random number between 1 and 100. Then we want to let the user guess what the number is. When the user guesses, we want to tell them if they are correct or incorrect. If their guess is incorrect, we want to inform the user if they guessed too high or too low. We want to let the user guess again until they get it correct or until they reach the maximum allowed tries. The maximum allowed tries will be 5.

Let’s Get Started

To get started, we need to create an empty C# console application in Visual Studio. For more information on how to do this, check out my how-to guide here.

Generate Random Number

The first thing we want to do is generate a random number between 1 and 100. This is very easy to do in C#. We create an integer called “randomNumber” and assign it to the return of Random.Next. The next method takes two arguments, the minimum and maximum value.See how to do this below.

int randomNumber = new Random().Next(1, 100);

Insert the above code in the Main method of our console application.

Get Number From User

Now we want to get a guess from the user. First, in the main method, we are going to ask the user to guess a number between 1 and 100. Then we are going to create a new static method named “GetGuess” to get the number from the user and return it.See the example below.

In the above code, we are creating a static method that returns an integer. In this method, we are initializing an integer “guess” to be equal to 0. This is done to keep the compiler from complaining and failing to build. Then we have a try-catch block. Inside the try-catch block, we are getting the input from the user with Console.ReadLine() and then Converting that to an integer and assigning it to the variable “guess.” If that fails, the code in the catch block will execute. Here we will tell the user “You did not enter a valid guess” and then call this same method so that the user can try again. In the end, we will return “guess.”

Evaluate Guess

Now that we have a way to get the user’s guess we need to determine if the user guesses correctly. If they did not guess correctly, then we tell them if they guessed too high or too low and then let them guess again until they reach the maximum allowed tries. To create this loop of guesses, we will use a while loop and set the condition to “true” which will create an infinite loop. We will then use breaks to break out of the loop when certain conditions are met. Take a look at the following code to see all of this in action.

In the above code, you see that we are first creating 2 variables, allowedTries and numberOfTries. We set the allowedTries variable to 5 and the numberOfTries variable to 0.

Next, we are creating the infinite loop to put the conditions.

Inside the while loop, we are first getting the guess from the user and assigning the guess to a variable guess. Then we are incrementing the numberOfTries variable by 1.

Now comes the conditionals:

  • If the guess is equal to the random number, tell the user they are correct and break out of the loop. The game is over.
  • If the guess is greater than the random number, tell the user they guessed too high.
  • If the guess is less than the random number, tell the user they guessed too low.
  • If the numberOfTries variable is equal to the allowedTries variable, show the user the random number and break out of the loop. The game is over.

Lastly, we are telling the user they have “{allowedTries – numberOfTries}” left. This is evaluated to be the number of tries the user has left.

See it in Action

Now when we run the application, we will be able to guess a random number between 1 and 100 until we get it correct or we run out of tries.

number-guessing-game

Congratulations! You have a working number guessing game. You can now play this game over and over for hours of fun. Not really, but I hope you enjoyed creating it as much as I did.

Remember I am always learning, so if you see a mistake or know a better way of doing something I have shown here, please let me and others know in the comments below. Thanks for reading.

About the Author Jeremy Shanks

Hi, my name is Jeremy Shanks. I am a Software Engineer in Raleigh, NC. Recently I embarked on the journey of teaching others how to develop projects in C#. Be sure to check them out if you are new to C#.

follow me on:
>