[RESOLVED]DiceRoller Function
Hey all,
I have a function as such:
Code:
Function THRoller(ByVal Swings As Integer) As Integer
Dim Hits, Misses As Integer 'variables for attack results
Do Until RollCount = 0 'RollCount taken from initiative winners AttacksBox
THRollVal = CInt(Int((6 * Rnd()) + 1))
If THRollVal >= THN Then 'if the roll is greater or equal to the to hit number, it hits
Hits = Hits + 1
RollCount = RollCount - 1
ElseIf THRollVal < THN Then 'if the roll is less than the to hit number, it misses
Misses = Misses + 1
RollCount = RollCount - 1
End If
Loop
MsgBox("Rollcount is " & RollCount & "and hits equal " & Hits) 'why do you keep showing me one number???
End Function
I'm sending the function a user determined value from another section. My problem is, it only seems to be generating one number, and listing each roll as a hit. How can I get a new roll(repeating values are fine) for each loop iteration?
My apologies if the answer is obvious. I've only been programming for a week or so. Any other tips for cleaner/faster practices would be appreciated as well.
Re: [RESOLVED]DiceRoller Function
All figured out. Thanks for all your help Jmc.
Re: [RESOLVED]DiceRoller Function
Some old code I found (maybe based on JMC's link?)
Code:
Public Class Form1
'requires a Button and two picture boxes
Dim foo As New Dice
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
'foo = New Dice
If foo.Count <> 2 Then
Dim d1 As New Die(PictureBox1)
Dim d2 As New Die(PictureBox2)
foo.AddDie(d1) 'add die to dice collection
foo.AddDie(d2)
End If
'roll the dice
Dim stpw As New Stopwatch
Dim rollAround As Integer = 500 'how long the dice roll around
stpw.Reset()
stpw.Start()
Do
foo.RollAll()
Me.Refresh()
Loop While stpw.ElapsedMilliseconds < rollAround
End Sub
Private Sub _Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs)
' Create string to draw.
If DirectCast(sender, PictureBox).Tag.ToString <> "" Then
Dim drawString As String = DirectCast(sender, PictureBox).Tag.ToString
' Create font and brush.
Dim drawFont As New Font("Arial", 24)
Dim drawBrush As New SolidBrush(Color.Black)
' Create point for upper-left corner of drawing.
Dim drawRect As Rectangle = e.ClipRectangle
' Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect)
End If
End Sub
Public Class Dice
Public Shared _rnd As Random 'only need one random
Private _lDie As List(Of Die) 'store n die
Public Sub New() 'set up
If _rnd Is Nothing Then
_rnd = New Random
End If
Me._lDie = New List(Of Die)
End Sub
Public Sub AddDie(ByVal aDie As Die)
'add a die to list
Me._lDie.Add(aDie)
End Sub
Public Sub RollAll()
'roll all the dies
For Each d As Die In Me._lDie
'randomly change the die value
If _rnd.Next(0, 2) = 1 OrElse d.Roll = -1 Then
d.Roll = _rnd.Next(1, 7)
d.Display()
End If
Next
End Sub
ReadOnly Property Count As Integer
Get
Return Me._lDie.Count
End Get
End Property
End Class
Public Class Die
Private _vDie As Integer = -1 'indicates the die has never been rolled
Private _disp As PictureBox
Private _newV As Integer
Public Sub New(ByVal display As PictureBox)
Me._disp = display
End Sub
Property Roll() As Integer
Get
Return Me._vDie
End Get
Set(ByVal value As Integer)
Me._vDie = value
End Set
End Property
Public Sub Display()
Me._disp.Tag = Me.Roll
Me._disp.Invalidate()
End Sub
End Class
Private Sub Form1_Shown(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Shown
'set up picture boxes
For Each c As Control In Me.Controls
If TypeOf c Is PictureBox Then
Dim pb As PictureBox = DirectCast(c, PictureBox)
pb.Image = New Bitmap(pb.Width, pb.Height)
pb.BackColor = Color.White
pb.Tag = ""
AddHandler pb.Paint, AddressOf _Paint
End If
Next
End Sub
End Class
Re: [RESOLVED]DiceRoller Function
For a broader understanding of pseudo random numbers take the advice in the the first part of the first post here http://social.msdn.microsoft.com/For...8-97c21017998f.