Code Monkey home page Code Monkey logo

weiqi.js's Introduction

weiqi.js Build Status

weiqi.js is an implementation of the board game Go. It provides mechanisms for representing a board with black and white stones, as well as the logic to actually play a game. The objects used in weiqi.js are persistent--methods often return entirely new instances instead of mutating internal state.

Persistence and structural sharing offer a memory-efficient way to represent multiple states of the board simutaneously, a desirable trait for applications where you'd like to explore the history of the game (such as for review) or to explore possible future responses (such as for AI decision-making).

The library is available as an npm package, and can be used in the browser with tools like browserify. If you don't want to use browserify, there is a pre-built file in the dist-browser directory that you can include in your browser-targeted projects right away. Access Weiqi with the global Weiqi variable in this case.

Usage

Creating a game

var Weiqi = require('weiqi');
var game = Weiqi.createGame(9);  // creates a game on a 9 x 9 board
game = Weiqi.createGame(13);  // creates a game on a 13 x 13 board
game = Weiqi.createGame(19);  // creates a game on a 19 x 19 board

Playing a game

var Weiqi = require('weiqi');
var game = Weiqi.createGame(9);
game = Weiqi.play(game, 'black', [2,2]);
game = Weiqi.play(game, 'white', [6,7]);
game = Weiqi.pass(game, 'black'); // black passes
game = Weiqi.pass(game, 'white'); // white passes

Weiqi.play and Weiqi.pass each take a player identifier ('black' or 'white'). Weiqi.play takes an additional zero-indexed array of size two that indicates the position to place a stone. Weiqi.play will raise exceptions for the following situations:

  • the game is already over (there have already been two consecutive passes)
  • it is not currently the turn of the player who is attempting to play
  • the move violates positional superko (at the end of any turn, the board cannot be in a state in which it has been previously).

Querying the game

get('currentPlayer') returns either 'black' or 'white'.

> var Weiqi = require('weiqi');
> var game = Weiqi.play(Weiqi.createGame(9), 'black', [2,2]);
> game.get('currentPlayer');
"o"

isOver returns true iff there have been two consecutive passes.

> var Weiqi = require('weiqi');
> var game = Weiqi.play(Weiqi.createGame(9), 'black', [2,2]);
> Weiqi.isOver(game);
false
> game = Weiqi.pass(game, 'white');
> Weiqi.isOver(game);
false
> game = Weiqi.pass(game, 'black');
> Weiqi.isOver(game);
true

To compute the score of the game (using area score) pass a value of komi to areaScore(). All stones are considered alive.

Weiqi.areaScore(game, 7.5);  // compute area score given 7.5 komi

weiqi.js's People

Contributors

cjlarose avatar

Watchers

James Cloos avatar  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.