class JustBackgammon::Die

Die

The die is a cube that be rolled to result in a number from 1 to 6.

Attributes

id[R]

@return [Fixnum] the identifier of the die.

number[R]

@return [Fixnum] the number of the die. Returns nil if not yet rolled

Public Class Methods

new(id: , number:) click to toggle source

A new instance of Die.

@param [Fixnum] id

The identifier of the die.

@param [Fixnum, NilClass] number

The number of the die. Returns nil if not yet rolled.

Example:

# Instantiates a new Die
JustBackgammon::Die.new(id: 1, number: 1)
# File lib/just_backgammon/die.rb, line 22
def initialize(id: , number:)
  @id = id
  @number = number
end

Public Instance Methods

==(other) click to toggle source

Equals operator compares the number to determine if the dice are equal.

@return [Boolean]

# File lib/just_backgammon/die.rb, line 50
def == (other)
  self.number == other.number
end
as_json() click to toggle source

A hashed serialized representation of the die

@return [Hash]

# File lib/just_backgammon/die.rb, line 57
def as_json
  { id: id, number: number }
end
reset() click to toggle source

Resets the die, the number will be nil.

@return [NilClass]

# File lib/just_backgammon/die.rb, line 43
def reset
  @number = nil
end
roll() click to toggle source

Rolls the die, the number will be between 1 and 6.

@return [Fixnum]

# File lib/just_backgammon/die.rb, line 36
def roll
  @number = Random.new.rand(6) + 1
end