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
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.
67 68 69 70 71 72 |
# File 'lib/sashite/pmn.rb', line 67 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 Error::Move, "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 |
# File 'lib/sashite/pmn.rb', line 48 def self.valid?(pmn_array) return false unless pmn_array.is_a?(::Array) move = Move.new(*pmn_array) move.valid? rescue Error false end |
.valid_location?(location) ⇒ Boolean
Validate a location string (CELL or HAND “*”).
80 81 82 83 84 |
# File 'lib/sashite/pmn.rb', line 80 def self.valid_location?(location) return false unless location.is_a?(::String) ::Sashite::Cell.valid?(location) || ::Sashite::Hand.reserve?(location) end |
.valid_piece?(piece) ⇒ Boolean
Validate a QPI piece string.
92 93 94 95 96 |
# File 'lib/sashite/pmn.rb', line 92 def self.valid_piece?(piece) return false unless piece.is_a?(::String) ::Sashite::Qpi.valid?(piece) end |