Class: Tabletop::Pool

Inherits:
Array
  • Object
show all
Includes:
Comparable
Defined in:
lib/tabletop/pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(init_dice) ⇒ Pool

Requires one parameter, which can be either of

- an array of Die objects
- a string of elements separated by spaces which can be in two different formats:
   + d-notation (ie, "d20", "3dF", etc) denoting dice that will be given random values
   + a value, a slash, then a number of sides (ie, "2/6", "47/100", etc)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tabletop/pool.rb', line 12

def initialize(init_dice)
  return super(init_dice) if init_dice.kind_of?(Array)
  d_groups = init_dice.split
  dice = []
  d_groups.each do |d|
    if d =~ /d/ # d_notation
      number, sides = d.split('d')
      number = number.to_i
      number += 1 if number == 0
      if sides.to_i > 0
        number.times { dice << Die.new(sides: sides.to_i)}
      elsif sides == "F"
        number.times {dice << FudgeDie.new}
      end
    else
      dice << Die.new_from_string(d)
    end
  end
  super(dice)
end

Instance Method Details

#*(operand) ⇒ Object

Returns #sum times the operand



66
67
68
# File 'lib/tabletop/pool.rb', line 66

def *(operand)
  sum * operand
end

#+(operand) ⇒ Object

If adding a pool or array of dice objects, returns the the union of these pools.

If adding a number, returns the sum of that number and all die values in the pool.

Otherwise, raises an ArgumentError.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tabletop/pool.rb', line 38

def +(operand)
  # if the parameter seems to be an array of dice (this includes pools)
  if operand.respond_to?(:all?) and operand.all?{|obj| obj.respond_to?(:roll)}
    new_union(operand)
  # if the parameter seems to be a randomizer
  elsif operand.respond_to?(:sides)
    new_union([operand])
  elsif operand.respond_to?(:to_int)
    sum + operand
  else
     raise ArgumentError, "Only numbers and other pools can be added to pools"
  end
end

#-(operand) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/tabletop/pool.rb', line 52

def -(operand)
  if operand.respond_to?(:to_a)
    super
  else
    sum - operand
  end
end

#<=>(operand) ⇒ Object

Compares the operand to #sum



61
62
63
# File 'lib/tabletop/pool.rb', line 61

def <=>(operand)
    sum <=> operand.to_int
end

#coerce(other) ⇒ Object

:nodoc:



70
71
72
# File 'lib/tabletop/pool.rb', line 70

def coerce(other) #:nodoc:
  [other, sum]
end

#d_notationObject

Returns a string of the pool’s dice in d-notation



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tabletop/pool.rb', line 80

def d_notation
  fudge = nil
  result = {}
  each do |die|
    if die.instance_of?(FudgeDie)
      fudge = count {|d| d.instance_of?(FudgeDie)}
    else
      result[die.sides] = count {|d| d.sides == die.sides}
    end
  end
  d_array = result.sort.collect do |d_group| 
    number = d_group[1]
    number = "" if number == 1
    sides = d_group[0]
    "#{number}d#{sides}"
  end
  if fudge
    d_array << "#{fudge}dF"
  end
  d_array
end

#drop(to_drop) ⇒ Object

Returns a copy of the current pool, minus any dice with values equal the value (or in the array of values) passed.



185
186
187
188
189
# File 'lib/tabletop/pool.rb', line 185

def drop(to_drop)
  to_drop = [to_drop].flatten #turn it into an array if it isn't one.
  kept = reject{|die| to_drop.include?{die.value}}
  Pool.new(kept)
end

#drop_highest(n = 1) ⇒ Object

Returns a copy of the Pool, minus the n highest-value dice



173
174
175
# File 'lib/tabletop/pool.rb', line 173

def drop_highest(n=1)
  Pool.new(self-highest(n))
end

#drop_lowest(n = 1) ⇒ Object

Returns a copy of the Pool, minus the n lowest-value dice.



178
179
180
# File 'lib/tabletop/pool.rb', line 178

def drop_lowest(n=1)
  Pool.new(self-lowest(n))
end

#highest(n = 1) ⇒ Object

Returns a Pool containing copies of the n highest dice



151
152
153
154
155
156
157
# File 'lib/tabletop/pool.rb', line 151

def highest(n=1)
  if n < length
    drop_lowest(length-n)
  else
    self
  end
end

#lowest(n = 1) ⇒ Object

Returns a Pool containing copies of the n lowest dice



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tabletop/pool.rb', line 160

def lowest(n=1)
  sorted = sort.first(n)
  in_order = []
  each do |d|
    if sorted.include?(d)
      in_order << d
      sorted -= [d]
    end
  end
  Pool.new(in_order)
end

#roll(params = {}) ⇒ Object

Rolls every die in the pool, and returns the Pool.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tabletop/pool.rb', line 103

def roll(params={})
  each do |die|

    meets_all_conditions = true

    params.each do |condition, term|
      attribute, comparison = condition.to_s.split("_")
      die_att = die.send(attribute.to_sym)

      case comparison
      when "under"
        meets_all_conditions = false unless die_att < term
      when "over"
        meets_all_conditions = false unless die_att > term
      when "equals"
        meets_all_conditions = false unless die_att == term
      end
    end

    die.roll if meets_all_conditions
  end
  self
end

#roll_if(&block) ⇒ Object



127
128
129
130
131
# File 'lib/tabletop/pool.rb', line 127

def roll_if(&block)
  each do |die|
    die.roll if block.call(die)
  end
end

#setsObject

Returns a string describing all sets of die values in the pool in ORE notation.



142
143
144
145
146
147
148
# File 'lib/tabletop/pool.rb', line 142

def sets
  result = {}
  each do |die|
    result[die.value] = count {|d| d.value == die.value}
  end
  result.sort_by{|height, width| [width, height] }.collect {|i| i[1].to_s+"x"+i[0].to_s}.reverse
end

#sumObject

Returns the sum of all values of dice in the pool



134
135
136
# File 'lib/tabletop/pool.rb', line 134

def sum
  inject(0) {|sum, d| sum + d.value}
end

#to_intObject



137
138
139
# File 'lib/tabletop/pool.rb', line 137

def to_int
  sum
end

#valuesObject

Returns an array of the value of each die in the pool



75
76
77
# File 'lib/tabletop/pool.rb', line 75

def values
  map {|die| die.value}
end