Skip to content

sashite/pmn.rb

Repository files navigation

Pmn.rb

Gem Version GitHub Version Build Codecov YARD Docs License

Parse, validate and emit PMN v1.0.0 — the rule‑agnostic Portable Move Notation — in pure Ruby.


Why PMN?

PMN expresses state‑changing actions without embedding game rules. Whether you are writing a Chess engine, a Shogi server, or a hybrid variant, PMN gives you a deterministic, game‑neutral core that travels well across languages and databases.


Installation

Add to your Gemfile:

# Gemfile
gem "portable_move_notation"

then:

bundle install

Or grab it directly:

gem install portable_move_notation

Require it in your code:

require "portable_move_notation" # provides the PortableMoveNotation namespace

Quick Start

Dump a single action (dropping a Shogi pawn on square "27"):

require "portable_move_notation"

move = PortableMoveNotation::Move.new(
  PortableMoveNotation::Action.new(
    src_square: nil,
    dst_square: "27",
    piece_name: "p",
    piece_hand: nil
  )
)

puts move.to_json
# Output: A JSON array with the move data

Parse it back:

restored = PortableMoveNotation::Move.from_json(move.to_json)
puts restored.actions.first.dst_square # => "27"

Advanced Examples

Chess · Kingside Castling

require "portable_move_notation"

king = PortableMoveNotation::Action.new(
  src_square: "e1", dst_square: "g1", piece_name: "K", piece_hand: nil
)
rook = PortableMoveNotation::Action.new(
  src_square: "h1", dst_square: "f1", piece_name: "R", piece_hand: nil
)

puts PortableMoveNotation::Move.new(king, rook).to_json
# Output: A JSON array containing both king and rook move data

Validation

PortableMoveNotation::Move.valid?(data) checks shape compliance against the spec — not game legality:

require "portable_move_notation"
require "json"

# Parse a simple JSON move with a pawn at square "27"
data = JSON.parse('[{ "dst_square" => "27", "piece_name" => "p" }]')
puts PortableMoveNotation::Move.valid?(data) # => true

You can also validate single actions:

# Check if an individual action is valid
action_data = { "dst_square" => "e4", "piece_name" => "P" }
puts PortableMoveNotation::Action.valid?(action_data) # => true

License

The gem is released under the MIT License.


About Sashité

Celebrating the beauty of Chinese, Japanese, and Western chess cultures. Find more projects & research at sashite.com.

About

A Ruby interface for data serialization in PMN format.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors 2

  •  
  •