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(id:, number:) ⇒ Die

A new instance of Die.

Example:

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


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

def initialize(id: , number:)
  @id = id
  @number = number
end

Instance Attribute Details

#idFixnum (readonly)



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

def id
  @id
end

#numberFixnum (readonly)



31
32
33
# File 'lib/just_backgammon/die.rb', line 31

def number
  @number
end

Instance Method Details

#==(other) ⇒ Boolean

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



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

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

#as_jsonHash

A hashed serialized representation of the die



57
58
59
# File 'lib/just_backgammon/die.rb', line 57

def as_json
  { id: id, number: number }
end

#resetNilClass

Resets the die, the number will be nil.



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

def reset
  @number = nil
end

#rollFixnum

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



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

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