class JustBackgammon::Move

Move

A move from a point to a point.

Attributes

from[R]

@return [Point] the point where the move starts

to[R]

@return [Point] the point where the move ends

Public Class Methods

new(from: , to:) click to toggle source

A new instance of Move.

@param [Point] from

The point where the move starts.

@param [Point] to

The point where the move ends.

Example:

# Instantiates a new Bar
JustBackgammon::Move.new({from: point_a, to: point_b})
# File lib/just_backgammon/move.rb, line 22
def initialize(from: , to:)
  @from = from
  @to = to
end

Public Instance Methods

absolute_distance_for_player(player_number) click to toggle source

The absolute distance of the move for the specified player.

@return [Fixnum]

# File lib/just_backgammon/move.rb, line 56
def absolute_distance_for_player(player_number)
  distance_for_player(player_number).abs
end
bear_off?() click to toggle source

Checks if the move is bearing off.

@return [Boolean]

# File lib/just_backgammon/move.rb, line 110
def bear_off?
  to.instance_of?(JustBackgammon::OffBoard)
end
blocked?(player_number) click to toggle source

Checks if the move is blocked.

@return [Boolean]

# File lib/just_backgammon/move.rb, line 77
def blocked?(player_number)
  case to
  when OffBoard
    false
  else
    to.owned_by_opponent?(player_number) && to.blocked?
  end
end
distance_for_player(player_number) click to toggle source

The distance of the move for the specified player.

@return [Fixnum]

# File lib/just_backgammon/move.rb, line 38
def distance_for_player(player_number)
  case player_number
  when 1
    from_number = from.instance_of?(Bar) ? 0 : from.number
    to_number = to.instance_of?(OffBoard) ? 25 : to.number
    to_number - from_number
  when 2
    from_number = from.instance_of?(Bar) ? 25 : from.number
    to_number = to.instance_of?(OffBoard) ? 0 : to.number
    to_number - from_number
  else
    0
  end
end
from_bar?() click to toggle source

Checks if the move is from the bar.

@return [Boolean]

# File lib/just_backgammon/move.rb, line 96
def from_bar?
  from.instance_of?(JustBackgammon::Bar)
end
missing_point?() click to toggle source

Checks if the move is missing points.

@return [Boolean]

# File lib/just_backgammon/move.rb, line 89
def missing_point?
  from.nil? || to.nil?
end
to_point?() click to toggle source

Checks if the move is to a point.

@return [Boolean]

# File lib/just_backgammon/move.rb, line 103
def to_point?
  to.instance_of?(JustBackgammon::Point)
end
wrong_direction?(player_number) click to toggle source

Checks if the move is in the wrong direction for the specified player.

@return [Boolean]

# File lib/just_backgammon/move.rb, line 63
def wrong_direction?(player_number)
  case player_number
  when 1
    distance_for_player(player_number) < 0
  when 2
    distance_for_player(player_number) > 0
  else
    true
  end
end