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

Class Method Details

.from_actions(actions) ⇒ Sashite::Pmn::Move

Create a Move from Action objects.

Examples:

a1 = Sashite::Pmn::Action.new("e2","e4","C:P")
a2 = Sashite::Pmn::Action.new("d7","d5","c:p")
move = Sashite::Pmn.from_actions([a1,a2])

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if actions is not an Array



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.

Examples:

Sashite::Pmn.parse(["e2","e4","C:P"]).actions.size # => 1

Parameters:

  • pmn_array (Array<String>)

    flat array of PMN elements

Returns:

Raises:



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).

Examples:

Sashite::Pmn.valid?(["e2","e4","C:P"]) # => true
Sashite::Pmn.valid?(["e2"])            # => false

Parameters:

  • pmn_array (Array)

Returns:

  • (Boolean)

    true if valid, false otherwise



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 “*”).

Parameters:

  • location (String)

Returns:

  • (Boolean)


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.

Parameters:

  • piece (String)

Returns:

  • (Boolean)


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