Class: Sashite::Pan::Action::Move
- Inherits:
-
Object
- Object
- Sashite::Pan::Action::Move
- 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
-
#destination ⇒ String
readonly
Destination CELL coordinate.
-
#source ⇒ String
readonly
Source CELL coordinate.
-
#transformation ⇒ String?
readonly
Optional EPIN transformation.
Class Method Summary collapse
-
.parse(pan_string) ⇒ Move
Parse a move notation string into a Move instance.
-
.valid?(pan_string) ⇒ Boolean
Check if a string represents a valid move action.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Custom equality comparison.
-
#capture? ⇒ Boolean
Check if this is a capture action.
-
#drop? ⇒ Boolean
Check if this is a drop action.
-
#drop_action? ⇒ Boolean
Check if this is a drop action (drop or drop_capture).
-
#drop_capture? ⇒ Boolean
Check if this is a drop capture action.
-
#hash ⇒ Integer
Custom hash implementation for use in collections.
-
#initialize(source, destination, transformation: nil) ⇒ Move
constructor
Create a new move action instance.
-
#modify? ⇒ Boolean
Check if this is a modify action.
-
#move? ⇒ Boolean
Check if this is a move action.
-
#movement? ⇒ Boolean
Check if this is a movement action.
-
#pass? ⇒ Boolean
Check if this is a pass action.
-
#piece ⇒ nil
Get the piece identifier.
-
#special? ⇒ Boolean
Check if this is a special action.
-
#static_capture? ⇒ Boolean
Check if this is a static capture action.
-
#to_s ⇒ String
Convert the action to its PAN string representation.
-
#type ⇒ Symbol
Get the action type.
Constructor Details
#initialize(source, destination, transformation: nil) ⇒ Move
Create a new move action instance
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
#destination ⇒ String (readonly)
Returns destination CELL coordinate.
35 36 37 |
# File 'lib/sashite/pan/action/move.rb', line 35 def destination @destination end |
#source ⇒ String (readonly)
Returns source CELL coordinate.
32 33 34 |
# File 'lib/sashite/pan/action/move.rb', line 32 def source @source end |
#transformation ⇒ String? (readonly)
Returns 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
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
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
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
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
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)
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
204 205 206 |
# File 'lib/sashite/pan/action/move.rb', line 204 def drop_capture? false end |
#hash ⇒ Integer
Custom hash implementation for use in collections
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
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
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
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
162 163 164 |
# File 'lib/sashite/pan/action/move.rb', line 162 def pass? false end |
#piece ⇒ nil
Get the 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
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
190 191 192 |
# File 'lib/sashite/pan/action/move.rb', line 190 def static_capture? false end |
#to_s ⇒ String
Convert the action to its PAN string representation
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 |
#type ⇒ Symbol
Get the action type
136 137 138 |
# File 'lib/sashite/pan/action/move.rb', line 136 def type TYPE end |