Class: JustBackgammon::Die
- Inherits:
-
Object
- Object
- JustBackgammon::Die
- 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
-
#id ⇒ Fixnum
readonly
The identifier of the die.
-
#number ⇒ Fixnum
readonly
The number of the die.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equals operator compares the number to determine if the dice are equal.
-
#as_json ⇒ Hash
A hashed serialized representation of the die.
-
#initialize(id:, number:) ⇒ Die
constructor
A new instance of Die.
-
#reset ⇒ NilClass
Resets the die, the number will be nil.
-
#roll ⇒ Fixnum
Rolls the die, the number will be between 1 and 6.
Methods included from Common
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
#id ⇒ Fixnum (readonly)
28 29 30 |
# File 'lib/just_backgammon/die.rb', line 28 def id @id end |
#number ⇒ Fixnum (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_json ⇒ Hash
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 |
#reset ⇒ NilClass
Resets the die, the number will be nil.
43 44 45 |
# File 'lib/just_backgammon/die.rb', line 43 def reset @number = nil end |
#roll ⇒ Fixnum
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 |