'-----------------------------------------
'DICE.VBP 12/31/97
'Author: Burt Abreu
'E-Mail: habreu@bellsouth.net
'URL: http://www.VBExplorer.com
'
'Beginners program that shows an example
'how to use random number generator to
'simulate dice rolls. You should be able
'to use the function in your own games
'that need simulated dice rolls. Those
'with more experience will forgive the
'heavy commenting, this is for beginners.
'Enjoy!
'
'Feel free to use this program however you
'wish. All I ask is that if you post it you
'give me proper credit.
'
'This is the code for the simple dice simulator.
'In case you don't have VB5 and you want to try
'this you'll need two command buttons and three
'labels. Name the labels lblMyRoll1, lblMyRoll2,
'lblHouseRoll and the command buttons cmdRollIt
'and cmdQuit. You'll miss out on the background
'graphics but it should work otherwise.
'------------------------------------------
ption Explicit 'Forces variable declarations
ption Base 1 'Sets the array start subscript to 1
'rather than default 0
Function RollDice(intNumOfSides, intNumOfDice) As Variant
im intIndex As Integer'an index to point to array elements
im MyRoll() As Variant'a dynamic array to hold each rolls dice totals
im intTotal As Integer'variable that accumulates total
ReDim MyRoll(intNumOfDice)
'-----------------------------------------------------
'Redimensions the MyRoll array to the size indicated
'by the passed NumOfDice each time it is called. Then
'the For..Next..Loop loops once for each die, and
'accumulates a total using intTotal to allow for mult
'-iple rolls to be returned as a total rather than
'individual amounts as in the case of the lblHouseRoll.
'-----------------------------------------------------
For intIndex = 1 To intNumOfDice
Randomize
MyRoll(intIndex) = Fix(intNumOfSides * Rnd) + 1
intTotal = intTotal + MyRoll(intIndex)
RollDice = intTotal
Next intIndex
End Function
Private Sub cmdQuit_Click()
Unload Me
End Sub
Private Sub cmdRollIt_Click()
lblMyRoll1.Caption = RollDice(6, 1)
lblMyRoll2.Caption = RollDice(6, 1)
lblHouseRoll.Caption = RollDice(6, 2)
If lblMyRoll1.Caption + lblMyRoll2.Caption _
> lblHouseRoll.Caption Then
lblMessage.Caption = "You Win!!!"
ElseIf lblMyRoll1.Caption + lblMyRoll2.Caption _
< lblHouseRoll.Caption Then
lblMessage.Caption = "You Lose!!!"
Else
lblMessage.Caption = "You Tied!!!"
End If
End Sub
Private Sub Form_Load()
Dim intNumOfSides As Integer
Dim intNumOfDice As Integer
End Sub
Downloads
In IE right-click and select 'Save Target As...' or in Netscape
right-click and select 'Save Link As...'
View Plain Text
Download Demo Project
Not Available