Home ยป Rock-Paper-Scissors console application

Rock-Paper-Scissors console application

by wskandot71

Here’s a simple and interactive Rock-Paper-Scissors console application in C#, along with a line-by-line explanation of how it works.

The game will let a user play against the computer until they choose to quit.

Features

  • The user selects Rock, Paper, or Scissors.
  • The computer randomly selects a move.
  • The winner is determined using standard game rules.
  • The game continues in a loop until the user types “quit”.

Full Code: RockPaperScissors.cs

using System;

class RockPaperScissors
{
    static void Main()
    {
        Console.WriteLine("Welcome to Rock-Paper-Scissors!");
        Console.WriteLine("Type rock, paper, or scissors to play. Type quit to exit.");

        string[] options = { "rock", "paper", "scissors" };
        Random random = new Random();

        while (true)
        {
            Console.Write("\nEnter your move: ");
            string userInput = Console.ReadLine().ToLower();

            if (userInput == "quit")
            {
                Console.WriteLine("Thanks for playing!");
                break;
            }

            // Validate user input
            if (Array.IndexOf(options, userInput) == -1)
            {
                Console.WriteLine("Invalid input. Please enter rock, paper, or scissors.");
                continue;
            }

            // Computer's move
            string computerMove = options[random.Next(options.Length)];
            Console.WriteLine("Computer chose: " + computerMove);

            // Determine the result
            if (userInput == computerMove)
            {
                Console.WriteLine("It's a tie!");
            }
            else if ((userInput == "rock" && computerMove == "scissors") ||
                     (userInput == "paper" && computerMove == "rock") ||
                     (userInput == "scissors" && computerMove == "paper"))
            {
                Console.WriteLine("You win!");
            }
            else
            {
                Console.WriteLine("You lose!");
            }
        }
    }
}

Code Breakdown

using System;

  • Brings in the base system library to allow input/output operations and other core features.

string[] options = { “rock”, “paper”, “scissors” };

  • This array stores the three valid options that the user and computer can choose.

Random random = new Random();

  • A Random object is used to randomly select a move for the computer.

while (true)

  • The game runs continuously in a loop until the player decides to exit.

string userInput = Console.ReadLine().ToLower();

  • Takes user input and converts it to lowercase for consistent comparison.

if (userInput == “quit”)

  • If the user enters “quit”, the loop breaks, and the game ends.

Array.IndexOf(options, userInput) == -1

  • Checks if the user input is valid by verifying that it’s one of the three valid choices.

string computerMove = options[random.Next(options.Length)];

  • Picks a random item from the options array as the computer’s move.

Game Rules Logic:

if (userInput == computerMove)
{
    Console.WriteLine("It's a tie!");
}
else if ((userInput == "rock" && computerMove == "scissors") ||
         (userInput == "paper" && computerMove == "rock") ||
         (userInput == "scissors" && computerMove == "paper"))
{
    Console.WriteLine("You win!");
}
else
{
    Console.WriteLine("You lose!");
}
  • Compares the player’s choice with the computer’s and prints the result.

How to Run This Program

Option 1: Using a C# IDE

  1. Open Visual Studio or another C# IDE.
  2. Create a new Console App project.
  3. Replace the default Program.cs content with the full code above.
  4. Run the program.

Option 2: Using the Command Line

  1. Save the code in a file named RockPaperScissors.cs.
  2. Open a terminal and run:
csc RockPaperScissors.cs
RockPaperScissors.exe

Sample Run

Welcome to Rock-Paper-Scissors!
Type rock, paper, or scissors to play. Type quit to exit.

Enter your move: rock
Computer chose: paper
You lose!

Enter your move: scissors
Computer chose: paper
You win!

Enter your move: quit
Thanks for playing!

 

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.