Class: JustBackgammon::DiceSet
- Inherits:
-
Object
- Object
- JustBackgammon::DiceSet
- Extended by:
- Forwardable, Common
- Defined in:
- lib/just_backgammon/dice_set.rb
Overview
DiceSet
The collections of dice.
Instance Attribute Summary collapse
-
#dice ⇒ Array<Dice>
readonly
Allthe dice in the set, each with a number.
Instance Method Summary collapse
-
#as_json ⇒ Hash
A hashed serialized representation of the dice set.
-
#find_by_number(number) ⇒ Die
finds a die that matches the specified number.
-
#initialize(dice:) ⇒ DiceSet
constructor
A new instance of DiceSet.
-
#numbers ⇒ Array<Fixnum>
returns an array of the numbers on the dice.
-
#reset ⇒ Array<Die>
sets all dice numbers to nil and reduces the number of dice to two.
-
#roll ⇒ Array<Die>
randomizes each die and duplicates if they are the same number.
Methods included from Common
Constructor Details
#initialize(dice:) ⇒ DiceSet
A new instance of DiceSet.
Example:
# Instantiates a new DiceSet
JustBackgammon::DiceSet.new({
dice: [{number: 1}, {number: 2}]
})
24 25 26 |
# File 'lib/just_backgammon/dice_set.rb', line 24 def initialize(dice:) @dice = Die.load(dice) end |
Instance Attribute Details
#dice ⇒ Array<Dice> (readonly)
Returns allthe dice in the set, each with a number.
29 30 31 |
# File 'lib/just_backgammon/dice_set.rb', line 29 def dice @dice end |
Instance Method Details
#as_json ⇒ Hash
A hashed serialized representation of the dice set.
72 73 74 |
# File 'lib/just_backgammon/dice_set.rb', line 72 def as_json dice.map(&:as_json) end |
#find_by_number(number) ⇒ Die
finds a die that matches the specified number.
37 38 39 |
# File 'lib/just_backgammon/dice_set.rb', line 37 def find_by_number(number) @dice.find { |d| d.number == number } end |
#numbers ⇒ Array<Fixnum>
returns an array of the numbers on the dice.
44 45 46 |
# File 'lib/just_backgammon/dice_set.rb', line 44 def numbers @dice.map(&:number) end |
#reset ⇒ Array<Die>
sets all dice numbers to nil and reduces the number of dice to two.
62 63 64 65 66 67 |
# File 'lib/just_backgammon/dice_set.rb', line 62 def reset @dice.each(&:reset) if @dice.size > 2 @dice.slice!(-2..-1) end end |
#roll ⇒ Array<Die>
randomizes each die and duplicates if they are the same number.
51 52 53 54 55 56 57 |
# File 'lib/just_backgammon/dice_set.rb', line 51 def roll @dice.each(&:roll) if @dice.first == @dice.last dup_dice = @dice.map { |d| Die.new(id: d.id + 2, number: d.number) } @dice.concat(dup_dice) end end |