Class: JustBackgammon::MoveList
- Inherits:
-
Object
- Object
- JustBackgammon::MoveList
- Extended by:
- Forwardable
- Defined in:
- lib/just_backgammon/move_list.rb
Overview
MoveList
A list of moves.
Instance Attribute Summary collapse
-
#moves ⇒ Array<Move>
readonly
All the moves.
Instance Method Summary collapse
-
#absolute_distances(current_player_number) ⇒ Array<Fixnum>
How far each of the moves go.
-
#all_moves_from_bar? ⇒ Boolean
Checks if all moves are from the bar.
-
#any_bar_empty_for_player?(current_player_number) ⇒ Boolean
Checks if any move is from an empty bar.
-
#any_bear_off? ⇒ Boolean
Checks if any move is bearing off.
-
#any_blocked?(current_player_number) ⇒ Boolean
Checks if any move is blocked from moving.
-
#any_missing_point? ⇒ Boolean
Checks if any move have no points.
-
#any_point_empty? ⇒ Boolean
Checks if any move have empty points.
-
#any_point_owned_by_opponent?(current_player_number) ⇒ Boolean
Checks if any move have is owned by the opponent.
-
#any_wrong_direction?(current_player_number) ⇒ Boolean
Checks if any move is going the wrong direction.
-
#combined_moves ⇒ Array<CombinedMove>
Combines moves if there are any that start where another ends.
-
#dice_mismatch?(current_player_number, dice) ⇒ Boolean
Checks if any of the moves don’t match the dice rolls.
-
#initialize(moves:) ⇒ MoveList
constructor
A new instance of MoveList.
-
#number_of_moves_from_bar ⇒ Fixnum
The number of moves from the bar.
-
#piece_moves_multiple_times? ⇒ Boolean
For any of the combined moves, checks if there is one with more two legs or more.
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
#moves ⇒ Array<Move> (readonly)
Returns all the moves.
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 { |m| m.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 moves.all? { |m| m. } 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 (current_player_number) moves.any? { |m| m. && m.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? { |m| m.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? { |m| m.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? { |m| m.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? { |m| m.from_point? && m.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? { |m| m.from_point? && m.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? { |m| m.wrong_direction?(current_player_number) } end |
#combined_moves ⇒ Array<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, m| matching_move = combined.find_index { |c| c.last.number == m.from.number } if matching_move combined[matching_move].push(m.to) else combined.push([m.from, m.to]) end combined end combined_data.map { |legs| JustBackgammon::CombinedMove.new(legs: legs) } end |
#dice_mismatch?(current_player_number, dice) ⇒ Boolean
Checks if any of the moves don’t match the dice rolls
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/just_backgammon/move_list.rb', line 128 def dice_mismatch?(current_player_number, dice) unallocated = dice.numbers allocated = [] moves.each do |m| move_distance = m.absolute_distance_for_player(current_player_number) index = unallocated.index do |d| if m.bear_off? d >= move_distance else d == move_distance end end if index die = unallocated.delete_at(index) allocated.push(die) end end allocated.size != moves.size end |
#number_of_moves_from_bar ⇒ Fixnum
The number of moves from the bar.
114 115 116 |
# File 'lib/just_backgammon/move_list.rb', line 114 def moves.select { |m| m. }.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? { |c| c.size > 2 } end |