Class: Sashite::Pan::Action::Drop

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

Overview

Drop action class

Handles drop actions to empty squares, with optional piece identifier and transformation.

Format: [<piece>]*<destination> Examples: “P*e5”, “*d4”, “S*c3=+S”

Constant Summary collapse

TYPE =

Action type

:drop
OPERATOR =

Operator constant

"*"
TRANSFORMATION_SEPARATOR =

Transformation separator

"="
ERROR_INVALID_DROP =

Error messages

"Invalid drop notation: %s"
ERROR_INVALID_DESTINATION =
"Invalid destination coordinate: %s"
ERROR_INVALID_PIECE =
"Invalid piece identifier: %s"
ERROR_INVALID_TRANSFORMATION =
"Invalid transformation piece: %s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination, piece: nil, transformation: nil) ⇒ Drop

Create a new drop action instance

Examples:

Drop.new("e5", piece: "P")                           # => #<Drop ...>
Drop.new("d4")                                       # => #<Drop ...>
Drop.new("c3", piece: "S", transformation: "+S")     # => #<Drop ...>

Raises:

  • (ArgumentError)

    if coordinates, piece, or transformation are invalid



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sashite/pan/action/drop.rb', line 120

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

  raise ::ArgumentError, format(ERROR_INVALID_PIECE, piece) if piece && !::Sashite::Epin.valid?(piece)

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

  @destination = destination
  @piece = piece
  @transformation = transformation

  freeze
end

Instance Attribute Details

#destinationString (readonly)



33
34
35
# File 'lib/sashite/pan/action/drop.rb', line 33

def destination
  @destination
end

#pieceString? (readonly)



36
37
38
# File 'lib/sashite/pan/action/drop.rb', line 36

def piece
  @piece
end

#transformationString? (readonly)



39
40
41
# File 'lib/sashite/pan/action/drop.rb', line 39

def transformation
  @transformation
end

Class Method Details

.parse(pan_string) ⇒ Drop

Parse a drop notation string into a Drop instance

Examples:

Drop.parse("P*e5")      # => #<Drop piece="P" destination="e5">
Drop.parse("*d4")       # => #<Drop destination="d4">
Drop.parse("S*c3=+S")   # => #<Drop piece="S" destination="c3" transformation="+S">

Raises:

  • (ArgumentError)

    if the string is not valid drop notation



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sashite/pan/action/drop.rb', line 90

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

  parts = pan_string.split(OPERATOR, 2)
  piece = parts[0].empty? ? nil : 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(destination, piece: piece, transformation: transformation)
end

.valid?(pan_string) ⇒ Boolean

Check if a string represents a valid drop action

Examples:

Drop.valid?("P*e5")       # => true
Drop.valid?("*d4")        # => true
Drop.valid?("S*c3=+S")    # => true


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
77
78
# File 'lib/sashite/pan/action/drop.rb', line 50

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

  piece_part = parts[0]
  dest_and_transform = parts[1]

  # Piece part is optional, but if present must be valid EPIN
  return false if !piece_part.empty? && !::Sashite::Epin.valid?(piece_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



241
242
243
244
245
246
247
# File 'lib/sashite/pan/action/drop.rb', line 241

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

  destination == other.destination &&
    piece == other.piece &&
    transformation == other.transformation
end

#capture?Boolean

Check if this is a capture action



184
185
186
# File 'lib/sashite/pan/action/drop.rb', line 184

def capture?
  false
end

#drop?Boolean

Check if this is a drop action



205
206
207
# File 'lib/sashite/pan/action/drop.rb', line 205

def drop?
  true
end

#drop_action?Boolean

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



233
234
235
# File 'lib/sashite/pan/action/drop.rb', line 233

def drop_action?
  true
end

#drop_capture?Boolean

Check if this is a drop capture action



212
213
214
# File 'lib/sashite/pan/action/drop.rb', line 212

def drop_capture?
  false
end

#hashInteger

Custom hash implementation for use in collections



255
256
257
# File 'lib/sashite/pan/action/drop.rb', line 255

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

#modify?Boolean

Check if this is a modify action



219
220
221
# File 'lib/sashite/pan/action/drop.rb', line 219

def modify?
  false
end

#move?Boolean

Check if this is a move action



177
178
179
# File 'lib/sashite/pan/action/drop.rb', line 177

def move?
  false
end

#movement?Boolean

Check if this is a movement action



226
227
228
# File 'lib/sashite/pan/action/drop.rb', line 226

def movement?
  false
end

#pass?Boolean

Check if this is a pass action



170
171
172
# File 'lib/sashite/pan/action/drop.rb', line 170

def pass?
  false
end

#sourcenil

Get the source coordinate



148
149
150
# File 'lib/sashite/pan/action/drop.rb', line 148

def source
  nil
end

#special?Boolean

Check if this is a special action



191
192
193
# File 'lib/sashite/pan/action/drop.rb', line 191

def special?
  false
end

#static_capture?Boolean

Check if this is a static capture action



198
199
200
# File 'lib/sashite/pan/action/drop.rb', line 198

def static_capture?
  false
end

#to_sString

Convert the action to its PAN string representation

Examples:

action.to_s  # => "P*e5" or "*d4" or "S*c3=+S"


158
159
160
161
162
163
164
165
# File 'lib/sashite/pan/action/drop.rb', line 158

def to_s
  result = +""
  result << piece if piece
  result << OPERATOR
  result << destination
  result << TRANSFORMATION_SEPARATOR << transformation if transformation
  result
end

#typeSymbol

Get the action type



141
142
143
# File 'lib/sashite/pan/action/drop.rb', line 141

def type
  TYPE
end