C# Tutorial – Create a Rock Paper Scissors Game in 100 lines of code
- Subject: C# Tutorials
- Learning Time: 1 Hour
Hi, in this tutorial we will be remaking the rock paper scissors game in .NET and C# programming in Visual Studio. This game will be completed within 100 lines of code. We will explore how to make a simple rock paper scissors game with texts, buttons and images. In this game you will make the game so the player can play it against a simple CPU which will choose a random selection from rock paper or scissors option in the game.
- Make a rock paper scissors game in 100 lines of code with .Net and C# programming
- Use buttons and events to control the game
- Create a simple CPU choice system
- Use if statement and switch statements in the code
- Import images and use them to show the choices for the player and the CPU
Video Tutorial –
Download the images for Rock Paper Scissors Tutorial Project in C#
Full Source Code –
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Rock_paper_scissors_.net_game_MOO_ICT { public partial class Form1 : Form { string playerChoice; string computerChoice; string[] Options = { "R", "P", "S", "P", "S", "R" }; Random random = new Random(); int computerScore; int playerScore; string draw; public Form1() { InitializeComponent(); } private void MakeChoiceEvent(object sender, EventArgs e) { Button tempButton = sender as Button; playerChoice = (string)tempButton.Tag; int i = random.Next(0, Options.Length); computerChoice = Options[i]; lblPlayerchoice.Text = "Player is: " + UpdateTextandImage(playerChoice, PLAYER_PIC); lblCPUchoice.Text = "Computer is: " + UpdateTextandImage(computerChoice, CPU_PIC); CheckGame(); } private string UpdateTextandImage(string text, PictureBox pic) { string word = null; switch (text) { case "R": word = "Rock"; pic.Image = Properties.Resources.ROCK; break; case "P": word = "Paper"; pic.Image = Properties.Resources.PAPER; break; case "S": word = "Scissors"; pic.Image = Properties.Resources.SCISSORS; break; } return word; } private void CheckGame() { if (computerChoice == playerChoice) { draw = " Draw!"; } else if (playerChoice == "R" && computerChoice == "P" || playerChoice == "S" && computerChoice == "R" || playerChoice == "P" && computerChoice == "S") { computerScore++; draw = null; } else { playerScore++; draw = null; } lblCPUresult.Text = "Computer Score: " + computerScore + Environment.NewLine + draw; lblPlayerresult.Text = "Player Score: " + playerScore + Environment.NewLine + draw; } } }