Module: Sashite::Pcn

Defined in:
lib/sashite/pcn.rb,
lib/sashite/pcn/game.rb,
lib/sashite/pcn/game/meta.rb,
lib/sashite/pcn/game/sides.rb,
lib/sashite/pcn/game/sides/player.rb

Overview

PCN (Portable Chess Notation) implementation for Ruby

Provides functionality for representing complete chess game records across variants using a comprehensive JSON-based format.

This implementation is strictly compliant with PCN Specification v1.0.0

Defined Under Namespace

Classes: Game

Class Method Summary collapse

Class Method Details

.parse(hash) ⇒ Game

Parse a PCN document from a hash structure

Examples:

Parse minimal PCN

game = Sashite::Pcn.parse({
  "setup" => "+rnbq+kbn+r/+p+p+p+p+p+p+p+p/8/8/8/8/+P+P+P+P+P+P+P+P/+RNBQ+KBN+R / C/c"
})

Parse complete game

game = Sashite::Pcn.parse({
  "meta" => { "event" => "World Championship" },
  "sides" => {
    "first" => { "name" => "Carlsen", "elo" => 2830, "style" => "CHESS" },
    "second" => { "name" => "Nakamura", "elo" => 2794, "style" => "chess" }
  },
  "setup" => "+rnbq+kbn+r/+p+p+p+p+p+p+p+p/8/8/8/8/+P+P+P+P+P+P+P+P/+RNBQ+KBN+R / C/c",
  "moves" => [["e2", "e4"], ["e7", "e5"]],
  "status" => "in_progress"
})

Parameters:

  • hash (Hash)

    the PCN document data

Returns:

  • (Game)

    new game instance

Raises:

  • (ArgumentError)

    if the document is invalid



36
37
38
# File 'lib/sashite/pcn.rb', line 36

def self.parse(hash)
  Game.new(**hash.transform_keys(&:to_sym))
end

.valid?(hash) ⇒ Boolean

Validate a PCN document structure

Examples:

Sashite::Pcn.valid?({ "setup" => "8/8/8/8/8/8/8/8 / C/c" })  # => true
Sashite::Pcn.valid?({ "moves" => [] })                        # => false

Parameters:

  • hash (Hash)

    the PCN document data

Returns:

  • (Boolean)

    true if the document is structurally valid



48
49
50
51
52
53
54
55
56
# File 'lib/sashite/pcn.rb', line 48

def self.valid?(hash)
  return false unless hash.is_a?(::Hash)
  return false unless hash.key?("setup") || hash.key?(:setup)

  parse(hash)
  true
rescue ::ArgumentError, ::TypeError
  false
end