C#

a C Sharp Rock, Paper, Scissors game with code explanation

Below is a C# Rock, Paper, Scissors game. This game is a console-based implementation where a user plays against the computer.

The computer’s choice is randomized, and the game follows the traditional rules of Rock, Paper, Scissors.

Full Code

using System;

class RockPaperScissors
{
    static void Main(string[] args)
    {
        Console.WriteLine("Welcome to Rock, Paper, Scissors!");
        Console.WriteLine("Rules: Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.");

        while (true)
        {
            Console.WriteLine("\nChoose an option:");
            Console.WriteLine("1. Rock");
            Console.WriteLine("2. Paper");
            Console.WriteLine("3. Scissors");
            Console.WriteLine("4. Exit");
            Console.Write("Enter your choice (1-4): ");

            string userInput = Console.ReadLine();

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

            // Convert user input to choice
            string userChoice = ConvertChoice(userInput);
            if (userChoice == null)
            {
                Console.WriteLine("Invalid input. Please choose a number between 1 and 4.");
                continue;
            }

            // Generate computer choice
            string computerChoice = GetComputerChoice();

            // Determine winner
            string result = DetermineWinner(userChoice, computerChoice);

            // Display choices and result
            Console.WriteLine($"\nYou chose: {userChoice}");
            Console.WriteLine($"Computer chose: {computerChoice}");
            Console.WriteLine(result);
        }
    }

    static string ConvertChoice(string input)
    {
        return input switch
        {
            "1" => "Rock",
            "2" => "Paper",
            "3" => "Scissors",
            _ => null
        };
    }

    static string GetComputerChoice()
    {
        Random random = new Random();
        int choice = random.Next(1, 4); // Random number between 1 and 3
        return ConvertChoice(choice.ToString());
    }

    static string DetermineWinner(string userChoice, string computerChoice)
    {
        if (userChoice == computerChoice)
        {
            return "It's a draw!";
        }

        return (userChoice, computerChoice) switch
        {
            ("Rock", "Scissors") => "You win! Rock beats Scissors.",
            ("Scissors", "Paper") => "You win! Scissors beats Paper.",
            ("Paper", "Rock") => "You win! Paper beats Rock.",
            ("Scissors", "Rock") => "You lose! Rock beats Scissors.",
            ("Paper", "Scissors") => "You lose! Scissors beats Paper.",
            ("Rock", "Paper") => "You lose! Paper beats Rock.",
            _ => "Unexpected error."
        };
    }
}

Explanation of the Code

  1. Game Introduction:
    • Displays the rules and provides a menu with options: Rock (1), Paper (2), Scissors (3), or Exit (4).
    Console.WriteLine("Welcome to Rock, Paper, Scissors!");
    Console.WriteLine("Rules: Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.");
    
  2. User Input Validation:
    • The ConvertChoice method translates user input (1, 2, or 3) into the corresponding string (Rock, Paper, Scissors).
    • If the input is invalid, it returns null, prompting the user to retry.
    static string ConvertChoice(string input)
    {
        return input switch
        {
            "1" => "Rock",
            "2" => "Paper",
            "3" => "Scissors",
            _ => null
        };
    }
    
  3. Computer’s Random Choice:
    • The GetComputerChoice method generates a random number (1 to 3) using the Random class and converts it into a choice using the ConvertChoice method.
    static string GetComputerChoice()
    {
        Random random = new Random();
        int choice = random.Next(1, 4); // Random number between 1 and 3
        return ConvertChoice(choice.ToString());
    }
    
  4. Determine Winner:
    • The DetermineWinner method compares the user’s choice and the computer’s choice to determine the result.
    • Uses a switch expression for clear and concise winner determination logic.
    static string DetermineWinner(string userChoice, string computerChoice)
    {
        if (userChoice == computerChoice)
        {
            return "It's a draw!";
        }
    
        return (userChoice, computerChoice) switch
        {
            ("Rock", "Scissors") => "You win! Rock beats Scissors.",
            ("Scissors", "Paper") => "You win! Scissors beats Paper.",
            ("Paper", "Rock") => "You win! Paper beats Rock.",
            ("Scissors", "Rock") => "You lose! Rock beats Scissors.",
            ("Paper", "Scissors") => "You lose! Scissors beats Paper.",
            ("Rock", "Paper") => "You lose! Paper beats Rock.",
            _ => "Unexpected error."
        };
    }
    
  5. Game Loop:
    • The program uses a while (true) loop to continuously play until the user selects “Exit” (4).
    • After each round, the program displays the user’s choice, the computer’s choice, and the result.
    if (userInput == "4")
    {
        Console.WriteLine("Thanks for playing! Goodbye!");
        break;
    }
    

Sample Run

Run the program:

Welcome to Rock, Paper, Scissors!
Rules: Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.

Choose an option:
1. Rock
2. Paper
3. Scissors
4. Exit
Enter your choice (1-4): 1

You chose: Rock
Computer chose: Scissors
You win! Rock beats Scissors.

Another round:

Choose an option:
1. Rock
2. Paper
3. Scissors
4. Exit
Enter your choice (1-4): 3

You chose: Scissors
Computer chose: Rock
You lose! Rock beats Scissors.

Exit:

Enter your choice (1-4): 4
Thanks for playing! Goodbye!

Features

  1. Randomized Computer Choice:
    • The Random class ensures the computer’s choice is unpredictable.
  2. User Input Validation:
    • Validates the user input and ensures it matches one of the menu options.
  3. Winner Determination:
    • Uses a switch expression to handle the game’s logic efficiently.
  4. Replayable Game:
    • The game loop allows the user to play multiple rounds until they decide to exit.

 

Ideas

  1. Scorekeeping:
    • Add a scoreboard to track wins, losses, and draws.
    int userWins = 0, computerWins = 0, draws = 0;
    
  2. Multiplayer Mode:
    • Allow two users to play against each other by prompting for both players’ inputs.
  3. Advanced Choices:
    • Extend the game to include more options like “Lizard” and “Spock” (Rock, Paper, Scissors, Lizard, Spock).
  4. GUI Version:
    • Use Windows Forms or WPF to create a graphical interface for the game.
  5. Timed Mode:
    • Add a timer to limit the time for making a choice.

This C# Rock, Paper, Scissors game demonstrates essential programming concepts like randomization, user input validation, and conditional logic.

Related posts

A C Sharp BMI and BMR Calculator program with code explanation

A C Sharp Bitcoin Price Converter with code explanation

A C Sharp Hi-Lo Guessing Game