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)

Parameters:

  • number (Fixnum, NilClass)

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



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

def initialize(number:)
  @number = number
end

Instance Attribute Details

#numberFixnum (readonly)

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

Returns:

  • (Fixnum)

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



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.

Returns:

  • (Boolean)


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

Returns:

  • (Hash)


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.

Returns:

  • (NilClass)


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.

Returns:

  • (Fixnum)


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

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