So I have been working on this dice roller for a while but am stuck.
My form looks like this, a comboBox1 where you can pick numberOfSides on the dice, a textbox1 where you type the numberOfTimes it shall roll the selected dice, a button1 and a richTextBox1 to display the rolls.
My problem now is to call this with the button and display the results in the richTextBox1 side by side like this 4 12 8 14 before it breaks it to next line.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.DataSource = Enum.GetValues(typeof(DiceValue));
}
public enum DiceValue
{
[Description("3 sided die")] Three = 3,
[Description("3 sided die")] Four = 4,
[Description("3 sided die")] Five = 5,
[Description("3 sided die")] Six = 6,
}
public class RollDice
{
public static Random rndGen = new Random((int)DateTime.Now.Ticks);
public static int RandomDice(Int32 numberOfSides, Int32 numberOfTimes)
{
int total = 0;
for (int i = 0; i < numberOfTimes; i++)
{
// Upperbound is exclusive so add 1 to the number of sides
total = total + rndGen.Next(1, numberOfSides + 1);
}
return total;
}
}
private void button1_Click(object sender, EventArgs e)
{
var numberOfSides = comboBox1.SelectedItem;
var numberOfTimes = textBox1.Text;
int result = RollDice.RandomDice(numberOfSides, numberOfTimes);
}
}
}
I guess I need to make the textBox1 to a Int32.TryParse or some kind of int but where or how? Then this code on the button does not really feel right except the int result line. Could someone please help me with this?