Class: JustBackgammon::Move

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

Overview

Move

A move from a point to a point.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • from (Point)

    The point where the move starts.

  • to (Point)

    The point where the move ends.



22
23
24
25
# File 'lib/just_backgammon/move.rb', line 22

def initialize(from: , to:)
  @from = from
  @to = to
end

Instance Attribute Details

#fromPoint (readonly)

Returns the point where the move starts.

Returns:

  • (Point)

    the point where the move starts



28
29
30
# File 'lib/just_backgammon/move.rb', line 28

def from
  @from
end

#toPoint (readonly)

Returns the point where the move ends.

Returns:

  • (Point)

    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.

Returns:

  • (Fixnum)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Fixnum)


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.

Returns:

  • (Boolean)


96
97
98
# File 'lib/just_backgammon/move.rb', line 96

def from_bar?
  from.instance_of?(JustBackgammon::Bar)
end

#missing_point?Boolean

Checks if the move is missing points.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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