Class: Tabletop::TokenStack

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_tokens = 1, hash = {}) ⇒ TokenStack

Returns a new instance of TokenStack.



26
27
28
29
# File 'lib/tabletop/token.rb', line 26

def initialize(num_tokens = 1, hash={})
  @count = num_tokens
  @max = hash[:max]
end

Instance Attribute Details

#countObject

The number of tokens in the stack, and the maximum number it can have



23
24
25
# File 'lib/tabletop/token.rb', line 23

def count
  @count
end

#maxObject

The number of tokens in the stack, and the maximum number it can have



23
24
25
# File 'lib/tabletop/token.rb', line 23

def max
  @max
end

Instance Method Details

#<=>(operand) ⇒ Object



31
32
33
# File 'lib/tabletop/token.rb', line 31

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

#add(n = 1) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
# File 'lib/tabletop/token.rb', line 40

def add(n = 1)
  raise ArgumentError unless n.respond_to?(:to_i)
  raise ArgumentError if n < 0
  n = n.to_i
  raise_if_over_max(n + @count)
  @count += n
end

#move(n, opts) ⇒ Object

Usage: stack_a.move(N, :to => stack_b) Removes N tokens from stack_a, and adds the same number to stack_b



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tabletop/token.rb', line 68

def move(n, opts)
  begin
    opts[:to].add(n)
  rescue NoMethodError
    raise ArgumentError
  end
  
  begin
    remove(n)
  rescue NotEnoughTokensError
    opts[:to].remove(n)
    raise
  end
end

#raise_if_over_max(value) ⇒ Object



48
49
50
51
52
# File 'lib/tabletop/token.rb', line 48

def raise_if_over_max(value)
  if @max
    raise ExceedMaxTokensError if value > @max
  end 
end

#refreshObject

Raises:

  • (NoMethodError)


83
84
85
86
# File 'lib/tabletop/token.rb', line 83

def refresh
  raise NoMethodError if @max.nil?
  @count = @max
end

#remove(n = 1) ⇒ Object

Raises NotEnoughTokensError if there aren’t enough tokens to remove

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
# File 'lib/tabletop/token.rb', line 55

def remove(n=1)
  raise ArgumentError unless n.respond_to?(:to_i)
  n = n.to_i
  raise ArgumentError if n < 0
  if n > @count
    raise NotEnoughTokensError.new(n, @count)
  end
  @count -= n
end