class Dice

Copyright © 2013 Adam Price (komidore64 at gmail dot com)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Attributes

child[RW]
count[R]
parent[RW]
sides[R]

Public Class Methods

new(count, sides = 1) click to toggle source
# File lib/easy_dice/dice.rb, line 21
def initialize(count, sides = 1)
  @count, @sides = count, sides
end

Public Instance Methods

+(n) click to toggle source
# File lib/easy_dice/dice.rb, line 31
def +(n)
  n = Dice.new(n) if n.class == Fixnum
  n.parent = self
  bottom.child = n
  top
end
max()
Alias for: maximum
maximum() click to toggle source
# File lib/easy_dice/dice.rb, line 44
def maximum
  max = @sides > 1 ? @sides * @count : @count
  max += @child.maximum unless @child.nil?
  max
end
Also aliased as: max
min()
Alias for: minimum
minimum() click to toggle source
# File lib/easy_dice/dice.rb, line 38
def minimum
  min = @count
  min += @child.minimum unless @child.nil?
  min
end
Also aliased as: min
roll() click to toggle source
# File lib/easy_dice/dice.rb, line 25
def roll
  total = @count.times.inject(0) { |total, _| total += rand(@sides) + 1 }
  total += @child.roll unless @child.nil?
  total
end
to_s() click to toggle source
# File lib/easy_dice/dice.rb, line 50
def to_s
  str = @sides > 1 ? "#{@count}d#{@sides}" : "#{@count}"
  str << " + #{@child.to_s}" unless @child.nil?
  str
end

Protected Instance Methods

bottom() click to toggle source
# File lib/easy_dice/dice.rb, line 67
def bottom
  return @child.nil? ? self : @child.bottom
end
top() click to toggle source
# File lib/easy_dice/dice.rb, line 63
def top
  return @parent.nil? ? self : @parent.top
end