winzip icon

Dice Roller / Math Formula Processing

Submitted on: 12/26/2018 4:28:17 PM
By: Mark Kintigh  
Level: Intermediate
User Rating: Unrated
Compatibility: VB.NET
Views: 1650
     This class is designed to calculate basic math functions (*, /, +, -), stored within a string, with a new symbol for adding a random value (simulating rolling dice). It is smart enough to also handle parenthetical formulas. I also built in C like conditions to be added to the end of the formula to skew the final result (valid conditions: >, <, =, !=, >=, <=). Has formula error handling. Almost every step of the process is heavily documented; great for beginner as well as intermediate programmers.

Here's some examples of formulas that it can process and what they mean:
  • 3d6+2 -- roll 3, 6-sided dice and add 2 to the results.
  • 3d6?>16:+1d6 -- roll 3, 6-sided dice and if the result is greater than 16 add an additional roll from a 6-sided die.
  • (1d4)d6 -- roll a 4-sided dice and then roll that many 6-sided dice for the result.

Example usage for the code (included in the header of the class):
Public Sub TestDice()
    Dim c As New clsDice
    Dim r As String = "(n/a)"
    Dim s As String = "help"
    While s.Length > 0
        s = InputBox("Last result: " & r & vbCrLf & vbCrLf & _
            "Enter formula (""help"" for assistance): ", _
            "Calculator", s)
        If s.ToLower = "help" Then
        ElseIf s <> "" Then
            If c.FormulaError Then
                MsgBox("There was an error in your formula: " & _
                    vbCrLf & s, MsgBoxStyle.Exclamation, "Error")
                r = c.Result
            End If
        End If
    End While
    c = Nothing
End Sub

This class has been around for a long time (written back in 2008) and has never broken. It was first written for an AD&D program and designed to handle the typical formulas you would see in roll playing games. I finally need to add something to it and decided to share.
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net

Other 2 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.