Module: Sashite::Pmn
- Defined in:
- lib/sashite/pmn.rb,
lib/sashite/pmn/move.rb,
lib/sashite/pmn/error.rb,
lib/sashite/pmn/action.rb
Overview
PMN (Portable Move Notation) implementation for Ruby
PMN is an array-based, rule-agnostic format that decomposes a move into a sequence of atomic actions. Each action is 2 or 3 elements:
[source, destination] # inferred piece
[source, destination, piece] # explicit piece (QPI)
Valid PMN arrays have length:
- multiple of 3, or
- multiple of 3 + 2
with a minimum of 2.
See specs: sashite.dev/specs/pmn/1.0.0/
Defined Under Namespace
Classes: Action, Error, InvalidActionError, InvalidLocationError, InvalidMoveError, InvalidPieceError, Move
Class Method Summary collapse
-
.from_actions(actions) ⇒ Sashite::Pmn::Move
Create a Move from Action objects.
-
.parse(pmn_array) ⇒ Sashite::Pmn::Move
Parse a PMN array into a Move object.
-
.valid?(pmn_array) ⇒ Boolean
Check if an array is valid PMN notation (non-raising).
-
.valid_location?(location) ⇒ Boolean
Validate a location string (CELL or HAND “*”).
-
.valid_piece?(piece) ⇒ Boolean
Validate a QPI piece string.
Class Method Details
.from_actions(actions) ⇒ Sashite::Pmn::Move
Create a Move from Action objects.
68 69 70 71 72 73 |
# File 'lib/sashite/pmn.rb', line 68 def self.from_actions(actions) raise ArgumentError, "Actions must be an array" unless actions.is_a?(Array) pmn_array = actions.flat_map(&:to_a) Move.new(*pmn_array) end |
.parse(pmn_array) ⇒ Sashite::Pmn::Move
Parse a PMN array into a Move object.
34 35 36 37 38 |
# File 'lib/sashite/pmn.rb', line 34 def self.parse(pmn_array) raise InvalidMoveError, "PMN must be an array, got #{pmn_array.class}" unless pmn_array.is_a?(Array) Move.new(*pmn_array) end |
.valid?(pmn_array) ⇒ Boolean
Check if an array is valid PMN notation (non-raising).
48 49 50 51 52 53 54 55 56 |
# File 'lib/sashite/pmn.rb', line 48 def self.valid?(pmn_array) return false unless pmn_array.is_a?(Array) return false if pmn_array.empty? move = Move.new(*pmn_array) move.valid? rescue Error false end |
.valid_location?(location) ⇒ Boolean
Validate a location string (CELL or HAND “*”).
81 82 83 84 85 |
# File 'lib/sashite/pmn.rb', line 81 def self.valid_location?(location) return false unless location.is_a?(String) Cell.valid?(location) || Hand.reserve?(location) end |
.valid_piece?(piece) ⇒ Boolean
Validate a QPI piece string.
93 94 95 96 97 |
# File 'lib/sashite/pmn.rb', line 93 def self.valid_piece?(piece) return false unless piece.is_a?(String) Qpi.valid?(piece) end |