Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a simple dice rolling game
Keywordsdice, roll, game, die
CategoriesPuzzles and Games
 
This example uses a class for the dice. The RollTheDie method sets the die's value.

    Public Number As Integer

Public Sub RolltheDie() Let Number = (Int(Rnd * 6) + 1) End Sub

Thanks to Evlich.

 
Private Sub Command1_Click()
    Form_Load
End Sub

Private Sub Command2_Click()
    'resize each array of dice classes
    For X = 0 To (CInt(Text1.Text) - 1)
        ReDim Preserve pdice(0 To X + 1)
        Set pdice(X) = New Diecls
    Next X
    For X = 0 To (CInt(Text2.Text) - 1)
        ReDim Preserve cdice(0 To X + 1)
        Set cdice(X) = New Diecls
    Next X
    'call the roll the dice method of every dice that is in
    ' each array
    For X = 0 To (CInt(Text1.Text) - 1)
        Set Dice = pdice(X)
            Dice.RolltheDie
    Next X
    For X = 0 To (CInt(Text2.Text) - 1)
        Set Dice = cdice(X)
            Dice.RolltheDie
    Next X
    'figure out the operation that is in teh operation list
    ' box
    Let cData = Operation.Text
    Select Case cData
        Case "Addition": Call Addition
        Case "Subtraction": Call Subtraction
        Case "Multiplication": Call Multiplication
        Case Else: Let Operation.Text = "Addition": Call _
            Addition
    End Select
    'display the totals
    PlayerDisplay.Caption = PTotal
    CompDisplay.Caption = CTotal
    'figure out whether the person won or lost by getting
    ' the win settings
    Let cData = Condition.Text
    Select Case cData
        Case "Higher Score": If PTotal > CTotal Then MsgBox _
            ("You win!!!")
        Case "Lower Score": If CTotal > PTotal Then MsgBox _
            ("You Win!!!")
        Case Else: If PTotal > CTotal Then MsgBox ("You " & _
            "win!!!")
    End Select
End Sub

Private Function Addition()
    PTotal = 0
    For X = 0 To (CInt(Text1.Text) - 1)
        Let PTotal = PTotal + pdice(X).Number
    Next X
    
    CTotal = 0
    For X = 0 To (CInt(Text2.Text) - 1)
        Let CTotal = CTotal + cdice(X).Number
    Next X
End Function

Private Function Multiplication()
    PTotal = 1
    For X = 0 To (CInt(Text1.Text) - 1)
        Let PTotal = PTotal * pdice(X).Number
    Next X
    
    CTotal = 1
    For X = 0 To (CInt(Text2.Text) - 1)
        Let CTotal = CTotal * cdice(X).Number
    Next X
End Function

Private Function Subtraction()
    PTotal = 0
    For X = 0 To (CInt(Text1.Text) - 1)
        Let PTotal = PTotal - pdice(X).Number
    Next X
    
    CTotal = 0
    For X = 0 To (CInt(Text2.Text) - 1)
        Let CTotal = CTotal - cdice(X).Number
    Next X
End Function
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated