Create Rock, Paper Scissors with Windows Form .NET

Rasyue
Dev Genius
Published in
9 min readJan 15, 2023

--

windows form .NET

Introduction

Windows Forms is a UI framework for building Windows desktop apps. It provides one of the most productive ways to create desktop apps based on the visual designer provided in Visual Studio.

Creating Windows Forms Project

Before we can begin creating our WinForms project, make sure you have Microsoft Visual Studio installed. You can get the Community version.

Once installed, open it and create a new project.

visual studio project list

Search for Windows Forms and choose the one with .NET Framework

winforms

Name your project name however you like and then create your project.

You should then have something like this.

winforms basic project

Now, let’s start designing our Windows Forms project.

Designing The Windows Form

Designing in WindowsForms project is really simple. You just have to drag-and-drop.

Once you add any kind of control, it will automatically generate the code for that control.

Let’s start by adding a simple TabControl to your project.

adding TabControl

The reason why we add this TabControl is to simulate multiple views and allow us to avoid having to hide and show all the controls later on.

Go ahead and start your project.

You will have something like this.

winforms

We will see later on how to hide the tabs.

Let’s add a button now that will start the game when clicked.

start game button

Now, let’s move on to the second tab page and completes it.

Tab Page 2

We added 3 buttons and 6 labels. Keep it simple.

If you are having trouble to add the controls to keep up with the tutorial.

Simple copy this code and paste into your Form1.Designer.cs file.

namespace Rasyue
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.startGame = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(3, -4);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(795, 453);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.startGame);
this.tabPage1.Location = new System.Drawing.Point(4, 25);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(787, 424);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.label6);
this.tabPage2.Controls.Add(this.label5);
this.tabPage2.Controls.Add(this.label4);
this.tabPage2.Controls.Add(this.label3);
this.tabPage2.Controls.Add(this.label2);
this.tabPage2.Controls.Add(this.label1);
this.tabPage2.Controls.Add(this.button3);
this.tabPage2.Controls.Add(this.button2);
this.tabPage2.Controls.Add(this.button1);
this.tabPage2.Location = new System.Drawing.Point(4, 25);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(787, 424);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// startGame
//
this.startGame.Location = new System.Drawing.Point(312, 171);
this.startGame.Name = "startGame";
this.startGame.Size = new System.Drawing.Size(148, 91);
this.startGame.TabIndex = 0;
this.startGame.Text = "Start Game";
this.startGame.UseVisualStyleBackColor = true;
//
// button1
//
this.button1.Location = new System.Drawing.Point(341, 242);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Rock";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(341, 280);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 0;
this.button2.Text = "Paper";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button1_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(341, 320);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 0;
this.button3.Text = "Scissors";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(338, 41);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(89, 16);
this.label1.TabIndex = 1;
this.label1.Text = "First to 5 Wins";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(59, 41);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(85, 16);
this.label2.TabIndex = 1;
this.label2.Text = "Player Score";
this.label2.Click += new System.EventHandler(this.label1_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(613, 41);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(66, 16);
this.label3.TabIndex = 1;
this.label3.Text = "Bot Score";
this.label3.Click += new System.EventHandler(this.label1_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(350, 155);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(45, 16);
this.label4.TabIndex = 1;
this.label4.Text = "Result";
this.label4.Click += new System.EventHandler(this.label1_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(613, 155);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(77, 16);
this.label5.TabIndex = 1;
this.label5.Text = "Bot Choose";
this.label5.Click += new System.EventHandler(this.label1_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(59, 155);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(96, 16);
this.label6.TabIndex = 1;
this.label6.Text = "Player Choose";
this.label6.Click += new System.EventHandler(this.label1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.tabPage2.PerformLayout();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Button startGame;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label4;
}
}

Now that we are done with the UI, let’s move to the coding part.

Coding The Game

Open the Form1.cs file. If you don’t already have it opened, just double click on any control you have in the form.

Form1.cs

This is the file where we will write our code. C# style!

On a high level, here are what we need to do:

  1. Create listener functions to each of the buttons. Start, Rock, Paper and Scissors buttons.
  2. When Start button is clicked, switch the tab panel.
  3. Disable the 3 buttons whenever one is clicked. (Rock, Paper, Scissors).
  4. Create an array containing, {"rock", "paper", "scissors"}. Randomly pick either one as the bot’s choice.
  5. Compare the player’s choice and bot’s choice and set the result as either win or lose.
  6. First to 5 points wins the game.

And…. the 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 Rasyue
{

public partial class Form1 : Form
{

int playerTotalScore = 0;
int botTotalScore = 0;

string[] botAnswers = { "paper", "rock", "scissors" };


public Form1()
{
InitializeComponent();

tabControl1.Top = tabControl1.Top - tabControl1.ItemSize.Height;
tabControl1.Height = tabControl1.Height + tabControl1.ItemSize.Height;
tabControl1.Region = new Region(new RectangleF(tabPage1.Left, tabPage1.Top, tabPage1.Width, tabPage1.Height + tabControl1.ItemSize.Height));

}

private void StartBtn_Click(object sender, EventArgs e)
{

tabControl1.SelectedIndex = 1;
}


private void RockBtn_Click(object sender, EventArgs e)
{

this.disableButtons();
this.CompareAnswers("rock");
}

private void PaperBtn_Click(object sender, EventArgs e)
{
this.disableButtons();
this.CompareAnswers("paper");
}
private void ScissorsBtn_Click(object sender, EventArgs e)
{
this.disableButtons();
this.CompareAnswers("scissors");
}


private void CompareAnswers(string userSelection)
{


Random random = new Random();
int randomIndex = random.Next(0, botAnswers.Length);

string botAnswer = botAnswers[randomIndex];



if (userSelection == "rock" && botAnswer == "paper")
{
botTotalScore++;
label4.Text = "YOU LOSE THIS ROUND!";
}
else if (userSelection == "rock" && botAnswer == "scissors")
{
playerTotalScore++;
label4.Text = "YOU WIN THIS ROUND!";
}
else if (userSelection == "paper" && botAnswer == "rock")
{
playerTotalScore++;
label4.Text = "YOU WIN THIS ROUND!";
}
else if (userSelection == "paper" && botAnswer == "scissors")
{
botTotalScore++;
label4.Text = "YOU LOSE THIS ROUND!";
}
else if (userSelection == "scissors" && botAnswer == "rock")
{
botTotalScore++;
label4.Text = "YOU LOSE THIS ROUND!";
}
else if (userSelection == "scissors" && botAnswer == "paper")
{
playerTotalScore++;
label4.Text = "YOU WIN THIS ROUND!";
}
else
{
label4.Text = "DRAW";
}


label6.Text = userSelection.ToUpper();
label5.Text = botAnswer.ToUpper();
label2.Text = playerTotalScore.ToString();
label3.Text = botTotalScore.ToString();


if (playerTotalScore == 5 || botTotalScore == 5)
{

if (playerTotalScore == 5)
{
label1.Text = "You win the game!";
}
else
{

label1.Text = "Bot wins the game!";
}
}
else
{
this.enableButtons();

}


}


private void enableButtons()
{
button1.Enabled = true;

button2.Enabled = true;

button3.Enabled = true;
}

private void disableButtons()
{

button1.Enabled = false;

button2.Enabled = false;

button3.Enabled = false;
}

private void label1_Click(object sender, EventArgs e)
{

}
}

}

Finally, the code for Form1.Designer.cs

namespace Rasyue
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.startGame = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(3, -4);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(795, 453);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.startGame);
this.tabPage1.Location = new System.Drawing.Point(4, 25);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(787, 424);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.label6);
this.tabPage2.Controls.Add(this.label5);
this.tabPage2.Controls.Add(this.label4);
this.tabPage2.Controls.Add(this.label3);
this.tabPage2.Controls.Add(this.label2);
this.tabPage2.Controls.Add(this.label1);
this.tabPage2.Controls.Add(this.button3);
this.tabPage2.Controls.Add(this.button2);
this.tabPage2.Controls.Add(this.button1);
this.tabPage2.Location = new System.Drawing.Point(4, 25);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(787, 424);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// startGame
//
this.startGame.Location = new System.Drawing.Point(312, 171);
this.startGame.Name = "startGame";
this.startGame.Size = new System.Drawing.Size(148, 91);
this.startGame.TabIndex = 0;
this.startGame.Text = "Start Game";
this.startGame.UseVisualStyleBackColor = true;
this.startGame.Click += new System.EventHandler(this.StartBtn_Click);

//
// button1
//
this.button1.Location = new System.Drawing.Point(341, 242);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Rock";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.RockBtn_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(341, 280);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 0;
this.button2.Text = "Paper";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.PaperBtn_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(341, 320);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 0;
this.button3.Text = "Scissors";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.ScissorsBtn_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(338, 41);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(89, 16);
this.label1.TabIndex = 1;
this.label1.Text = "First to 5 Wins";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(59, 41);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(85, 16);
this.label2.TabIndex = 1;
this.label2.Text = "Player Score";
this.label2.Click += new System.EventHandler(this.label1_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(613, 41);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(66, 16);
this.label3.TabIndex = 1;
this.label3.Text = "Bot Score";
this.label3.Click += new System.EventHandler(this.label1_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(350, 155);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(45, 16);
this.label4.TabIndex = 1;
this.label4.Text = "Result";
this.label4.Click += new System.EventHandler(this.label1_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(613, 155);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(77, 16);
this.label5.TabIndex = 1;
this.label5.Text = "Bot Choose";
this.label5.Click += new System.EventHandler(this.label1_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(59, 155);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(96, 16);
this.label6.TabIndex = 1;
this.label6.Text = "Player Choose";
this.label6.Click += new System.EventHandler(this.label1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.tabPage2.PerformLayout();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Button startGame;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label4;
}
}

Be mindful of the namespace Rasyue, since you probably have different project name.

Start The Game

rock, paper, scissors

Why don’t you try to make it even more visual appealing by adding images and sound effects?

The End…

--

--