class JustBackgammon::MoveList

MoveList

A list of moves.

Attributes

moves[R]

@return [Array<Move>] all the moves

Public Class Methods

new(moves:) click to toggle source

A new instance of MoveList.

@param [Array<Move>] moves

All the moves.

Example:

# Instantiates a new MoveList
JustBackgammon::MoveList.new([move_a, move_b])
# File lib/just_backgammon/move_list.rb, line 20
def initialize(moves:)
  @moves = moves
end

Public Instance Methods

absolute_distances(current_player_number) click to toggle source

How far each of the moves go.

@return [Array<Fixnum>]

# File lib/just_backgammon/move_list.rb, line 121
def absolute_distances(current_player_number)
  moves.map { |move| move.absolute_distance_for_player(current_player_number) }
end
all_moves_from_bar?() click to toggle source

Checks if all moves are from the bar.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 107
def all_moves_from_bar?
  moves.all?(&:from_bar?)
end
any_bar_empty_for_player?(current_player_number) click to toggle source

Checks if any move is from an empty bar.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 79
def any_bar_empty_for_player?(current_player_number)
  moves.any? { |move| move.from_bar? && move.empty_for_player?(current_player_number) }
end
any_bear_off?() click to toggle source

Checks if any move is bearing off.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 100
def any_bear_off?
  moves.any?(&:bear_off?)
end
any_blocked?(current_player_number) click to toggle source

Checks if any move is blocked from moving.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 86
def any_blocked?(current_player_number)
  moves.any? { |move| move.blocked?(current_player_number) }
end
any_missing_point?() click to toggle source

Checks if any move have no points.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 58
def any_missing_point?
  moves.any?(&:missing_point?)
end
any_point_empty?() click to toggle source

Checks if any move have empty points.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 65
def any_point_empty?
  combined_moves.any? { |move| move.from_point? && move.empty? }
end
any_point_owned_by_opponent?(current_player_number) click to toggle source

Checks if any move have is owned by the opponent.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 72
def any_point_owned_by_opponent?(current_player_number)
  combined_moves.any? { |move| move.from_point? && move.owned_by_opponent?(current_player_number) }
end
any_wrong_direction?(current_player_number) click to toggle source

Checks if any move is going the wrong direction.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 93
def any_wrong_direction?(current_player_number)
  moves.any? { |move| move.wrong_direction?(current_player_number) }
end
cannot_bear_off?(current_player_number, points) click to toggle source

Checks if they player cannot bear off.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 137
def cannot_bear_off?(current_player_number, points)
  any_bear_off? && !(points.not_home(current_player_number).map(&:number) - map { |move| move.from.number }).empty?
end
combined_moves() click to toggle source

Combines moves if there are any that start where another ends.

@return [Array<CombinedMove>]

# File lib/just_backgammon/move_list.rb, line 33
def combined_moves
  combined_data = moves.inject([]) do |combined, move|
    matching_move = combined.find_index { |combined_move| combined_move.last.number == move.from.number }
    if matching_move
      combined[matching_move].push(move.to)
    else
      combined.push([move.from, move.to])
    end

    combined
  end

  combined_data.map { |legs| JustBackgammon::CombinedMove.new(legs: legs) }
end
dice_mismatch?(current_player_number, points, dice, bar) click to toggle source

Checks if any of the moves don't match the dice rolls

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 144
def dice_mismatch?(current_player_number, points, dice, bar)
  current_player_has_moves?(current_player_number, points, dice, bar) && moves_mismatch_dice?(current_player_number, dice)
end
number_of_moves_from_bar() click to toggle source

The number of moves from the bar.

@return [Fixnum]

# File lib/just_backgammon/move_list.rb, line 114
def number_of_moves_from_bar
  moves.select(&:from_bar?).size
end
piece_moves_multiple_times?() click to toggle source

For any of the combined moves, checks if there is one with more two legs or more.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 51
def piece_moves_multiple_times?
  combined_moves.any?(&:multi_leg?)
end
pieces_still_on_bar?(current_player_number, points, dice, bar) click to toggle source

Checks if there are still pieces on the bar.

@return [Boolean]

# File lib/just_backgammon/move_list.rb, line 128
def pieces_still_on_bar?(current_player_number, points, dice, bar)
  !all_moves_from_bar? && \
  number_of_moves_from_bar != bar.number_of_pieces_owned_by_player(current_player_number) && \
  points.destinations(bar, dice, current_player_number).size >= number_of_moves_from_bar
end