Results 1 to 6 of 6

Thread: Contest 14 - DnD Dice Roller (Delaney - VB.net)

  1. #1

    Thread Starter
    Hyperactive Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    412

    Contest 14 - DnD Dice Roller (Delaney - VB.net)

    Hello

    Finally, I chose the console application, I never made one before and it reminds me my old days with DOS (when I was young )

    Here is my contribution :
    VB.NET (2017) Code:
    1. Imports System
    2. Imports System.Collections.Generic
    3. Imports System.Linq
    4.  
    5. Module RollTheDices
    6.  
    7.     Sub Main()
    8.         Console.Title = "Roll the dices!!"
    9.         Console.WriteLine("quit to exit, help for info how to write the command" & Environment.NewLine & "enter you roll command")
    10.         While True
    11.             Dim command As String = Console.ReadLine
    12.             If command.ToLower = "quit" Then
    13.                 Exit While
    14.             ElseIf command.ToLower = "help" Then
    15.                 Console.WriteLine("Enter your command under the template xdy with x the number of dices and y the number of faces of the dice")
    16.                 Console.WriteLine("You can roll several types of dice by simply separate the command with a comma or a space")
    17.                 Console.WriteLine("Dices allowed are 4-sided, 6-sided, 8-sided, 10-sided, 12-sided, 20-sided and percentage dice (you must use % instead of 100)")
    18.                 Console.WriteLine("Examples : 1d6,8d4 1d%")
    19.             ElseIf command = "" Then
    20.                 'deal with empty input
    21.             Else
    22.                 Roll(command.ToLower)
    23.             End If
    24.  
    25.         End While
    26.         MsgBox("Thank you for playing",, "Good Bye")
    27.  
    28.     End Sub
    29.  
    30.     Public Class Dice
    31.         Property dice_command As String
    32.         Property Nb As Integer
    33.         Property Max As Integer
    34.         Property Result As Integer()
    35.     End Class
    36.  
    37.     Public Function Getresult(a As Integer, b As Integer) As Integer()
    38.         Dim RND As New Random
    39.         Dim value(a) As Integer
    40.         For i = 0 To a - 1
    41.             value(i) = RND.Next(1, b + 1)
    42.         Next
    43.         value(a) = value.Sum
    44.         Return value
    45.     End Function
    46.  
    47.  
    48.     Private Sub Roll(commande As String)
    49.         Dim game As New List(Of Dice)
    50.  
    51.         Try
    52.             Dim dices() As String = commande.Split(New Char() {" "c, ","c}, StringSplitOptions.None)
    53.             For Each ddice In dices
    54.                 Dim values() As String = ddice.Split(New Char() {"d"c})
    55.                 If values(0) = Nothing Then values(0) = "1" ' in case you enter d6 instead of 1d6
    56.                 If Not (values(1).Contains("4") Or values(1).Contains("6") Or values(1).Contains("8") Or values(1).Contains("10") Or values(1).Contains("12") Or values(1).Contains("20") Or values(1).Contains("%")) Then
    57.                     Console.WriteLine("This dice d{0} doesn't exist, try again", values(1))
    58.                     Exit Sub
    59.                 End If
    60.                 If values(1) = "%" Then values(1) = "100" 'affect %
    61.                 Dim de As New Dice With {.dice_command = ddice, .Nb = CInt(values(0)), .Max = CInt(values(1)), .Result = Getresult(.Nb, .Max)}
    62.                 game.Add(de)
    63.             Next
    64.         Catch ex As Exception
    65.             Console.WriteLine("You did something wrong ;) try again !")
    66.         End Try
    67.  
    68.         For Each dice In game
    69.             Dim texte As String = ""
    70.             With dice
    71.                 texte += String.Format("{0} --> ", .dice_command)
    72.                 For i = 0 To dice.Nb - 1
    73.                     texte += String.Format("{0}, ", .Result(i).ToString)
    74.                 Next
    75.                 texte += String.Format("Total = {0}    ", .Result(.Nb).ToString)
    76.             End With
    77.             Console.WriteLine(texte)
    78.         Next
    79.  
    80.     End Sub
    81.  
    82.  
    83.  
    84. End Module

    and the fiddle link : https://dotnetfiddle.net/BbF0Tw

    Regards
    Last edited by Delaney; Oct 27th, 2020 at 04:30 AM. Reason: added the language in the title
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,764

    Re: Contest 14 - DnD Dice Roller (Delaney)

    The program worked without issues.

    In terms of length of code, this one is certainly the shortest.

    There were a couple of areas of improvement that I could see. For example:
    Code:
    If command.ToLower = "quit" Then
    Typically you'd want to use the String::Equals method and pass the StringComparison overload:
    Code:
    If command.Equals("quit", StringComparison.OrdinalIgnoreCase) Then
    Also, the Random class should not be declared inside the GetResult function. Instead, it should declared as a ReadOnly variable at the module level:
    Code:
    Private ReadOnly _rnd As New Random()
    There were some stylistic issues I had with the code, such as mixing the & and + operators to concatenate Strings. I also would loved to have seen delegates used to parse the incoming commands, but these don't actually go against you. They are more ramblings inside my mind.

  3. #3

    Thread Starter
    Hyperactive Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    412

    Re: Contest 14 - DnD Dice Roller (Delaney)

    Quote Originally Posted by dday9 View Post
    I also would loved to have seen delegates used to parse the incoming commands,
    Hello Dday9,

    I would need your help to understand how to do it because I don't know this way. Can you give me an example ?

    regards
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,764

    Re: Contest 14 - DnD Dice Roller (Delaney - VB.net)

    Take a look at this example:
    Code:
    Imports System
    Imports System.Collections.Generic
    Public Module Program
    
        Private Delegate Function CommandDelegate() As Boolean
        Private ReadOnly Commands As New Dictionary(Of String, CommandDelegate) From {
            { "quit", New CommandDelegate(AddressOf Quit) },
            { "help", New CommandDelegate(AddressOf Help) }
        }
    
        Public Sub Main()
            Help()
            Dim arguments() As String
            Dim playing As Boolean = True
            Do
                Console.Write("> ")
                arguments = Console.ReadLine().Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
    
                Try
                    playing = ParseInput(arguments)
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
            Loop While playing
        End Sub
    
        Private Function ParseInput(arguments() As String) As Boolean
            If (arguments.Length = 0) Then
                ' handle empty input
                Return True
            End If
    
            Dim argument1 As String = arguments(0)
            If (Not Commands.ContainsKey(argument1)) Then
                ' roll
                Return True
            End If
    
            Return Commands(argument1).Invoke()
        End Function
    
        Private Function Help() As Boolean
    	Console.WriteLine("Enter ""help"" to bring up these instructions.")
    	Console.WriteLine("Enter ""quit"" to stop rolling.")
            Console.WriteLine("Enter your command under the template xdy with x the number of dices and y the number of faces of the dice")
            Console.WriteLine("You can roll several types of dice by simply separate the command with a comma or a space")
            Console.WriteLine("Dices allowed are 4-sided, 6-sided, 8-sided, 10-sided, 12-sided, 20-sided and percentage dice (you must use % instead of 100)")
            Console.WriteLine("Examples : 1d6,8d4 1d%")
            Return True
        End Function
    
        Private Function Quit() As Boolean
            Return False
        End Function
    
        Private Sub Roll(<[ParamArray]()> arguments() As String)
            ' logic here
        End Sub
    
    End Module
    Fiddle: https://dotnetfiddle.net/tV0roK

    The basic idea in this example is that a delegate function is setup which will return a boolean value that represents if the user wants to continue playing. The commands with their respective functions are defined in the Commands dictionary. Then in your main loop, you check for valid input which in turn invokes the respective command's delegate function.
    Last edited by dday9; Oct 28th, 2020 at 08:47 AM.

  5. #5

    Thread Starter
    Hyperactive Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    412

    Re: Contest 14 - DnD Dice Roller (Delaney - VB.net)

    Thanks for the example. if i understand well, at some point you associate some words (string) to some functions so when you write the word, it call the function associated.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,764

    Re: Contest 14 - DnD Dice Roller (Delaney - VB.net)

    That's basically right.

    The dictionary of String, Delegate in this case associates a word to the delegate function. In my opinion it is a more efficient means of code management because it allows you to easily add/remove commands.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width