UbGames

How to Roll Dice in C#

roll dice

How to Roll Dice in C#

This tutorial will simulate the roll of two dice in C# Windows Forms.

The program will use the Random Class, that represents a pseudo-random number generator, which is an algorithm that produces a sequence of numbers that meet certain statistical requirements for randomness. The Random.Next() method will be used to get a random integer within a specified range. Since we are using dice, the range will be 1 to 6.

dice

Install Visual Studio IDE

If you have not installed Visual Studio 2022, please read our Tutorial on How to Install Visual Studio.

Create a new Windows Forms App with Visual Studio

If you are not sure how to create a new Windows Forms project in C#, please read our Tutorial on How to Create a C# Project in Visual Studio.

DiceRoller Project File and Folder Structure

You will need to copy the dice images into a folder called “Dice” in the project folder. For example, C:\MyProjects\DiceRoller\Dice\.

Download the Dice1.zip file (images) to the project folder, for example “DiceRoller”.

Once you create your new project you should have something like the following:

roll dice

This project will use the default form “Form1” to display the controls and add the game logic.

Resize Form1 by changing the size of the form to 300×300. Changing the form size will fit the images on the form.

Setting up the Form1.cs design file

You will need to add two picture boxes, two labels and one button to Form1 and set the properties for each.

Click on the View>Toolbox in the Menu bar. Under “All Windows Forms”, select the PictureBox control, then drag & drop the picture box onto the Form1 window.

roll dice

Next, set the properties for this PictureBox, change the name to  “diceOneImage” and the size to (75, 75). Then, create the second picture box by copying the “diceOneImage” picture box and paste it onto the form.

Set the properties for the new PictureBox, change the name to  “diceTwoImage” and the size to (75, 75).

Form1 should look like the following:

roll dice

Add two labels to the form, using the toolbox window, drag and drop the Label component onto the form, then change the name to “diceOneLabel”.  Copy and paste this label onto the form and rename it to “diceTwoLabel”.

Place the labels under the corresponding image, diceOneLabel under diceOneImage and diceTwoLabel under diceTwoImage.

Form1 should look like the following:

roll dice

Add a button to the form, using the toolbox, drag and drop the Button component onto the form underneath the labels. Then change the name to “rollBtn” (Btn is short for Button), the text property to “Roll Dice” and resize the button to (260,60).

Form1 should look like the following:

roll dice

Next, you will import the images into your project. Click on the PictureBox named “diceOneImage”, then click on “Image” in the properties window.

roll dice

Then click on the three dots to open the Select Resource menu.

roll dice

Click on Import and select the six dice images under the “Dice” folder in the project folder, then click “Open”. Then click the “OK” button and the images will be ready to use in the project.

Adding code to the project

Double click on the “Roll Dice” button on the form. Visual Studio will automatically create a new click event for the button and open the code window.

Before we can access the images from the resources folder, a variable for each image will need to be added.

The following code will add the six dice images into an array. The name after “Resources” is the name of each image.

Image[] dice = {

Properties.Resources.dieRed_border1,

Properties.Resources.dieRed_border2,

Properties.Resources.dieRed_border3,

Properties.Resources.dieRed_border4,

Properties.Resources.dieRed_border5,

Properties.Resources.dieRed_border6};

Next, add the code for a Random variable to simulate the rolling of a die.

Random random = new Random();

Next, add two variables to hold the value of the dice.

int diceOneValue;

int diceTwoValue;

Next, you will add the code to the “rollBtn” click event.

The Random.Next() method is used to get a randomized value for the dice, this value will be in the range from 1 to 6.

Add the code for each dice, diceOneValue and diceTwoValue.

private void rollBtn_Click(object sender, EventArgs e)

{

    diceOneValue = random.Next(1, 7);

    diceTwoValue = random.Next(1, 7);

}

The two variables will contain a randomized value representing each dice.

Next, the variable for each dice image will be set to the corresponding randomized value.

diceOneImage.Image = dice[diceOneValue – 1];

diceTwoImage.Image = dice[diceTwoValue – 1];

The value of the dice is used to locate the correct image from the array. For example, if the random value for dice 1 (diceOneValue) is 3, then the image selected in the dice array would be the image in array(2), in this case dice number two (dieRed_border2).

Next, populate the labels with the value of the dice. “Dice #: Dice # Value”

diceOneLabel.Text = “Dice 1: ” + diceOneValue.ToString();

diceTwoLabel.Text = “Dice 2: ” + diceTwoValue.ToString();

The program will randomly roll two dice and display their images.

Code for Form1 Load:

The code for form1 should be similar to the following:

namespace DiceGame

{

    public partial class Form1 : Form

    {

        //Note: the names after Resources are the names of the actual images

        Image[] dice = {

        Properties.Resources.dieRed_border1,

        Properties.Resources.dieRed_border2,

        Properties.Resources.dieRed_border3,

        Properties.Resources.dieRed_border4,

        Properties.Resources.dieRed_border5,

        Properties.Resources.dieRed_border6};//holds all of our images.

        Random random = new Random();//will randomize your dice values

        int diceOneValue;//holds your dice 1 value

        int diceTwoValue;//holds your dice 2 value

        public Form1()

        {

            InitializeComponent();

        }

        private void rollBtn_Click(object sender, EventArgs e)

        {

            // Generates a random number within a range, random.Next(min, max-1);

            diceOneValue = random.Next(1, 7);//randomize dice 1’s value, // creates a number between 1 and 6

            diceTwoValue = random.Next(1, 7);//randomize dice 2’s value, // creates a number between 1 and 6

            diceOneImage.Image = dice[diceOneValue – 1];//set dice 1’s image to corresponding value

            diceTwoImage.Image = dice[diceTwoValue – 1];//set dice 2’s image to corresponding value

            diceOneLabel.Text = “Dice One: ” + diceOneValue.ToString();//set dice 1’s label

            diceTwoLabel.Text = “Dice Two: ” + diceTwoValue.ToString();//set dice 2’s label

        }

    }

}

Build and run the project

Following is a screenshot of the final project.

dice

Download the source code for this tutorial, DiceGame1_CS.zip.

Conclusion:

There are many places online to find asset packs containing dice images. Click here for free game assets.

ubgames

How to Roll Dice in C#