Sha256: 4c01af0f7f07bf79ffebc5c816827a5a6dbd82c19f6621c7b9cdb8ed35a7e41d

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

require 'just_backgammon/piece'

module JustBackgammon

  # = OffBoard
  #
  # The off board is where pieces go when bearing off. Contains an array of pieces.
  class OffBoard
    extend Common

    # A new instance of OffBoard.
    #
    # @param [Array<Hash>] pieces
    #   All the pieces off board, each piece has an player_number.
    #
    # ==== Example:
    #   # Instantiates a new OffBoard
    #   JustBackgammon::OffBoard.new({
    #     pieces: [{player_number: 1}, {player_number: 1}]
    #   })
    def initialize(pieces:)
      @pieces = Piece.load(pieces)
    end

    # @return [Array<Piece>] all the pieces on the bar, each piece has an player_number
    attr_reader :pieces

    # The identifier of the bar, returns the string 'bar'.
    #
    # @return [String]
    def number
      'off_board'
    end

    # ALl the pieces owned by the specified player.
    #
    # @param [Fixnum] player_number
    #   the specified player number.
    #
    # @return [Array<Piece>]
    def pieces_owned_by_player(player_number)
      pieces.select { |p| p.player_number == player_number }
    end

    # Number of pieces owned by the specified player.
    #
    # @param [Fixnum] player_number
    #   the specified player number.
    #
    # @return [Fixnum]
    def number_of_pieces_owned_by_player(player_number)
      pieces_owned_by_player(player_number).size
    end

    # Adds a piece off board.
    #
    # @param [Piece] piece
    #   the piece to push off board.
    #
    # @return [Array<Piece>]
    def push(piece)
      @pieces.push(piece)
    end

    def hittable?(_=nil)
      false
    end

    # A hashed serialized representation of off board.
    #
    # @return [Hash]
    def as_json
      { pieces: pieces.map(&:as_json) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
just_backgammon-1.1.0 lib/just_backgammon/off_board.rb