Class: JustBackgammon::MoveList

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

Overview

MoveList

A list of moves.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(moves:) ⇒ MoveList

A new instance of MoveList.

Example:

# Instantiates a new MoveList
JustBackgammon::MoveList.new([move_a, move_b])


20
21
22
# File 'lib/just_backgammon/move_list.rb', line 20

def initialize(moves:)
  @moves = moves
end

Instance Attribute Details

#movesArray<Move> (readonly)



25
26
27
# File 'lib/just_backgammon/move_list.rb', line 25

def moves
  @moves
end

Instance Method Details

#absolute_distances(current_player_number) ⇒ Array<Fixnum>

How far each of the moves go.



121
122
123
# 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?Boolean

Checks if all moves are from the bar.



107
108
109
# 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) ⇒ Boolean

Checks if any move is from an empty bar.



79
80
81
# 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?Boolean

Checks if any move is bearing off.



100
101
102
# File 'lib/just_backgammon/move_list.rb', line 100

def any_bear_off?
  moves.any?(&:bear_off?)
end

#any_blocked?(current_player_number) ⇒ Boolean

Checks if any move is blocked from moving.



86
87
88
# 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?Boolean

Checks if any move have no points.



58
59
60
# File 'lib/just_backgammon/move_list.rb', line 58

def any_missing_point?
  moves.any?(&:missing_point?)
end

#any_point_empty?Boolean

Checks if any move have empty points.



65
66
67
# 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) ⇒ Boolean

Checks if any move have is owned by the opponent.



72
73
74
# 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) ⇒ Boolean

Checks if any move is going the wrong direction.



93
94
95
# 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) ⇒ Boolean

Checks if they player cannot bear off.



137
138
139
# 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_movesArray<CombinedMove>

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Boolean

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



144
145
146
# 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_barFixnum

The number of moves from the bar.



114
115
116
# 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?Boolean

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



51
52
53
# 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) ⇒ Boolean

Checks if there are still pieces on the bar.



128
129
130
131
132
# 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