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
- 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.");
- 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 }; }
- 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()); }
- 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." }; }
- 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
- Randomized Computer Choice:
- The Random class ensures the computer’s choice is unpredictable.
- User Input Validation:
- Validates the user input and ensures it matches one of the menu options.
- Winner Determination:
- Uses a switch expression to handle the game’s logic efficiently.
- Replayable Game:
- The game loop allows the user to play multiple rounds until they decide to exit.
Ideas
- Scorekeeping:
- Add a scoreboard to track wins, losses, and draws.
int userWins = 0, computerWins = 0, draws = 0;
- Multiplayer Mode:
- Allow two users to play against each other by prompting for both players’ inputs.
- Advanced Choices:
- Extend the game to include more options like “Lizard” and “Spock” (Rock, Paper, Scissors, Lizard, Spock).
- GUI Version:
- Use Windows Forms or WPF to create a graphical interface for the game.
- 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.