Class: Sashite::Pan::Action::Modify

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

Overview

Modify action class

Handles in-place transformation actions where a piece changes its attributes without moving.

Format: <square>=<piece> Examples: “e4=+P”, “c3=k’”

Constant Summary collapse

TYPE =

Action type

:modify
OPERATOR =

Operator constant

"="
ERROR_INVALID_MODIFY =

Error messages

"Invalid modify notation: %s"
ERROR_INVALID_SQUARE =
"Invalid square coordinate: %s"
ERROR_INVALID_PIECE =
"Invalid piece identifier: %s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(square, piece) ⇒ Modify

Create a new modify action instance

Examples:

Modify.new("e4", "+P")   # => #<Modify ...>
Modify.new("c3", "k'")   # => #<Modify ...>

Parameters:

  • square (String)

    CELL coordinate

  • piece (String)

    EPIN piece identifier (final state)

Raises:

  • (ArgumentError)

    if coordinate or piece is invalid



87
88
89
90
91
92
93
94
95
# File 'lib/sashite/pan/action/modify.rb', line 87

def initialize(square, piece)
  raise ::ArgumentError, format(ERROR_INVALID_SQUARE, square) unless ::Sashite::Cell.valid?(square)
  raise ::ArgumentError, format(ERROR_INVALID_PIECE, piece) unless ::Sashite::Epin.valid?(piece)

  @destination = square
  @piece = piece

  freeze
end

Instance Attribute Details

#destinationString (readonly)

Returns destination CELL coordinate (square being modified).

Returns:

  • (String)

    destination CELL coordinate (square being modified)



29
30
31
# File 'lib/sashite/pan/action/modify.rb', line 29

def destination
  @destination
end

#pieceString (readonly)

Returns EPIN piece identifier (final state).

Returns:

  • (String)

    EPIN piece identifier (final state)



32
33
34
# File 'lib/sashite/pan/action/modify.rb', line 32

def piece
  @piece
end

Class Method Details

.parse(pan_string) ⇒ Modify

Parse a modify notation string into a Modify instance

Examples:

Modify.parse("e4=+P")      # => #<Modify destination="e4" piece="+P">
Modify.parse("c3=k'")      # => #<Modify destination="c3" piece="k'">

Parameters:

  • pan_string (String)

    modify notation string

Returns:

  • (Modify)

    modify action instance

Raises:

  • (ArgumentError)

    if the string is not valid modify notation



68
69
70
71
72
73
74
75
76
# File 'lib/sashite/pan/action/modify.rb', line 68

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

  parts = pan_string.split(OPERATOR, 2)
  square = parts[0]
  piece = parts[1]

  new(square, piece)
end

.valid?(pan_string) ⇒ Boolean

Check if a string represents a valid modify action

Examples:

Modify.valid?("e4=+P")       # => true
Modify.valid?("c3=k'")       # => true
Modify.valid?("e4")          # => false

Parameters:

  • pan_string (String)

    the string to validate

Returns:

  • (Boolean)

    true if valid modify notation



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sashite/pan/action/modify.rb', line 43

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

  square_part = parts[0]
  piece_part = parts[1]

  return false unless ::Sashite::Cell.valid?(square_part)
  return false unless ::Sashite::Epin.valid?(piece_part)

  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



202
203
204
205
206
207
# File 'lib/sashite/pan/action/modify.rb', line 202

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

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

#capture?Boolean

Check if this is a capture action

Returns:

  • (Boolean)

    false



145
146
147
# File 'lib/sashite/pan/action/modify.rb', line 145

def capture?
  false
end

#drop?Boolean

Check if this is a drop action

Returns:

  • (Boolean)

    false



166
167
168
# File 'lib/sashite/pan/action/modify.rb', line 166

def drop?
  false
end

#drop_action?Boolean

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

Returns:

  • (Boolean)

    false



194
195
196
# File 'lib/sashite/pan/action/modify.rb', line 194

def drop_action?
  false
end

#drop_capture?Boolean

Check if this is a drop capture action

Returns:

  • (Boolean)

    false



173
174
175
# File 'lib/sashite/pan/action/modify.rb', line 173

def drop_capture?
  false
end

#hashInteger

Custom hash implementation for use in collections

Returns:

  • (Integer)

    hash value



215
216
217
# File 'lib/sashite/pan/action/modify.rb', line 215

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

#modify?Boolean

Check if this is a modify action

Returns:

  • (Boolean)

    true



180
181
182
# File 'lib/sashite/pan/action/modify.rb', line 180

def modify?
  true
end

#move?Boolean

Check if this is a move action

Returns:

  • (Boolean)

    false



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

def move?
  false
end

#movement?Boolean

Check if this is a movement action

Returns:

  • (Boolean)

    false



187
188
189
# File 'lib/sashite/pan/action/modify.rb', line 187

def movement?
  false
end

#pass?Boolean

Check if this is a pass action

Returns:

  • (Boolean)

    false



131
132
133
# File 'lib/sashite/pan/action/modify.rb', line 131

def pass?
  false
end

#sourcenil

Get the source coordinate

Returns:

  • (nil)

    modify actions have no source



107
108
109
# File 'lib/sashite/pan/action/modify.rb', line 107

def source
  nil
end

#special?Boolean

Check if this is a special action

Returns:

  • (Boolean)

    false



152
153
154
# File 'lib/sashite/pan/action/modify.rb', line 152

def special?
  false
end

#static_capture?Boolean

Check if this is a static capture action

Returns:

  • (Boolean)

    false



159
160
161
# File 'lib/sashite/pan/action/modify.rb', line 159

def static_capture?
  false
end

#to_sString

Convert the action to its PAN string representation

Examples:

action.to_s  # => "e4=+P" or "c3=k'"

Returns:

  • (String)

    modify notation



124
125
126
# File 'lib/sashite/pan/action/modify.rb', line 124

def to_s
  "#{destination}#{OPERATOR}#{piece}"
end

#transformationnil

Get the transformation piece

Returns:

  • (nil)

    modify actions have no separate transformation (piece represents final state)



114
115
116
# File 'lib/sashite/pan/action/modify.rb', line 114

def transformation
  nil
end

#typeSymbol

Get the action type

Returns:

  • (Symbol)

    :modify



100
101
102
# File 'lib/sashite/pan/action/modify.rb', line 100

def type
  TYPE
end