Class: Sashite::Pan::Action::Move

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/pan/action/move.rb

Overview

Move action class

Handles move actions to empty squares, with optional transformation.

Format: <source>-<destination> Examples: “e2-e4”, “e7-e8=Q”, “a7-a8=+R”

Constant Summary collapse

TYPE =

Action type

:move
OPERATOR =

Operator constant

"-"
TRANSFORMATION_SEPARATOR =

Transformation separator

"="
ERROR_INVALID_MOVE =

Error messages

"Invalid move notation: %s"
ERROR_INVALID_SOURCE =
"Invalid source coordinate: %s"
ERROR_INVALID_DESTINATION =
"Invalid destination coordinate: %s"
ERROR_INVALID_TRANSFORMATION =
"Invalid transformation piece: %s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, transformation: nil) ⇒ Move

Create a new move action instance

Examples:

Move.new("e2", "e4")                        # => #<Move ...>
Move.new("e7", "e8", transformation: "Q")   # => #<Move ...>

Parameters:

  • source (String)

    source CELL coordinate

  • destination (String)

    destination CELL coordinate

  • transformation (String, nil) (defaults to: nil)

    optional EPIN transformation

Raises:

  • (ArgumentError)

    if coordinates or transformation are invalid



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sashite/pan/action/move.rb', line 116

def initialize(source, destination, transformation: nil)
  raise ::ArgumentError, format(ERROR_INVALID_SOURCE, source) unless ::Sashite::Cell.valid?(source)
  unless ::Sashite::Cell.valid?(destination)
    raise ::ArgumentError, format(ERROR_INVALID_DESTINATION, destination)
  end

  if transformation && !::Sashite::Epin.valid?(transformation)
    raise ::ArgumentError, format(ERROR_INVALID_TRANSFORMATION, transformation)
  end

  @source = source
  @destination = destination
  @transformation = transformation

  freeze
end

Instance Attribute Details

#destinationString (readonly)

Returns destination CELL coordinate.

Returns:

  • (String)

    destination CELL coordinate



35
36
37
# File 'lib/sashite/pan/action/move.rb', line 35

def destination
  @destination
end

#sourceString (readonly)

Returns source CELL coordinate.

Returns:

  • (String)

    source CELL coordinate



32
33
34
# File 'lib/sashite/pan/action/move.rb', line 32

def source
  @source
end

#transformationString? (readonly)

Returns optional EPIN transformation.

Returns:

  • (String, nil)

    optional EPIN transformation



38
39
40
# File 'lib/sashite/pan/action/move.rb', line 38

def transformation
  @transformation
end

Class Method Details

.parse(pan_string) ⇒ Move

Parse a move notation string into a Move instance

Examples:

Move.parse("e2-e4")      # => #<Move source="e2" destination="e4">
Move.parse("e7-e8=Q")    # => #<Move source="e7" destination="e8" transformation="Q">

Parameters:

  • pan_string (String)

    move notation string

Returns:

  • (Move)

    move action instance

Raises:

  • (ArgumentError)

    if the string is not valid move notation



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sashite/pan/action/move.rb', line 87

def self.parse(pan_string)
  raise ::ArgumentError, format(ERROR_INVALID_MOVE, pan_string) unless valid?(pan_string)

  parts = pan_string.split(OPERATOR, 2)
  source = parts[0]
  dest_and_transform = parts[1]

  if dest_and_transform.include?(TRANSFORMATION_SEPARATOR)
    dest_parts = dest_and_transform.split(TRANSFORMATION_SEPARATOR, 2)
    destination = dest_parts[0]
    transformation = dest_parts[1]
  else
    destination = dest_and_transform
    transformation = nil
  end

  new(source, destination, transformation: transformation)
end

.valid?(pan_string) ⇒ Boolean

Check if a string represents a valid move action

Examples:

Move.valid?("e2-e4")       # => true
Move.valid?("e7-e8=Q")     # => true
Move.valid?("e2+e4")       # => false

Parameters:

  • pan_string (String)

    the string to validate

Returns:

  • (Boolean)

    true if valid move notation



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sashite/pan/action/move.rb', line 49

def self.valid?(pan_string)
  return false unless pan_string.is_a?(::String)
  return false unless pan_string.include?(OPERATOR)

  parts = pan_string.split(OPERATOR, 2)
  return false if parts.size != 2

  source_part = parts[0]
  dest_and_transform = parts[1]

  return false unless ::Sashite::Cell.valid?(source_part)

  # Check if there's a transformation
  if dest_and_transform.include?(TRANSFORMATION_SEPARATOR)
    dest_parts = dest_and_transform.split(TRANSFORMATION_SEPARATOR, 2)
    return false if dest_parts.size != 2

    destination_part = dest_parts[0]
    transformation_part = dest_parts[1]

    return false unless ::Sashite::Cell.valid?(destination_part)
    return false unless ::Sashite::Epin.valid?(transformation_part)
  else
    return false unless ::Sashite::Cell.valid?(dest_and_transform)
  end

  true
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Custom equality comparison

Parameters:

  • other (Object)

    object to compare with

Returns:

  • (Boolean)

    true if actions are equal



233
234
235
236
237
238
239
# File 'lib/sashite/pan/action/move.rb', line 233

def ==(other)
  return false unless other.is_a?(self.class)

  source == other.source &&
    destination == other.destination &&
    transformation == other.transformation
end

#capture?Boolean

Check if this is a capture action

Returns:

  • (Boolean)

    false



176
177
178
# File 'lib/sashite/pan/action/move.rb', line 176

def capture?
  false
end

#drop?Boolean

Check if this is a drop action

Returns:

  • (Boolean)

    false



197
198
199
# File 'lib/sashite/pan/action/move.rb', line 197

def drop?
  false
end

#drop_action?Boolean

Check if this is a drop action (drop or drop_capture)

Returns:

  • (Boolean)

    false



225
226
227
# File 'lib/sashite/pan/action/move.rb', line 225

def drop_action?
  false
end

#drop_capture?Boolean

Check if this is a drop capture action

Returns:

  • (Boolean)

    false



204
205
206
# File 'lib/sashite/pan/action/move.rb', line 204

def drop_capture?
  false
end

#hashInteger

Custom hash implementation for use in collections

Returns:

  • (Integer)

    hash value



247
248
249
# File 'lib/sashite/pan/action/move.rb', line 247

def hash
  [self.class, source, destination, transformation].hash
end

#modify?Boolean

Check if this is a modify action

Returns:

  • (Boolean)

    false



211
212
213
# File 'lib/sashite/pan/action/move.rb', line 211

def modify?
  false
end

#move?Boolean

Check if this is a move action

Returns:

  • (Boolean)

    true



169
170
171
# File 'lib/sashite/pan/action/move.rb', line 169

def move?
  true
end

#movement?Boolean

Check if this is a movement action

Returns:

  • (Boolean)

    true



218
219
220
# File 'lib/sashite/pan/action/move.rb', line 218

def movement?
  true
end

#pass?Boolean

Check if this is a pass action

Returns:

  • (Boolean)

    false



162
163
164
# File 'lib/sashite/pan/action/move.rb', line 162

def pass?
  false
end

#piecenil

Get the piece identifier

Returns:

  • (nil)

    move actions have no piece identifier



143
144
145
# File 'lib/sashite/pan/action/move.rb', line 143

def piece
  nil
end

#special?Boolean

Check if this is a special action

Returns:

  • (Boolean)

    false



183
184
185
# File 'lib/sashite/pan/action/move.rb', line 183

def special?
  false
end

#static_capture?Boolean

Check if this is a static capture action

Returns:

  • (Boolean)

    false



190
191
192
# File 'lib/sashite/pan/action/move.rb', line 190

def static_capture?
  false
end

#to_sString

Convert the action to its PAN string representation

Examples:

action.to_s  # => "e2-e4" or "e7-e8=Q"

Returns:

  • (String)

    move notation



153
154
155
156
157
# File 'lib/sashite/pan/action/move.rb', line 153

def to_s
  result = "#{source}#{OPERATOR}#{destination}"
  result += "#{TRANSFORMATION_SEPARATOR}#{transformation}" if transformation
  result
end

#typeSymbol

Get the action type

Returns:

  • (Symbol)

    :move



136
137
138
# File 'lib/sashite/pan/action/move.rb', line 136

def type
  TYPE
end