C# tutorial — Basic Rock, Paper, Scissors game

Alex
6 min readApr 19, 2021

Hellooo, it has been a while since I have written an article (I swear I always say that) and I have been delving more into C# as of recently. So I thought it would be cool to show off basic programs I have been building in my spare time and hopefully people can take something from it :)

First things first. For this project I will use Rider, it is an interesting piece of software that is a C# IDE for Apple Mac users. I believe you can also create C# console applications using Visual Studio Code, however, that is not in the scope of this tutorial.

So in Rider I chose “console application” and once this has finished generating you will see a project that includes a file named “Program.cs”. Within this file there is a “Main” function, within this function create 3 variables. One with a type of “bool” named “gmeLoop” with a default value of true. Also, create 2 int variables one named “userPoints” with a default value of 0 and another named “computerPoints” with a default value of 0.

bool gameLoop = true;
int userPoints = 0;
int computerPoints = 0;

The gameLoop variable will be used within a while loop so the code within the loop will run as long as the variable “gameLoop” is true, else it will exit the loop and the code below it will run.

bool gameLoop = true;
int userPoints = 0;
int computerPoints = 0;
while (gameLoop)
{
}

In addition, we now need to write a few small messages inside of the “while” loop to the console that will explain the game and options available to the user.

Console.WriteLine("Hello and welcome to my Rock, Paper, Scissors game. Please enter a number below for your choice and press enter:");

Console.WriteLine("1.Rock");
Console.WriteLine("2.Paper");
Console.WriteLine("3.Scissors");

Our next step is to get the value that the user entered into the console. We can use Console.ReadLine(); for this and set the value to a variable.

string userChoice = Console.ReadLine();

After this we will create an instance of the Random object which we can use later so our variable that we will use as the computers chosen item is between 1,2 or 3 so it is the same as the numbers the user has to enter.

Random randomChoice = new Random();
int computerChoice = randomChoice.Next(1, 4);

We will use the “Next();” method of the Random class to set a min value of 1 and max value of 4.

We then start the main part of our game code. We will do a switch statement on the random value generated from the “computerChoice” variable and do cases of 1,2 and 3.

Inside each of these cases we will do an if statement to determine what value the user entered and depending on these 2 values evaluate whether the computer has earned a point or the user has.

switch (computerChoice)
{
case 1:
break;
case 2:break;case 3:
break;
}

So in case 1 (so the computer has chosen rock) as that is the equivalent to what the user will choose if they choose 1 (You could use an array instead which contains the actual values “rock”, “paper”, “scissors” or make the user enter the actual words so in the if statements we are comparing against the words and not numbers and that may make the project more readable) then the end result will be a tie as both players have chosen Rock so no players earn a point.

if (userChoice == "1")
{

//this is a tie
Console.WriteLine("User chose Rock");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("It is a tie.");
}

However, in case 1 if the user has chosen 2 i.e. the computer has chosen rock but the user has chosen paper then the user wins a point (As paper beats rock)

else if (userChoice == "2")
{
Console.WriteLine("User chose Paper");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("User wins");
userPoints++;
}

So in the above else if statement we write a small Console.WriteLine statement and also increment the userPoints variable.

else if (userChoice == "3")
{
Console.WriteLine("User chose Scissors");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("Computer wins");
computerPoints++;
}

Above is the example where the computer chooses rock but the user chooses scissors so the computer wins.

else if (userChoice == "3")
{
Console.WriteLine("User chose Scissors");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("Computer wins");
computerPoints++;
}

Below is the other 2 cases, the only differences really are where the userPoints and computerPoints variables increment using the “++” operator.

case 1:
if (userChoice == "1")
{

//this is a tie
Console.WriteLine("User chose Rock");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("It is a tie.");
}

else if (userChoice == "2")
{
Console.WriteLine("User chose Paper");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("User wins");
userPoints++;
}
else if (userChoice == "3")
{
Console.WriteLine("User chose Scissors");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("Computer wins");
computerPoints++;
}
break;
case 2:
if (userChoice == "1")
{

//this is a tie
Console.WriteLine("User chose Rock");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("It is a tie");
}

else if (userChoice == "2")
{
Console.WriteLine("User chose Paper");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("User wins");
userPoints++;
}
else if (userChoice == "3")
{
Console.WriteLine("User chose Scissors");
Console.WriteLine("Computer chose Rock");
Console.WriteLine("Computer wins");
computerPoints++;
}
break;
case 3: //computer chose scissors
if (userChoice == "1")
{

//this is a tie
Console.WriteLine("User chose Rock");
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("User wins.");
userPoints++;
}

else if (userChoice == "2")
{
Console.WriteLine("User chose Paper");
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("Computer wins");
computerPoints++;
}
else if (userChoice == "3")
{
Console.WriteLine("User chose Scissors");
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("It is a tie");
}
break;

After the basic game logic has been completed we will Console.WriteLine a few messages that asks the user if they want to play the game again.

Console.WriteLine("Do you wish to play again?");
Console.WriteLine("Enter Y or N");

We will again use Console.ReadLine() to contain what the user has inputted and use that in an if statement so if the user enters “N”, “n” or “no” in the console the game will then exit as we set the gameLoop variable to false and the computer/user points are shown.

if (playAgain == "N" || playAgain == "n" || playAgain=="no") 
{
gameLoop = false;
Console.WriteLine($"User has {userPoints} points - Computer has {computerPoints} points");
}

So with our basic game we have a while loop that will loop over the game logic as long as the gameLoop variable is true, a switch statement that is based on the randomly generated number which is the computers choice of rock, paper or scissors. Then depending on the if statements the user/computer points will increment. After this logic is complete the user is then asked if they would like to play again.

If they do not then the loop ends and the accumulated points are shown to the user.

If you compile the program now you should see the below.

Ways you could improve this program or make it more complicated include adding the infamous “lizard” and “spock” elements to this from the big bang theory, adding a database element to the program to be able to record the user/computer wins and then export this data to an excel file.

My blog is here (it is still in progress) — https://cowboycode.co.uk/ — I would love it if people could tell me there C# console projects they have made in the comments as sometimes I get stuck for ideas for projects apart from building text adventure games or even how they would improve the rock, paper, scissors game I’m sure there are multiple ways this could be done.

Thank you very much for reading my article I appreciate every clap, comment (whether positive or negative) and share, I hope you all have a fantastic day :)

--

--