Git Product home page Git Product logo

smartgame's Introduction

smartgame Build Status

A node library for parsing SGF format game records into JavaScript and back again. Then use smartgamer to navigate and manipulate those game records.

Installation

For most projects, you'll just want to install smartgame locally and add it to your project's dependencies in package.json:

$ npm install --save smartgame

If you want (for whatever reason) to use smartgame anywhere, you can install it globally.

$ npm install -g smartgame

Usage

var sgf = require('smartgame');
var fs = require('fs');

// Grab an SGF file from somewhere
var example = fs.readFileSync('sgf/example.sgf', { encoding: 'utf8' });

var collection = sgf.parse(example);

// ... use the collection object however you want!

// ... when collection has been modified and you want to save it as an .sgf file
var collectionSGF = sgf.generate(collection);
fs.writeFileSync('new-example.sgf', collectionSGF, { encoding: 'utf8' });

Example JS Game Record

Let's take a very simple SGF file as an example:

(;GM[1]FF[4]CA[UTF-8]SZ[19];B[pd];W[dp];B[pp](;W[dd])(;W[dc];B[ce];W[ed](;B[ch];W[jc])(;B[ci])))

The parse function would turn this into a JS Object that looked like this:

{
	gameTrees: [
		{
			parent: <a reference to the parent object>,
			nodes: [
				{ GM: '1', FF: '4', CA: 'UTF-8', SZ: '19' },
				{ B: 'pd' },
				{ W: 'dp' },
				{ B: 'pp' }
			],
			sequences: [
				{
					parent: <a reference to the parent object>,
					nodes: [
						{ W: 'dd' }
					]
				},
				{
					parent: <a reference to the parent object>,
					nodes: [
						{ W: 'dc' },
						{ B: 'ce' },
						{ W: 'ed' }
					],
					sequences: [
						{
							parent: <a reference to the parent object>,
							nodes: [
								{ B: 'ch' },
								{ W: 'jc' }
							]
						},
						{
							parent: <a reference to the parent object>,
							nodes: [
								{ B: 'ci' }
							]
						}
					]
				}
			]
		}
	]
}

You'll still have to read up a little bit on the way SGFs work, but the structure is a simple and straightforward representation of the SGF in JS form.

Want an easy way to navigate and manipulate that game? Check out smartgamer.

License

MIT

History

This parser began life as part of another project, but I thought it was useful enough to become its own module. Breaking it and the other components of that project out into individual modules has helped me improve the separation of concerns and make the project more approachable. It's the first NPM package I've written that's not a plugin for something else (ie, a Grunt plugin or Yeoman generator) and any criticisms or suggestions are welcome.

smartgame's People

Contributors

neagle avatar gwylim avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.