Class: Sashite::Pan::Action::DropCapture

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

Overview

Drop capture action class

Handles drop actions with capture - placing a piece from reserve onto an occupied square, capturing the piece there.

Format: [<piece>].<destination> Examples: “L.b4”, “.c3”, “P.e5=+P”

Constant Summary collapse

TYPE =

Action type

:drop_capture
OPERATOR =

Operator constant

"."
TRANSFORMATION_SEPARATOR =

Transformation separator

"="
ERROR_INVALID_DROP_CAPTURE =

Error messages

"Invalid drop capture 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) ⇒ DropCapture

Create a new drop capture action instance

Examples:

DropCapture.new("b4", piece: "L")                    # => #<DropCapture ...>
DropCapture.new("c3")                                # => #<DropCapture ...>
DropCapture.new("e5", piece: "P", transformation: "+P") # => #<DropCapture ...>

Parameters:

  • destination (String)

    destination CELL coordinate

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

    optional EPIN piece identifier

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

    optional EPIN transformation

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_capture.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)

Returns destination CELL coordinate.

Returns:

  • (String)

    destination CELL coordinate



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

def destination
  @destination
end

#pieceString? (readonly)

Returns optional EPIN piece identifier.

Returns:

  • (String, nil)

    optional EPIN piece identifier



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

def piece
  @piece
end

#transformationString? (readonly)

Returns optional EPIN transformation.

Returns:

  • (String, nil)

    optional EPIN transformation



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

def transformation
  @transformation
end

Class Method Details

.parse(pan_string) ⇒ DropCapture

Parse a drop capture notation string into a DropCapture instance

Examples:

DropCapture.parse("L.b4")      # => #<DropCapture piece="L" destination="b4">
DropCapture.parse(".c3")       # => #<DropCapture destination="c3">
DropCapture.parse("P.e5=+P")   # => #<DropCapture piece="P" destination="e5" transformation="+P">

Parameters:

  • pan_string (String)

    drop capture notation string

Returns:

Raises:

  • (ArgumentError)

    if the string is not valid drop capture 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_capture.rb', line 90

def self.parse(pan_string)
  raise ::ArgumentError, format(ERROR_INVALID_DROP_CAPTURE, 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 capture action

Examples:

DropCapture.valid?("L.b4")       # => true
DropCapture.valid?(".c3")        # => true
DropCapture.valid?("P.e5=+P")    # => true

Parameters:

  • pan_string (String)

    the string to validate

Returns:

  • (Boolean)

    true if valid drop capture notation



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_capture.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

Parameters:

  • other (Object)

    object to compare with

Returns:

  • (Boolean)

    true if actions are equal



241
242
243
244
245
246
247
# File 'lib/sashite/pan/action/drop_capture.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

Returns:

  • (Boolean)

    false



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

def capture?
  false
end

#drop?Boolean

Check if this is a drop action

Returns:

  • (Boolean)

    false



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

def drop?
  false
end

#drop_action?Boolean

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

Returns:

  • (Boolean)

    true



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

def drop_action?
  true
end

#drop_capture?Boolean

Check if this is a drop capture action

Returns:

  • (Boolean)

    true



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

def drop_capture?
  true
end

#hashInteger

Custom hash implementation for use in collections

Returns:

  • (Integer)

    hash value



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

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

#modify?Boolean

Check if this is a modify action

Returns:

  • (Boolean)

    false



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

def modify?
  false
end

#move?Boolean

Check if this is a move action

Returns:

  • (Boolean)

    false



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

def move?
  false
end

#movement?Boolean

Check if this is a movement action

Returns:

  • (Boolean)

    false



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

def movement?
  false
end

#pass?Boolean

Check if this is a pass action

Returns:

  • (Boolean)

    false



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

def pass?
  false
end

#sourcenil

Get the source coordinate

Returns:

  • (nil)

    drop capture actions have no source



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

def source
  nil
end

#special?Boolean

Check if this is a special action

Returns:

  • (Boolean)

    false



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

def special?
  false
end

#static_capture?Boolean

Check if this is a static capture action

Returns:

  • (Boolean)

    false



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

def static_capture?
  false
end

#to_sString

Convert the action to its PAN string representation

Examples:

action.to_s  # => "L.b4" or ".c3" or "P.e5=+P"

Returns:

  • (String)

    drop capture notation



158
159
160
161
162
163
164
165
# File 'lib/sashite/pan/action/drop_capture.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

Returns:

  • (Symbol)

    :drop_capture



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

def type
  TYPE
end