I am writing a dice roller winforms application using C# 2012 VS. Dice roller is set up for playing Shadowrun tabletop. I feel that there might be too much code going into the GUI, but I am unsure how it should be formatted. I am also very open to general advice. This is my first program I am trying to construct on my own after graduating. I have never attempted a GUI application before.
User Control 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 DiceRollerWinForms
{
public partial class DiceRollerUserControl : Form
{
private RollDice diceRoll = new RollDice();
private Roll currentRoll = new Roll();
private Int32 rollNumber = 1;
private Int32 currentNumDice = 1;
private Int32 lastNumHit = 0;
private Int32 lastNumDiceRolled = 0;
private string numDiceText = "Number of Dice";
public DiceRollerUserControl()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void mainSplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
{
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void diceToRollBox_TextChanged(object sender, EventArgs e)
{
if (Int32.TryParse(diceToRollBox.Text, out currentNumDice))
{
currentNumDice = Int32.Parse(diceToRollBox.Text);
}
else
{
currentNumDice = 1;
}
diceToRollBox.Text = currentNumDice.ToString();
}
private void subtractDiceButton_Click(object sender, EventArgs e)
{
if (currentNumDice > 1)
{
currentNumDice--;
}
diceToRollBox.Text = currentNumDice.ToString();
}
private void addDiceButton_Click(object sender, EventArgs e)
{
if (currentNumDice < 100)
{
currentNumDice++;
}
diceToRollBox.Text = currentNumDice.ToString();
}
private void RollDiceButton_Click(object sender, EventArgs e)
{
currentRoll = diceRoll.RollTheDice(currentNumDice, false, false);
ListViewItem i = new ListViewItem(rollNumber.ToString());
i.SubItems.Add(currentRoll.numHits.ToString());
i.SubItems.Add(currentRoll.rawRoll);
i.SubItems.Add(currentRoll.isGlitch.ToString());
i.SubItems.Add(currentRoll.isCritGlitch.ToString());
resultView.Items.Add(i);
rollNumber++;
}
private void RollDiceWithEdgeButton_Click(object sender, EventArgs e)
{
currentRoll = diceRoll.RollTheDice(currentNumDice, false, false);
ListViewItem i = new ListViewItem(rollNumber.ToString());
i.SubItems.Add(currentRoll.numHits.ToString());
i.SubItems.Add(currentRoll.rawRoll);
i.SubItems.Add(currentRoll.isGlitch.ToString());
i.SubItems.Add(currentRoll.isCritGlitch.ToString());
resultView.Items.Add(i);
rollNumber++;
}
private void reRollDiceWithEdgeButton_Click(object sender, EventArgs e)
{
currentRoll = diceRoll.RollTheDice(currentNumDice, false, false);
ListViewItem i = new ListViewItem(rollNumber.ToString());
i.SubItems.Add(currentRoll.numHits.ToString());
i.SubItems.Add(currentRoll.rawRoll);
i.SubItems.Add(currentRoll.isGlitch.ToString());
i.SubItems.Add(currentRoll.isCritGlitch.ToString());
resultView.Items.Add(i);
rollNumber++;
}
private void resultView_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Other Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace DiceRollerWinForms
{
class Roll
{
public string rawRoll { get; set; }
public Int32 numHits {get; set;}
public bool isGlitch {get; set;}
public bool isCritGlitch{get; set;}
public Int32 lastNumDiceRolled { get; set; }
public Int32 lastNumHitsRolled { get; set; }
public bool lastRollWasEdge { get; set; }
internal void FinalRollResults(Int32[] resultsRaw, Int32 numDice)
{
Int32[] rollResults = new Int32[6];
for (Int32 i = 0; i < numDice; i++)
{
switch(resultsRaw[i])
{
case 1:
rollResults[0]++;
break;
case 2:
rollResults[1]++;
break;
case 3:
rollResults[2]++;
break;
case 4:
rollResults[3]++;
break;
case 5:
rollResults[4]++;
break;
case 6:
rollResults[5]++;
break;
}
}
numHits = rollResults[4] + rollResults[5];
//If more than half the dice you rolled show a one, then you’ve got problems. This is called a glitch.
if ((numDice / 2) < rollResults[0])
{
this.isGlitch = true;
if (numHits == 0)
{
this.isCritGlitch = true;
}
}
rawRoll = string.Join(",", resultsRaw);
}
public Roll()
{
//six the number of sides on a dice
rawRoll = ""; //its a magic number
numHits = 0;
isGlitch = false;
isCritGlitch = false;
}
}
class RollDice : Roll
{
private Int32 const_Delay = 0;
private RNGCryptoServiceProvider RNGProvider = new RNGCryptoServiceProvider();
public Roll RollTheDice(Int32 numberOfDiceToRoll, bool edgeRoll, bool reRollEdge)
{
Roll currentRoll = new Roll();
Int32[] results = new Int32[numberOfDiceToRoll];
for (int i = 0; i < numberOfDiceToRoll; i++)
{
System.Threading.Thread.Sleep(const_Delay);
results[i] = RNGDiceRoll(RNGProvider);
}
currentRoll.FinalRollResults(results, numberOfDiceToRoll);
currentRoll.lastNumDiceRolled = numberOfDiceToRoll;
currentRoll.lastNumHitsRolled = currentRoll.numHits;
if (edgeRoll)
{
lastRollWasEdge = true;
}
return currentRoll;
}
private Int32 RNGDiceRoll(RNGCryptoServiceProvider Provider)
{
byte[] arr = new byte[4];
Int32 rand = 0;
do
{
Provider.GetBytes(arr);
rand = BitConverter.ToInt32(arr, 0);
}
while (rand < 1);
Int32 roll = (rand % 6) + 1;
return roll;
}
}
}