Class: JustBackgammon::DiceSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Common
Defined in:
lib/just_backgammon/dice_set.rb

Overview

DiceSet

The collections of dice.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

load

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

#diceArray<Dice> (readonly)



29
30
31
# File 'lib/just_backgammon/dice_set.rb', line 29

def dice
  @dice
end

Instance Method Details

#as_jsonHash

A hashed serialized representation of the dice set.



71
72
73
# File 'lib/just_backgammon/dice_set.rb', line 71

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

#numbersArray<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

#resetArray<Die>

sets all dice numbers to nil and reduces the number of dice to two.



61
62
63
64
65
66
# File 'lib/just_backgammon/dice_set.rb', line 61

def reset
  @dice.each(&:reset)
  if @dice.size > 2
    @dice.slice!(-2..-1)
  end
end

#rollArray<Die>

randomizes each die and duplicates if they are the same number.



51
52
53
54
55
56
# File 'lib/just_backgammon/dice_set.rb', line 51

def roll
  @dice.each(&:roll)
  if @dice.first == @dice.last
    @dice.concat(@dice)
  end
end