Creating a Guessing Game in C# and ASP.NET Core

Path2Pixelation
6 min readJan 23, 2021
a person writing a program

Hello, I expect that you are new to C# and ASP.NET Core or are wanting to simply brush up on your skills. This tutorial is going to teach you how to create a guessing game in C#.

You can change the subject of what the guess is about. In my case it is about guessing the video game title that the program is thinking of.

Feel free to check out my repo on GitHub here: https://github.com/mjs001/GuessingGame

First of all, if you have no familiarity with C# at all please check out this video on YouTube. https://www.youtube.com/watch?v=GhQdlIFylQ8&t=2979s

That video is highly informative. I am also a big fan of freecodecamp.

Alright, to begin lets create a new ASP.NET Core console app.

Make sure to do the C# one and not the VB one!

Now that we have initialized the project we will move onto creating the variables.

default console app

This is what should be showing right now. You will want to leave most of it alone but get rid of line 9. Now its time to start writing some code!

starting variables

We will start inside the curly brackets right below Main. This is where the main part of your program will lie. Just a quick tip, you can write comments in C# with //. You must start the comment with // and end the comment with // so it’d look like this: //my first comment!//

First we will write int guessCount = 0; so that we can initialize the variable for how many guesses the player has inputted. Then we start the maxGuesses variable. int maxGuesses = 8; you can set the number to however many guesses you want. Lastly, we create the string guess which is equal to a empty string. string guess = “”;

Alright, so we have created our first variables. Great, now to start outputting some text to the screen. Tip #2: If you want to check out the progress of the game of see what is being put on the screen run it! You can do this with the green button at the top of visual studio. In my case it says “Guess” because that is what my application is called.

console.writeline is similar to console.log in js!

To print text to the console you need to use Console.WriteLine(“<whatever text you want>”); So to start why not write what the video game is called? Another tip, \n creates a new line in the console. Then we write out the Rules. Feel free to adjust your rules to your particular game. Lastly, you can write that the game is starting.

Lets backtrack a little and write something at the top. We need to import some more modules for some methods we will create later.

underneath using System;

Underneath, using System; insert using System.Linq; and using System.Threading.Tasks;

Alrighty, next we will create our first method!

randomGameChooser() method

So, go out of the main method and create a new method called RandomGameChooser(). Essentially what this will do is pick a random game out of the array we are creating every time you start the program. Cool right?! Alrighty, so next create the string, return the string, create the games array, and a new Random object, then the index and finally define chosenGame.

all the variables

So what’s next? Well if you run the program its not going to know to run this method. So you need to run the RandomGameChooser method and save it in a variable to be used later. The other variables that are mentioned here will be created in a bit. Ignore the description string and totalNumOfWords integer for now.

Next, we are going to create the other two methods.

randomGamesDescription method

I have not included the full list of descriptions in this screenshot because it is too big. However, this should be enough for you to understand how to create the rest of them. Or if you so choose go to my github repo and copy it from there.

So what is this method doing? Well, it is returning the description of the game that is declared in the game string with a switch statement. So, roll up your sleeves and get typing!

Make sure to include this after you write all of your descriptions.

return statement

Okay second method down, one more to go.

whatlettersarecorrect method

I wont go into too much detail about this method but ultimately we are trying to figure out what letters the user has correct so far in the users response.

So we have three methods now. However, the user has not been able to add any of their responses yet. Wow, looks like its not a very good game. YET. We will get there. First, remember those variables I told you to ignore? Include those now.

all var

Next up is the while loop that will allow the user to input their response.

while loop

Basically we are saying if the guess string has the game string in it regardless of whether it is upper or lower case and the number of guesses does not equal the max number of guesses you win. If one of those or both are not true then keep running the loop. Console.ReadLine allows the user to input their response. So now run your program! It should look something like this:

YAYYY! We created a console game in C#!

Let me know what you all think of this tutorial. It is my first one! If you’d like to keep up with more programming related content check out my site at:

I am actively looking for work as a developer. If you would like to reach me about a job opportunity please message me on linked in or by email at mjane.sullivan.001@gmail.com. My linked in profile can be found here:

https://www.linkedin.com/in/miriam-sullivan/

ALL LINKS MENTIONED IN THIS TUTORIAL:

c# tut

path2pixelation.wordpress.com

--

--