Sha256: 012f9fd6291ca17ed9bc3560aac459d40d9ddc570c3663b1bca0b49abee7c8df
Contents?: true
Size: 1.68 KB
Versions: 3
Compression:
Stored size: 1.68 KB
Contents
require 'just_backgammon/off_board' 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 owner. # # ==== Example: # # Instantiates a new OffBoard # JustBackgammon::OffBoard.new({ # pieces: [{owner: 1}, {owner: 1}] # }) def initialize(pieces:) @pieces = Piece.load(pieces) end # @return [Array<Piece>] all the pieces on the bar, each piece has an owner 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.owner == 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 # A hashed serialized representation of off board. # # @return [Hash] def as_json { pieces: pieces.map(&:as_json) } end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
just_backgammon-0.1.2 | lib/just_backgammon/off_board.rb |
just_backgammon-0.1.1 | lib/just_backgammon/off_board.rb |
just_backgammon-0.1.0 | lib/just_backgammon/off_board.rb |