Class: Sashite::Pan::Action::Special
- Inherits:
-
Object
- Object
- Sashite::Pan::Action::Special
- Defined in:
- lib/sashite/pan/action/special.rb
Overview
Special action class
Handles special move actions with implicit side effects. Used for moves like castling or en passant where additional transformations occur beyond the primary movement.
Format: <source>~<destination> Examples: “e1~g1” (castling), “e5~f6” (en passant)
Constant Summary collapse
- TYPE =
Action type
:special- OPERATOR =
Operator constant
"~"- TRANSFORMATION_SEPARATOR =
Transformation separator
"="- ERROR_INVALID_SPECIAL =
Error messages
"Invalid special 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) ⇒ Special
Parse a special notation string into a Special instance.
-
.valid?(pan_string) ⇒ Boolean
Check if a string represents a valid special 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) ⇒ Special
constructor
Create a new special 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) ⇒ Special
Create a new special action instance
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/sashite/pan/action/special.rb', line 118 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.
37 38 39 |
# File 'lib/sashite/pan/action/special.rb', line 37 def destination @destination end |
#source ⇒ String (readonly)
Returns source CELL coordinate.
34 35 36 |
# File 'lib/sashite/pan/action/special.rb', line 34 def source @source end |
#transformation ⇒ String? (readonly)
Returns optional EPIN transformation.
40 41 42 |
# File 'lib/sashite/pan/action/special.rb', line 40 def transformation @transformation end |
Class Method Details
.parse(pan_string) ⇒ Special
Parse a special notation string into a Special instance
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sashite/pan/action/special.rb', line 89 def self.parse(pan_string) raise ::ArgumentError, format(ERROR_INVALID_SPECIAL, 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 special action
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 77 78 |
# File 'lib/sashite/pan/action/special.rb', line 51 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
235 236 237 238 239 240 241 |
# File 'lib/sashite/pan/action/special.rb', line 235 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
178 179 180 |
# File 'lib/sashite/pan/action/special.rb', line 178 def capture? false end |
#drop? ⇒ Boolean
Check if this is a drop action
199 200 201 |
# File 'lib/sashite/pan/action/special.rb', line 199 def drop? false end |
#drop_action? ⇒ Boolean
Check if this is a drop action (drop or drop_capture)
227 228 229 |
# File 'lib/sashite/pan/action/special.rb', line 227 def drop_action? false end |
#drop_capture? ⇒ Boolean
Check if this is a drop capture action
206 207 208 |
# File 'lib/sashite/pan/action/special.rb', line 206 def drop_capture? false end |
#hash ⇒ Integer
Custom hash implementation for use in collections
249 250 251 |
# File 'lib/sashite/pan/action/special.rb', line 249 def hash [self.class, source, destination, transformation].hash end |
#modify? ⇒ Boolean
Check if this is a modify action
213 214 215 |
# File 'lib/sashite/pan/action/special.rb', line 213 def modify? false end |
#move? ⇒ Boolean
Check if this is a move action
171 172 173 |
# File 'lib/sashite/pan/action/special.rb', line 171 def move? false end |
#movement? ⇒ Boolean
Check if this is a movement action
220 221 222 |
# File 'lib/sashite/pan/action/special.rb', line 220 def movement? true end |
#pass? ⇒ Boolean
Check if this is a pass action
164 165 166 |
# File 'lib/sashite/pan/action/special.rb', line 164 def pass? false end |
#piece ⇒ nil
Get the piece identifier
145 146 147 |
# File 'lib/sashite/pan/action/special.rb', line 145 def piece nil end |
#special? ⇒ Boolean
Check if this is a special action
185 186 187 |
# File 'lib/sashite/pan/action/special.rb', line 185 def special? true end |
#static_capture? ⇒ Boolean
Check if this is a static capture action
192 193 194 |
# File 'lib/sashite/pan/action/special.rb', line 192 def static_capture? false end |
#to_s ⇒ String
Convert the action to its PAN string representation
155 156 157 158 159 |
# File 'lib/sashite/pan/action/special.rb', line 155 def to_s result = "#{source}#{OPERATOR}#{destination}" result += "#{TRANSFORMATION_SEPARATOR}#{transformation}" if transformation result end |
#type ⇒ Symbol
Get the action type
138 139 140 |
# File 'lib/sashite/pan/action/special.rb', line 138 def type TYPE end |