Sha256: 8b2a5fb16b236d644dbf068915488411d0d1953a1be4e05893527eb9c3420ecb

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

require 'just_backgammon/common'

module JustBackgammon

  # = Die
  #
  # The die is a cube that be rolled to result in a number from 1 to 6.
  class Die
    extend Common

    # 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)
    def initialize(id: , number:)
      @id = id
      @number = number
    end

    # @return [Fixnum] the identifier of the die.
    attr_reader :id

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

    # Rolls the die, the number will be between 1 and 6.
    #
    # @return [Fixnum]
    def roll
      @number = Random.new.rand(6) + 1
    end

    # Resets the die, the number will be nil.
    #
    # @return [NilClass]
    def reset
      @number = nil
    end

    # Equals operator compares the number to determine if the dice are equal.
    #
    # @return [Boolean]
    def == (other)
      self.number == other.number
    end

    # A hashed serialized representation of the die
    #
    # @return [Hash]
    def as_json
      { id: id, number: number }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
just_backgammon-1.1.0 lib/just_backgammon/die.rb
just_backgammon-1.0.2 lib/just_backgammon/die.rb
just_backgammon-1.0.1 lib/just_backgammon/die.rb