Skip to content

Instantly share code, notes, and snippets.

@mparker17
Created July 19, 2012 01:36

Revisions

  1. M Parker revised this gist Aug 13, 2013. 1 changed file with 15 additions and 6 deletions.
    21 changes: 15 additions & 6 deletions roll.rb
    Original file line number Diff line number Diff line change
    @@ -12,13 +12,22 @@ def roll(num = 1, sides = 6, bonus = 0)
    # - 1d4
    # - d4
    def parse_command(command = "")
    parts = command.split(/d/)
    # Split up the command.
    prefix_parts = command.split(/d/)
    suffix_parts = prefix_parts[1].split(/\+|-/)

    # The number of dice
    num = to_i(command_halves[0])
    # The number of dice.
    num = prefix_parts[0].empty? ? 1 : prefix_parts[0].to_i

    command_quarters = command_halves[1].split(/+/)
    # The type of dice.
    sides = suffix_parts[0].to_i

    # The type of dice
    # type = command_quarters[
    # Bonus.
    bonus = suffix_parts[1].empty? ? 0 : suffix_parts[1].to_i

    # Tell the user what we're doing so if they mistyped it, they're not
    # confused.
    $stdout.write(sprintf("Rolling %d x %d-sided dice with bonus %d.", num, sides, bonus))

    return roll(num, sides, bonus)
    end
  2. M Parker revised this gist Aug 13, 2013. 1 changed file with 8 additions and 9 deletions.
    17 changes: 8 additions & 9 deletions roll.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    #!/usr/bin/ruby

    def roll(num = 1, sides = 6, bonus = 0)
    # Rand returns a number between 1 and the argument.
    return (rand(sides-1) + 1 + bonus) * num;
    # Rand returns a number between 1 and the argument.
    return (rand(sides-1) + 1 + bonus) * num;
    end

    # Parse a string in the following forms:
    @@ -12,14 +12,13 @@ def roll(num = 1, sides = 6, bonus = 0)
    # - 1d4
    # - d4
    def parse_command(command = "")
    parts = command.split(/d/)
    parts = command.split(/d/)

    # The number of dice
    num = to_i(command_halves[0])
    # The number of dice
    num = to_i(command_halves[0])

    command_quarters = command_halves[1].split(/+/)

    # The type of dice
    type = command_quarters[
    command_quarters = command_halves[1].split(/+/)

    # The type of dice
    # type = command_quarters[
    end
  3. M Parker created this gist Jul 19, 2012.
    25 changes: 25 additions & 0 deletions roll.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/usr/bin/ruby

    def roll(num = 1, sides = 6, bonus = 0)
    # Rand returns a number between 1 and the argument.
    return (rand(sides-1) + 1 + bonus) * num;
    end

    # Parse a string in the following forms:
    # - 1d4+1 or 1d4-1
    # - d4+1 or d4-1
    # - +1 or -1
    # - 1d4
    # - d4
    def parse_command(command = "")
    parts = command.split(/d/)

    # The number of dice
    num = to_i(command_halves[0])

    command_quarters = command_halves[1].split(/+/)

    # The type of dice
    type = command_quarters[

    end