Class: Sashite::Pmn::Action
- Inherits:
-
Object
- Object
- Sashite::Pmn::Action
- Defined in:
- lib/sashite/pmn/action.rb
Overview
Represents an atomic action within a PMN move.
Each action encodes a single transformation:
[source, destination] # inferred piece
[source, destination, piece] # explicit QPI piece
Locations are either CELL coordinates or HAND (“*”). Actions where source == destination are allowed (pass / in-place).
Instance Attribute Summary collapse
-
#destination ⇒ String
readonly
The destination location (CELL or “*”).
-
#piece ⇒ String?
readonly
The piece in QPI format, or nil if inferred.
-
#source ⇒ String
readonly
The source location (CELL or “*”).
Class Method Summary collapse
-
.from_hash(hash) ⇒ Action
Build from a hash with keys :source, :destination, optional :piece.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Equality by source, destination, piece.
-
#board_move? ⇒ Boolean
True when neither drop nor capture.
-
#board_to_board? ⇒ Boolean
True if both endpoints are board locations.
-
#capture? ⇒ Boolean
True if the action takes from board to reserve.
-
#drop? ⇒ Boolean
True if the action places from reserve to board.
-
#from_reserve? ⇒ Boolean
True if source is HAND (“*”).
- #hash ⇒ Integer
-
#inferred? ⇒ Boolean
True if piece is not explicitly specified.
-
#initialize(source, destination, piece = nil) ⇒ Action
constructor
Create an immutable action.
- #inspect ⇒ String
-
#piece_specified? ⇒ Boolean
True if a piece string is explicitly specified.
-
#piece_valid? ⇒ Boolean
True if a specified piece is valid QPI (false if none).
-
#to_a ⇒ Array<String>
2 elems if inferred, 3 if explicit.
-
#to_h ⇒ Hash
{ source:, destination:, piece: } (piece omitted if nil).
-
#to_reserve? ⇒ Boolean
True if destination is HAND (“*”).
-
#valid? ⇒ Boolean
True if all components are valid.
Constructor Details
#initialize(source, destination, piece = nil) ⇒ Action
Create an immutable action.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sashite/pmn/action.rb', line 36 def initialize(source, destination, piece = nil) validate_source!(source) validate_destination!(destination) validate_piece!(piece) if piece @source = source.freeze @destination = destination.freeze @piece = piece&.freeze freeze end |
Instance Attribute Details
#destination ⇒ String (readonly)
Returns the destination location (CELL or “*”).
24 25 26 |
# File 'lib/sashite/pmn/action.rb', line 24 def destination @destination end |
#piece ⇒ String? (readonly)
Returns the piece in QPI format, or nil if inferred.
27 28 29 |
# File 'lib/sashite/pmn/action.rb', line 27 def piece @piece end |
#source ⇒ String (readonly)
Returns the source location (CELL or “*”).
21 22 23 |
# File 'lib/sashite/pmn/action.rb', line 21 def source @source end |
Class Method Details
.from_hash(hash) ⇒ Action
Build from a hash with keys :source, :destination, optional :piece.
148 149 150 151 152 153 |
# File 'lib/sashite/pmn/action.rb', line 148 def self.from_hash(hash) raise ::ArgumentError, "Hash must include :source" unless hash.key?(:source) raise ::ArgumentError, "Hash must include :destination" unless hash.key?(:destination) new(hash[:source], hash[:destination], hash[:piece]) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns equality by source, destination, piece.
120 121 122 123 124 125 126 |
# File 'lib/sashite/pmn/action.rb', line 120 def ==(other) return false unless other.is_a?(Action) source == other.source && destination == other.destination && piece == other.piece end |
#board_move? ⇒ Boolean
Returns true when neither drop nor capture.
93 94 95 |
# File 'lib/sashite/pmn/action.rb', line 93 def board_move? !drop? && !capture? end |
#board_to_board? ⇒ Boolean
Returns true if both endpoints are board locations.
78 79 80 |
# File 'lib/sashite/pmn/action.rb', line 78 def board_to_board? ::Sashite::Cell.valid?(source) && ::Sashite::Cell.valid?(destination) end |
#capture? ⇒ Boolean
Returns true if the action takes from board to reserve.
88 89 90 |
# File 'lib/sashite/pmn/action.rb', line 88 def capture? ::Sashite::Cell.valid?(source) && to_reserve? end |
#drop? ⇒ Boolean
Returns true if the action places from reserve to board.
83 84 85 |
# File 'lib/sashite/pmn/action.rb', line 83 def drop? from_reserve? && ::Sashite::Cell.valid?(destination) end |
#from_reserve? ⇒ Boolean
Returns true if source is HAND (“*”).
68 69 70 |
# File 'lib/sashite/pmn/action.rb', line 68 def from_reserve? ::Sashite::Hand.reserve?(source) end |
#hash ⇒ Integer
130 131 132 |
# File 'lib/sashite/pmn/action.rb', line 130 def hash [source, destination, piece].hash end |
#inferred? ⇒ Boolean
Returns true if piece is not explicitly specified.
51 52 53 |
# File 'lib/sashite/pmn/action.rb', line 51 def inferred? piece.nil? end |
#inspect ⇒ String
135 136 137 138 139 |
# File 'lib/sashite/pmn/action.rb', line 135 def inspect attrs = ["source=#{source.inspect}", "destination=#{destination.inspect}"] attrs << "piece=#{piece.inspect}" if piece "#<#{self.class.name} #{attrs.join(' ')}>" end |
#piece_specified? ⇒ Boolean
Returns true if a piece string is explicitly specified.
56 57 58 |
# File 'lib/sashite/pmn/action.rb', line 56 def piece_specified? !piece.nil? end |
#piece_valid? ⇒ Boolean
Returns true if a specified piece is valid QPI (false if none).
61 62 63 64 65 |
# File 'lib/sashite/pmn/action.rb', line 61 def piece_valid? return false if piece.nil? ::Sashite::Qpi.valid?(piece) end |
#to_a ⇒ Array<String>
Returns 2 elems if inferred, 3 if explicit.
100 101 102 |
# File 'lib/sashite/pmn/action.rb', line 100 def to_a inferred? ? [source, destination] : [source, destination, piece] end |
#to_h ⇒ Hash
Returns { source:, destination:, piece: } (piece omitted if nil).
105 106 107 |
# File 'lib/sashite/pmn/action.rb', line 105 def to_h { source: source, destination: destination, piece: piece }.compact end |
#to_reserve? ⇒ Boolean
Returns true if destination is HAND (“*”).
73 74 75 |
# File 'lib/sashite/pmn/action.rb', line 73 def to_reserve? ::Sashite::Hand.reserve?(destination) end |
#valid? ⇒ Boolean
Returns true if all components are valid.
112 113 114 115 116 |
# File 'lib/sashite/pmn/action.rb', line 112 def valid? valid_location?(source) && valid_location?(destination) && (piece.nil? || ::Sashite::Qpi.valid?(piece)) end |