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:
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
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
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):
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:
public Form1()
{
InitializeComponent();
InitializeVariables();
updateUserInterface();
}
Form class as a whole:
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.Threading.Tasks;
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).