Class: Dicey::AbstractDie

Inherits:
Object
  • Object
show all
Defined in:
lib/dicey/abstract_die.rb

Overview

Asbtract die which may have an arbitrary list of sides, not even neccessarily numbers, but strings or other objects.

Direct Known Subclasses

NumericDie

Constant Summary collapse

@@random =

Shared randomness source, accessed through rand and srand.

Random.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sides_list) ⇒ AbstractDie

Returns a new instance of AbstractDie.

Parameters:

  • sides_list (Enumerable<Any>)

Raises:



71
72
73
74
75
76
77
78
79
# File 'lib/dicey/abstract_die.rb', line 71

def initialize(sides_list)
  @sides_list = sides_list.to_a
  @sides_list = @sides_list.dup if @sides_list.equal?(sides_list)
  raise DiceyError, "dice must have at least one side!" if @sides_list.empty?

  @sides_list.freeze
  @sides_num = @sides_list.size
  @current_side_index = 0
end

Instance Attribute Details

#sides_listArray<Any> (readonly)

Die’s list of sides.

Returns:

  • (Array<Any>)


62
63
64
# File 'lib/dicey/abstract_die.rb', line 62

def sides_list
  @sides_list
end

#sides_numInteger (readonly)

Number of sides of the die.

Returns:

  • (Integer)


67
68
69
# File 'lib/dicey/abstract_die.rb', line 67

def sides_num
  @sides_num
end

Class Method Details

.describe(dice) ⇒ String

Get a text representation of a list of dice.

Parameters:

Returns:

  • (String)


34
35
36
37
38
# File 'lib/dicey/abstract_die.rb', line 34

def self.describe(dice)
  return dice.to_s if AbstractDie === dice

  dice.to_a.join("+")
end

.from_count(count, definition) ⇒ Array<AbstractDie>

Create a number of equal dice.

Parameters:

  • count (Integer)

    number of dice to create

  • definition (Enumerable<Any>, Any)

    definition suitable for the dice class

Returns:



55
56
57
# File 'lib/dicey/abstract_die.rb', line 55

def self.from_count(count, definition)
  Array.new(count) { new(definition) }
end

.from_list(*definitions) ⇒ Array<AbstractDie>

Create a bunch of different dice at once.

Parameters:

  • definitions (Array<Enumerable<Any>>, Array<Any>)

    list of definitions suitable for the dice class

Returns:



45
46
47
# File 'lib/dicey/abstract_die.rb', line 45

def self.from_list(*definitions)
  definitions.map { new(_1) }
end

.randObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a random value using a private instance of Random.

See Also:

  • Random#rand


12
13
14
# File 'lib/dicey/abstract_die.rb', line 12

def self.rand(...)
  @@random.rand(...)
end

.srandObject

Reset internal randomizer using a new seed.

See Also:

  • Random.new


18
19
20
# File 'lib/dicey/abstract_die.rb', line 18

def self.srand(...)
  @@random = Random.new(...)
end

Instance Method Details

#==(other) ⇒ Boolean

Determine if this die and the other one have the same list of sides. Be aware that differently ordered sides are not considered equal.

Parameters:

Returns:

  • (Boolean)


120
121
122
# File 'lib/dicey/abstract_die.rb', line 120

def ==(other)
  AbstractDie === other && same_sides?(other)
end

#currentAny

Get current side of the die.

Returns:

  • (Any)

    current side



84
85
86
# File 'lib/dicey/abstract_die.rb', line 84

def current
  @sides_list[@current_side_index]
end

#eql?(other) ⇒ Boolean

Determine if this die and the other one are of the same class and have the same list of sides. Be aware that differently ordered sides are not considered equal.

die_1.eql?(die_2) implies die_1.hash == die_2.hash.

Parameters:

Returns:

  • (Boolean)


132
133
134
# File 'lib/dicey/abstract_die.rb', line 132

def eql?(other)
  self.class === other && same_sides?(other)
end

#hashInteger

Generates an Integer hash value for this object.

Returns:

  • (Integer)


139
140
141
# File 'lib/dicey/abstract_die.rb', line 139

def hash
  [self.class, @sides_list].hash
end

#nextAny

Get next side of the die, advancing internal state. Starts from first side, wraps from last to first side.

Returns:

  • (Any)

    next side



92
93
94
95
96
# File 'lib/dicey/abstract_die.rb', line 92

def next
  ret = current
  @current_side_index = (@current_side_index + 1) % @sides_num
  ret
end

#rollAny

Move internal state to a random side.

Returns:

  • (Any)

    rolled side



101
102
103
104
# File 'lib/dicey/abstract_die.rb', line 101

def roll
  @current_side_index = self.class.rand(@sides_num)
  current
end

#to_sString

Return a string representing the die.

Default representation is a list of sides in round brackets.

Returns:

  • (String)


111
112
113
# File 'lib/dicey/abstract_die.rb', line 111

def to_s
  (@sides_list.size > 1) ? "(#{@sides_list.join(",")})" : "(#{@sides_list.first},)"
end