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}]
})

Parameters:

  • dice (Array<Hash>)

    All the dice in the set, each with a number.



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)

Returns allthe dice in the set, each with a number.

Returns:

  • (Array<Dice>)

    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_jsonHash

A hashed serialized representation of the dice set.

Returns:

  • (Hash)


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.

Returns:



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.

Returns:

  • (Array<Fixnum>)


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.

Returns:



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

#rollArray<Die>

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

Returns:



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