Class: JustBackgammon::Die

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

Overview

Die

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

load

Constructor Details

#initialize(number:) ⇒ Die

A new instance of Die.

Example:

# Instantiates a new Die
JustBackgammon::Die.new(number: 1)


19
20
21
# File 'lib/just_backgammon/die.rb', line 19

def initialize(number:)
  @number = number
end

Instance Attribute Details

#numberFixnum (readonly)



24
25
26
# File 'lib/just_backgammon/die.rb', line 24

def number
  @number
end

Instance Method Details

#==(other) ⇒ Boolean

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



43
44
45
# File 'lib/just_backgammon/die.rb', line 43

def == (other)
  self.number == other.number
end

#as_jsonHash

A hashed serialized representation of the die



50
51
52
# File 'lib/just_backgammon/die.rb', line 50

def as_json
  { number: number }
end

#resetNilClass

Resets the die, the number will be nil.



36
37
38
# File 'lib/just_backgammon/die.rb', line 36

def reset
  @number = nil
end

#rollFixnum

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



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

def roll
  @number = Random.new.rand(6) + 1
end