Class: Crapshoot::Tokens::Series

Inherits:
Base
  • Object
show all
Defined in:
lib/crapshoot/tokens/series.rb

Defined Under Namespace

Classes: TooManyDiceException

Instance Method Summary collapse

Methods inherited from Base

#independent, #precedent

Constructor Details

#initialize(count, sides, drop = nil) ⇒ Series

Returns a new instance of Series.



4
5
6
7
8
# File 'lib/crapshoot/tokens/series.rb', line 4

def initialize(count, sides, drop = nil)
  @count = count.to_i
  @sides = sides.to_i
  @drop = drop
end

Instance Method Details

#eval(stack) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/crapshoot/tokens/series.rb', line 10

def eval(stack)
  raise TooManyDiceException.new(@count) if @count > 50_000
  results_array = (1..@count).to_a.map{ roll_a_die }.sort
  numeric_result = results_array.inject(&:+)
  roll_description = results_array.join '+'
  case @drop
  when '^'
    max = results_array.last
    numeric_result -= max
    roll_description += "-#{max}"
  when 'v'
    min = results_array.first
    numeric_result -= min
    roll_description += "-#{min}"
  end
  @result = Result.new numeric_result
  @result.description = "(#{roll_description})"
  @result.detailed_description = [["#{@count}d#{@sides}#{@drop}", roll_description]]

  return @result
end

#inspectObject



32
33
34
# File 'lib/crapshoot/tokens/series.rb', line 32

def inspect
  "<Crapshoot::Tokens::Series dice=#{@count}d#{@sides} drop=#{@drop or 'nothing'}>"
end