Class: JustBackgammon::OffBoard

Inherits:
Object
  • Object
show all
Extended by:
Common
Defined in:
lib/just_backgammon/off_board.rb

Overview

OffBoard

The off board is where pieces go when bearing off. Contains an array of pieces.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

load

Constructor Details

#initialize(pieces:) ⇒ OffBoard

A new instance of OffBoard.

Example:

# Instantiates a new OffBoard
JustBackgammon::OffBoard.new({
  pieces: [{owner: 1}, {owner: 1}]
})

Parameters:

  • pieces (Array<Hash>)

    All the pieces off board, each piece has an owner.



21
22
23
# File 'lib/just_backgammon/off_board.rb', line 21

def initialize(pieces:)
  @pieces = Piece.load(pieces)
end

Instance Attribute Details

#piecesArray<Piece> (readonly)

Returns all the pieces on the bar, each piece has an owner.

Returns:

  • (Array<Piece>)

    all the pieces on the bar, each piece has an owner



26
27
28
# File 'lib/just_backgammon/off_board.rb', line 26

def pieces
  @pieces
end

Instance Method Details

#as_jsonHash

A hashed serialized representation of off board.

Returns:

  • (Hash)


72
73
74
# File 'lib/just_backgammon/off_board.rb', line 72

def as_json
  { pieces: pieces.map(&:as_json) }
end

#hittable?(_ = nil) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/just_backgammon/off_board.rb', line 65

def hittable?(_=nil)
  false
end

#numberString

The identifier of the bar, returns the string ‘bar’.

Returns:

  • (String)


31
32
33
# File 'lib/just_backgammon/off_board.rb', line 31

def number
  'off_board'
end

#number_of_pieces_owned_by_player(player_number) ⇒ Fixnum

Number of pieces owned by the specified player.

Parameters:

  • player_number (Fixnum)

    the specified player number.

Returns:

  • (Fixnum)


51
52
53
# File 'lib/just_backgammon/off_board.rb', line 51

def number_of_pieces_owned_by_player(player_number)
  pieces_owned_by_player(player_number).size
end

#pieces_owned_by_player(player_number) ⇒ Array<Piece>

ALl the pieces owned by the specified player.

Parameters:

  • player_number (Fixnum)

    the specified player number.

Returns:



41
42
43
# File 'lib/just_backgammon/off_board.rb', line 41

def pieces_owned_by_player(player_number)
  pieces.select { |p| p.owner == player_number }
end

#push(piece) ⇒ Array<Piece>

Adds a piece off board.

Parameters:

  • piece (Piece)

    the piece to push off board.

Returns:



61
62
63
# File 'lib/just_backgammon/off_board.rb', line 61

def push(piece)
  @pieces.push(piece)
end