Create a Windows Application that rolls any number of dice, displays the number
ID: 3632274 • Letter: C
Question
Create a Windows Application that rolls any number of dice, displays the number on each die, and displays a total of the value of all dice in the roll.· The input should come from a general Sub that uses an InputBox to ask the user for the number of dice and returns the answer via a parameter.
· Each roll should be generated by a call to a Function. Declare and initialize the following class-level variable:
Dim roller As New Random(Now. Millisecond)
Your Function should call “roller.Next” with two arguments (min, max) to generate and return a roll value between 1 and 6. The min value can be hard-coded as 1. The max value should be passed as input to the function. We’ll assume all of our dice have six sides, so you should call your function with an argument that evaluates to 6.
· You should have two Subs for output; one that displays the value of each die in a list box; another that displays the final sum of all dice.
Example: The user is asked for the number of dice to roll and responds with 3. The following three random rolls and sum are shown:
5
1
6
Total: 12
Explanation / Answer
Public Class frmD6 Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click If cmbDieSize.Text = String.Empty Then MessageBox.Show("Please choose the size of die you wish to roll", "Error") 'Quick error handling in case the user tries to roll without selecting a die size Else Dim oDice As New Random Dim iDieSize As Integer = CInt(cmbDieSize.Text) Dim iDiceResult As Integer = oDice.Next(1, iDieSize + 1) lstDiceRolls.Items.Add("D" & iDieSize.ToString & ControlChars.Tab & iDiceResult.ToString) End If End Sub Private Sub frmD6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstDiceRolls.Items.Add("Size" & ControlChars.Tab & "Roll") End Sub End Class