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.



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

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



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

def pieces
  @pieces
end

Instance Method Details

#as_jsonHash

A hashed serialized representation of off board.

Returns:

  • (Hash)


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

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

#hittable?(_ = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def hittable?(_=nil)
  false
end

#numberString

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

Returns:

  • (String)


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

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)


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

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:



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

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:



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

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