Class: Sashite::Pan::Action::Special

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new special action instance

Examples:

Special.new("e1", "g1")                      # => #<Special ...>
Special.new("e5", "f6", transformation: nil) # => #<Special ...>

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



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

#destinationString (readonly)

Returns destination CELL coordinate.

Returns:

  • (String)

    destination CELL coordinate



37
38
39
# File 'lib/sashite/pan/action/special.rb', line 37

def destination
  @destination
end

#sourceString (readonly)

Returns source CELL coordinate.

Returns:

  • (String)

    source CELL coordinate



34
35
36
# File 'lib/sashite/pan/action/special.rb', line 34

def source
  @source
end

#transformationString? (readonly)

Returns optional EPIN transformation.

Returns:

  • (String, nil)

    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

Examples:

Special.parse("e1~g1")      # => #<Special source="e1" destination="g1">
Special.parse("e5~f6")      # => #<Special source="e5" destination="f6">

Parameters:

  • pan_string (String)

    special notation string

Returns:

  • (Special)

    special action instance

Raises:

  • (ArgumentError)

    if the string is not valid special notation



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

Examples:

Special.valid?("e1~g1")       # => true
Special.valid?("e5~f6")       # => true
Special.valid?("e1-g1")       # => false

Parameters:

  • pan_string (String)

    the string to validate

Returns:

  • (Boolean)

    true if valid special notation



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

Parameters:

  • other (Object)

    object to compare with

Returns:

  • (Boolean)

    true if actions are equal



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

Returns:

  • (Boolean)

    false



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

Returns:

  • (Boolean)

    false



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)

Returns:

  • (Boolean)

    false



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

Returns:

  • (Boolean)

    false



206
207
208
# File 'lib/sashite/pan/action/special.rb', line 206

def drop_capture?
  false
end

#hashInteger

Custom hash implementation for use in collections

Returns:

  • (Integer)

    hash value



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

Returns:

  • (Boolean)

    false



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

Returns:

  • (Boolean)

    false



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

Returns:

  • (Boolean)

    true



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

Returns:

  • (Boolean)

    false



164
165
166
# File 'lib/sashite/pan/action/special.rb', line 164

def pass?
  false
end

#piecenil

Get the piece identifier

Returns:

  • (nil)

    special actions have no 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

Returns:

  • (Boolean)

    true



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

Returns:

  • (Boolean)

    false



192
193
194
# File 'lib/sashite/pan/action/special.rb', line 192

def static_capture?
  false
end

#to_sString

Convert the action to its PAN string representation

Examples:

action.to_s  # => "e1~g1" or "e5~f6"

Returns:

  • (String)

    special notation



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

#typeSymbol

Get the action type

Returns:

  • (Symbol)

    :special



138
139
140
# File 'lib/sashite/pan/action/special.rb', line 138

def type
  TYPE
end