15,748,748 members
See more:
Hi all,
I am doing an online C# course and need some practice with if-else statements so I have an option to make a dice game.

In this game, the player can bet a certain amount of money (as long as it is less than or equal to their displayed current balance) and roll three dice and try and beat the computer. The closest player to 18 wins, if both have the same total value then it is a draw. The player can keep playing until they run out of money or until they decide to stop playing.

I am wanting to use the try-catch statement as well as the If Else statement and have nested if-else statements. I am a little stuck on the coding of the if-else statements and getting the value of the randomly generated dice amount for each of the die, to show in an uneditable textbox dedicated to each die on the user interface.

Also if I want the player to start with a balance of \$500 how would I display this on the form? I am definitely a beginner so any help would be incredibly appreciated! :)

Honestly very confused and as you can see I have started but got stuck!

What I have tried:

```public partial class Form1 : Form
{
//create random number generator
Random rollDice = new Random();

public Form1()
{
InitializeComponent();
}

private void buttonRollDice_Click(object sender, EventArgs e)
{

decimal currentBalance = 500m;
decimal betAmount = 0m;

//get amount from current balance
currentBalance = decimal.Parse(textBoxCurrentBalance.Text);
//get bet amount form textbox
betAmount = decimal.Parse(textBoxBetAmount.Text);

//check if bet amount is less than or equal to current balance
if (betAmount <= currentBalance)
{
//create variables for player and computer dice
int playerDice1 = rollDice.Next(1, 7);
int playerDice2 = rollDice.Next(1, 7);
int playerDice3 = rollDice.Next(1, 7);

int compDice1 = rollDice.Next(1, 7);
int compDice2 = rollDice.Next(1, 7);
int compDice3 = rollDice.Next(1, 7);

if (true)
{

}
else
{

}
}
else
{
MessageBox.Show("Error, Please enter an amount smaller or equal to your current balance");
}
}
}```
Posted
Updated 3-May-20 21:33pm

## Solution 1

First off, the try..catch is for catching errors in the code. It isn't going to be of much help here. Another thing, you are going to want to declare the variables at the beginning of the form class. Also recommend creating constants for any future values that you may end up wanting to change (such as the start balance). in future, with this constant, if you want to change the start balance, you just change this one variable so you don't have to search through all your code to find and change each time you have a 500:

VB
```public partial class Form1 : Form
{
//Constants for future updates in case you want to change some base numbers around
static decimal START_BALANCE = 500;

//create random number generator
Random rollDice = new Random();
//some variables
decimal currentBalance = 0;
decimal betAmount = 0;
//Dice Values
int player1Dice = 0;
int player2Dice = 0;
int player3Dice = 0;
int playerDiceTotal = 0;

int comp1Dice = 0;
int comp2Dice = 0;
int comp3Dice = 0;
int compDiceTotal = 0;

//To determine the winner
int winner = 0;```

As for displaying the player's current Balance on the form, the best way would be to create a reusable function to update the player's UI. Example
VB
```private void updateUserInterface()
{
//Show Currency Amount in USD formatting rules.
textBoxCurrentBalance.Text = currentBalance.ToString("\$#,##0.00");
//Show Player Dice Values
txtPlayerDice1.Text = player1Dice.ToString();
txtPlayerDice2.Text = player2Dice.ToString();
txtPlayerDice3.Text = player3Dice.ToString();
txtPlayerTotal.Text = playerDiceTotal.ToString();

txtComputerDice1.Text =comp1Dice.ToString();
txtComputerDice2.Text = comp2Dice.ToString();
txtComputerDice3.Text = comp3Dice.ToString();
txtComputerTotal.Text = compDiceTotal.ToString();
if (playerDiceTotal > 0 && compDiceTotal > 0)
{

switch (winner)
{
case 0:
label5.Text = "Draw";
break;
case 1:
label5.Text = "Player Wins";
break;
case 2:
label5.Text = "Computer Wins";
break;

}
}

}
```

As for the actual Dice Rolling
VB
```private void buttonRollDice_Click(object sender, EventArgs e)
{
Random r = new Random();

Decimal.TryParse(textBoxBetAmount.Text, out betAmount);

if (currentBalance > betAmount && textBoxBetAmount.Text != "")
{
player1Dice = r.Next(1, 7);
player2Dice = r.Next(1, 7);
player3Dice = r.Next(1, 7);
playerDiceTotal = player1Dice + player2Dice + player3Dice;

comp1Dice = r.Next(1, 7);
comp2Dice = r.Next(1, 7);
comp3Dice = r.Next(1, 7);
compDiceTotal = comp3Dice + comp2Dice + comp1Dice;

int playerDiff = 18 - playerDiceTotal;
int compDiff = 18 - compDiceTotal;

if (playerDiff > compDiff)
{
//computer is closest to 18
winner = 2;
}
else if (playerDiff==compDiff)
{
//draw
winner = 0;
}
else if (playerDiff < compDiff)
{
//player is closest to 18
winner = 1;
}

updateUserInterface();

}
else
{
MessageBox.Show("Error, Please enter dollar amount smaller or equal to your Current Balance.");
}

}
```

Recommend Adding a function to initialize all variables that need to have initial values(such as the setting the current balance to the starting balance):

VB
```private void InitializeVariables()
{
currentBalance = START_BALANCE;
}
```

and in the Form constructor, where InitializeComponent is called, we want to call the InitializeVariables and the updateUserInterface functions:
VB
```public Form1()
{
InitializeComponent();
InitializeVariables();
updateUserInterface();
}
```

Form class as a whole:
VB
```using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{

//Some constants for future Changes
static decimal START_BALANCE = 500;
//

decimal currentBalance = 0;
decimal betAmount = 0;
int player1Dice = 0;
int player2Dice = 0;
int player3Dice = 0;
int playerDiceTotal = 0;

int comp1Dice = 0;
int comp2Dice = 0;
int comp3Dice = 0;
int compDiceTotal = 0;

//0 = draw, 1 = player, 2 = computer
int winner = 0;

public Form1()
{
InitializeComponent();
InitializeVariables();
updateUserInterface();
}

private void InitializeVariables()
{
currentBalance = START_BALANCE;
}

private void buttonRollDice_Click(object sender, EventArgs e)
{
Random r = new Random();

Decimal.TryParse(textBoxBetAmount.Text, out betAmount);

if (currentBalance > betAmount && textBoxBetAmount.Text != "")
{
player1Dice = r.Next(1, 7);
player2Dice = r.Next(1, 7);
player3Dice = r.Next(1, 7);
playerDiceTotal = player1Dice + player2Dice + player3Dice;

comp1Dice = r.Next(1, 7);
comp2Dice = r.Next(1, 7);
comp3Dice = r.Next(1, 7);
compDiceTotal = comp3Dice + comp2Dice + comp1Dice;

int playerDiff = 18 - playerDiceTotal;
int compDiff = 18 - compDiceTotal;

if (playerDiff > compDiff)
{
//computer is closest to 18
winner = 2;
}
else if (playerDiff==compDiff)
{
//draw
winner = 0;
}
else if (playerDiff < compDiff)
{
//player is closest to 18
winner = 1;
}

updateUserInterface();

}
else
{
MessageBox.Show("Error, Please enter dollar amount smaller or equal to your Current Balance.");
}

}

private void updateUserInterface()
{
//Show Currency Amount in USD formatting rules.
textBoxCurrentBalance.Text = currentBalance.ToString("\$#,##0.00");
//Show Player Dice Values
txtPlayerDice1.Text = player1Dice.ToString();
txtPlayerDice2.Text = player2Dice.ToString();
txtPlayerDice3.Text = player3Dice.ToString();
txtPlayerTotal.Text = playerDiceTotal.ToString();

txtComputerDice1.Text =comp1Dice.ToString();
txtComputerDice2.Text = comp2Dice.ToString();
txtComputerDice3.Text = comp3Dice.ToString();
txtComputerTotal.Text = compDiceTotal.ToString();
if (playerDiceTotal > 0 && compDiceTotal > 0)
{

switch (winner)
{
case 0:
label5.Text = "Draw";
break;
case 1:
label5.Text = "Player Wins";
break;
case 2:
label5.Text = "Computer Wins";
break;

}
}

}

}
}```

I know I have made some suggestions in which I would use personally. Every developer has his/her own way of doing things, I just like to make some things a bit easier on myself so that I don't have to repeat the same code over and over. Results in fewer lines of code as well. Hopefully this will help you. Good luck.(Hopefully there aren't any typos in the code as that can be a really painful search).

BillWoodruff 2-May-20 8:04am
lots of good suggestions/code, but, if you write the code for the OP, are you helping the OP to learn ?

## Solution 2

Before you write any code it is a good idea to have a think about what exactly are you trying to do and what steps you need to take to achieve your goal – the smaller the steps the better.
It seems to me that you have got five main processes in the game. Input data, validate input, simulate dice throws, process results and, finally, display the results. If you can break down the main processes into smaller sub-processes, so much the better. Each process needs a bit of thought about the best way to implement it. Taking the simulation of the dice throws as an example. Here are a couple of points to consider.
How to simulate a single dice throw?
Has system Random got a method that will generate a number between 1 and 6? What’s the best way to use system Random? Hint- do not create a new instance with every call to the simulation method.
What is the best way to return data so that it can be easily consumed by the next stage?
A collection of three dice throws is required for each player. Is it best to return a collection or just return each throw in isolation? If you decide to return a collection, is there a collection class that has a method to sum its contents so that you will not need to write code to do that?
Finally, I’d like to pass on a piece of advice that was given to me in my early days – the quality of your code is inversely proportional to the number of if then else statements it contains. Good luck.

v3