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])

Parameters:

  • moves (Array<Move>)

    All the moves.



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

def initialize(moves:)
  @moves = moves
end

Instance Attribute Details

#movesArray<Move> (readonly)

Returns all the moves.

Returns:

  • (Array<Move>)

    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.

Returns:

  • (Array<Fixnum>)


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.

Returns:

  • (Boolean)


107
108
109
# File 'lib/just_backgammon/move_list.rb', line 107

def all_moves_from_bar?
  moves.all? { |m| m.from_bar? }
end

#any_bar_empty_for_player?(current_player_number) ⇒ Boolean

Checks if any move is from an empty bar.

Returns:

  • (Boolean)


79
80
81
# File 'lib/just_backgammon/move_list.rb', line 79

def any_bar_empty_for_player?(current_player_number)
  moves.any? { |m| m.from_bar? && m.empty_for_player?(current_player_number) }
end

#any_bear_off?Boolean

Checks if any move is bearing off.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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_movesArray<CombinedMove>

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

Returns:



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

Returns:

  • (Boolean)


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_barFixnum

The number of moves from the bar.

Returns:

  • (Fixnum)


114
115
116
# File 'lib/just_backgammon/move_list.rb', line 114

def number_of_moves_from_bar
  moves.select { |m| m.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.

Returns:

  • (Boolean)


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