Class: Sashite::Pan::Action::StaticCapture

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

Overview

Static capture action class

Handles capture actions without movement - removing a piece from the board without the capturing piece moving.

Format: <square> Examples: “d4”, “+e5”

Constant Summary collapse

TYPE =

Action type

:static_capture
OPERATOR =

Operator constant

"+"
ERROR_INVALID_STATIC_CAPTURE =

Error messages

"Invalid static capture notation: %s"
ERROR_INVALID_SQUARE =
"Invalid square coordinate: %s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(square) ⇒ StaticCapture

Create a new static capture action instance

Examples:

StaticCapture.new("d4")  # => #<StaticCapture ...>

Parameters:

  • square (String)

    CELL coordinate of piece to capture

Raises:

  • (ArgumentError)

    if coordinate is invalid



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

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

  @destination = square

  freeze
end

Instance Attribute Details

#destinationString (readonly)

Returns destination CELL coordinate (square where piece is captured).

Returns:

  • (String)

    destination CELL coordinate (square where piece is captured)



27
28
29
# File 'lib/sashite/pan/action/static_capture.rb', line 27

def destination
  @destination
end

Class Method Details

.parse(pan_string) ⇒ StaticCapture

Parse a static capture notation string into a StaticCapture instance

Examples:

StaticCapture.parse("+d4")      # => #<StaticCapture destination="d4">

Parameters:

  • pan_string (String)

    static capture notation string

Returns:

Raises:

  • (ArgumentError)

    if the string is not valid static capture notation



55
56
57
58
59
60
# File 'lib/sashite/pan/action/static_capture.rb', line 55

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

  square = pan_string[1..]
  new(square)
end

.valid?(pan_string) ⇒ Boolean

Check if a string represents a valid static capture action

Examples:

StaticCapture.valid?("+d4")       # => true
StaticCapture.valid?("+e5")       # => true
StaticCapture.valid?("d4")        # => false

Parameters:

  • pan_string (String)

    the string to validate

Returns:

  • (Boolean)

    true if valid static capture notation



38
39
40
41
42
43
44
45
# File 'lib/sashite/pan/action/static_capture.rb', line 38

def self.valid?(pan_string)
  return false unless pan_string.is_a?(::String)
  return false unless pan_string.start_with?(OPERATOR)
  return false if pan_string.length < 2

  square = pan_string[1..]
  ::Sashite::Cell.valid?(square)
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



189
190
191
192
193
# File 'lib/sashite/pan/action/static_capture.rb', line 189

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

  destination == other.destination
end

#capture?Boolean

Check if this is a capture action

Returns:

  • (Boolean)

    false



132
133
134
# File 'lib/sashite/pan/action/static_capture.rb', line 132

def capture?
  false
end

#drop?Boolean

Check if this is a drop action

Returns:

  • (Boolean)

    false



153
154
155
# File 'lib/sashite/pan/action/static_capture.rb', line 153

def drop?
  false
end

#drop_action?Boolean

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

Returns:

  • (Boolean)

    false



181
182
183
# File 'lib/sashite/pan/action/static_capture.rb', line 181

def drop_action?
  false
end

#drop_capture?Boolean

Check if this is a drop capture action

Returns:

  • (Boolean)

    false



160
161
162
# File 'lib/sashite/pan/action/static_capture.rb', line 160

def drop_capture?
  false
end

#hashInteger

Custom hash implementation for use in collections

Returns:

  • (Integer)

    hash value



201
202
203
# File 'lib/sashite/pan/action/static_capture.rb', line 201

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

#modify?Boolean

Check if this is a modify action

Returns:

  • (Boolean)

    false



167
168
169
# File 'lib/sashite/pan/action/static_capture.rb', line 167

def modify?
  false
end

#move?Boolean

Check if this is a move action

Returns:

  • (Boolean)

    false



125
126
127
# File 'lib/sashite/pan/action/static_capture.rb', line 125

def move?
  false
end

#movement?Boolean

Check if this is a movement action

Returns:

  • (Boolean)

    false



174
175
176
# File 'lib/sashite/pan/action/static_capture.rb', line 174

def movement?
  false
end

#pass?Boolean

Check if this is a pass action

Returns:

  • (Boolean)

    false



118
119
120
# File 'lib/sashite/pan/action/static_capture.rb', line 118

def pass?
  false
end

#piecenil

Get the piece identifier

Returns:

  • (nil)

    static capture actions have no piece identifier



94
95
96
# File 'lib/sashite/pan/action/static_capture.rb', line 94

def piece
  nil
end

#sourcenil

Get the source coordinate

Returns:

  • (nil)

    static capture actions have no source



87
88
89
# File 'lib/sashite/pan/action/static_capture.rb', line 87

def source
  nil
end

#special?Boolean

Check if this is a special action

Returns:

  • (Boolean)

    false



139
140
141
# File 'lib/sashite/pan/action/static_capture.rb', line 139

def special?
  false
end

#static_capture?Boolean

Check if this is a static capture action

Returns:

  • (Boolean)

    true



146
147
148
# File 'lib/sashite/pan/action/static_capture.rb', line 146

def static_capture?
  true
end

#to_sString

Convert the action to its PAN string representation

Examples:

action.to_s  # => "+d4"

Returns:

  • (String)

    static capture notation



111
112
113
# File 'lib/sashite/pan/action/static_capture.rb', line 111

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

#transformationnil

Get the transformation piece

Returns:

  • (nil)

    static capture actions have no transformation



101
102
103
# File 'lib/sashite/pan/action/static_capture.rb', line 101

def transformation
  nil
end

#typeSymbol

Get the action type

Returns:

  • (Symbol)

    :static_capture



80
81
82
# File 'lib/sashite/pan/action/static_capture.rb', line 80

def type
  TYPE
end