Class: JustBackgammon::Point

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

Overview

Point

A point where pieces can land on. Each one has a number.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

load

Constructor Details

#initialize(pieces:, number:) ⇒ Point

A new instance of Point.

Example:

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

Parameters:

  • pieces (Array<Hash>)

    All the pieces on this point.

  • number (Fixnum)

    The point number and identifier.



28
29
30
31
# File 'lib/just_backgammon/point.rb', line 28

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

Instance Attribute Details

#numberFixnum (readonly)

Returns the point number and identifier.

Returns:

  • (Fixnum)

    the point number and identifier



37
38
39
# File 'lib/just_backgammon/point.rb', line 37

def number
  @number
end

#piecesArray<Piece> (readonly)

Returns all the pieces on this point.

Returns:

  • (Array<Piece>)

    all the pieces on this point



34
35
36
# File 'lib/just_backgammon/point.rb', line 34

def pieces
  @pieces
end

Instance Method Details

#as_jsonHash

A hashed serialized representation of the bar.

Returns:

  • (Hash)


101
102
103
# File 'lib/just_backgammon/point.rb', line 101

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

#blocked?Boolean

Checks if point has more than one piece.

Returns:

  • (Boolean)


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

def blocked?
  size > 1
end

#blot?Boolean

Checks if point has only one piece.

Returns:

  • (Boolean)


80
81
82
# File 'lib/just_backgammon/point.rb', line 80

def blot?
  size == 1
end

#home?(player_number) ⇒ Boolean

Checks if point is a home point for specified player.

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
# File 'lib/just_backgammon/point.rb', line 87

def home?(player_number)
  case player_number
  when 1
    (19..24).include?(number)
  when 2
    (1..6).include?(number)
  else
    true
  end
end

#owned_by_opponent?(player_number) ⇒ Boolean

Checks if point has pieces owned by the opponent of the specified player.

Returns:

  • (Boolean)


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

def owned_by_opponent?(player_number)
  pieces.any? { |p| p.owner != player_number }
end

#owned_by_player?(player_number) ⇒ Boolean

Checks if point has pieces owned by the specified player.

Returns:

  • (Boolean)


59
60
61
# File 'lib/just_backgammon/point.rb', line 59

def owned_by_player?(player_number)
  pieces.any? { |p| p.owner == player_number }
end

#popPiece, NilClass

Removes a piece and returns it.

Returns:



45
46
47
# File 'lib/just_backgammon/point.rb', line 45

def pop
  @pieces.pop
end

#push(piece) ⇒ Array<Piece>

Adds a piece to the point.

Returns:



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

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