Class: JustBackgammon::Move
- Inherits:
-
Object
- Object
- JustBackgammon::Move
- Extended by:
- Forwardable
- Defined in:
- lib/just_backgammon/move.rb
Overview
Move
A move from a point to a point.
Instance Attribute Summary collapse
-
#from ⇒ Point
readonly
The point where the move starts.
-
#to ⇒ Point
readonly
The point where the move ends.
Instance Method Summary collapse
-
#absolute_distance_for_player(player_number) ⇒ Fixnum
The absolute distance of the move for the specified player.
-
#bear_off? ⇒ Boolean
Checks if the move is bearing off.
-
#blocked?(player_number) ⇒ Boolean
Checks if the move is blocked.
-
#distance_for_player(player_number) ⇒ Fixnum
The distance of the move for the specified player.
-
#from_bar? ⇒ Boolean
Checks if the move is from the bar.
-
#initialize(from:, to:) ⇒ Move
constructor
A new instance of Move.
-
#missing_point? ⇒ Boolean
Checks if the move is missing points.
-
#to_point? ⇒ Boolean
Checks if the move is to a point.
-
#wrong_direction?(player_number) ⇒ Boolean
Checks if the move is in the wrong direction for the specified player.
Constructor Details
#initialize(from:, to:) ⇒ Move
A new instance of Move.
Example:
# Instantiates a new Bar
JustBackgammon::Move.new({from: point_a, to: point_b})
22 23 24 25 |
# File 'lib/just_backgammon/move.rb', line 22 def initialize(from: , to:) @from = from @to = to end |
Instance Attribute Details
#from ⇒ Point (readonly)
Returns the point where the move starts.
28 29 30 |
# File 'lib/just_backgammon/move.rb', line 28 def from @from end |
#to ⇒ Point (readonly)
Returns the point where the move ends.
31 32 33 |
# File 'lib/just_backgammon/move.rb', line 31 def to @to end |
Instance Method Details
#absolute_distance_for_player(player_number) ⇒ Fixnum
The absolute distance of the move for the specified player.
56 57 58 |
# File 'lib/just_backgammon/move.rb', line 56 def absolute_distance_for_player(player_number) distance_for_player(player_number).abs end |
#bear_off? ⇒ Boolean
Checks if the move is bearing off.
110 111 112 |
# File 'lib/just_backgammon/move.rb', line 110 def bear_off? to.instance_of?(JustBackgammon::OffBoard) end |
#blocked?(player_number) ⇒ Boolean
Checks if the move is blocked.
77 78 79 80 81 82 83 84 |
# 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) ⇒ Fixnum
The distance of the move for the specified player.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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? ⇒ Boolean
Checks if the move is from the bar.
96 97 98 |
# File 'lib/just_backgammon/move.rb', line 96 def from.instance_of?(JustBackgammon::Bar) end |
#missing_point? ⇒ Boolean
Checks if the move is missing points.
89 90 91 |
# File 'lib/just_backgammon/move.rb', line 89 def missing_point? from.nil? || to.nil? end |
#to_point? ⇒ Boolean
Checks if the move is to a point.
103 104 105 |
# File 'lib/just_backgammon/move.rb', line 103 def to_point? to.instance_of?(JustBackgammon::Point) end |
#wrong_direction?(player_number) ⇒ Boolean
Checks if the move is in the wrong direction for the specified player.
63 64 65 66 67 68 69 70 71 72 |
# 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 |