Skip to content

Games as values ​

A game in Goban is a value: frozen, plain data. Nothing changes it. Every game function takes a game and returns a Result: either a new game, or a refusal that says why not.

js
import { newGame, play } from '@kiranpg/goban';

const game = newGame({ size: 9 });
const result = play(game, { x: 2, y: 2 });

if (result.ok) {
  console.log(result.game.moveNumber); // 1
  console.log(game.moveNumber);        // still 0: the old value is untouched
} else {
  console.log(result.error.code, result.error.message);
}

Because values never change, you can keep any of them: for undo, to compare positions, or to try a move without committing to it.

Points ​

Points are zero-based { x, y } from the top-left corner. On a 19×19 board, { x: 0, y: 0 } is the top-left corner and { x: 18, y: 18 } the bottom-right.

What a game holds ​

The most useful fields of a Game:

FieldWhat it is
size, rules, komi, modeHow the game was set up
stonesEvery stone on the board at the move being viewed, as { x, y, color }
toPlay'black' or 'white'
moveNumberMoves and passes from the start to the move being viewed
lastMoveThe move that led here, or null at the start
prisonersStones each side has captured
phase'playing', 'scoring' after two passes in a row, or 'over'
scoreThe count while scoring and once the game is over; null while playing
currentThe move being viewed: its ID, its parent and children, and its marks and comment
movesEvery move in the record, variations included

JSON.stringify(game) gives exactly these fields.

Only real games are accepted ​

Only values made by Goban's own functions count as games. A copy, or an object rebuilt from JSON, throws a TypeError when you pass it to a game function. To store a game or send it somewhere, save it as SGF and open it again:

js
import { openSgf, saveSgf } from '@kiranpg/goban';

const text = saveSgf(game);       // store or send this
const result = openSgf(text, { mode: 'play' });

Refusals ​

A refusal is something a correct caller can run into, such as playing on an occupied point. Every refusal has a code to branch on and a message you can show people. Some also say where: a point, a move, or a position in a file.

CodeWhen
occupiedThe point already has a stone.
suicideThe stone would have no liberties and capture nothing.
koThe move would retake a ko straight away.
off-boardThe point isn't on the board.
game-overThe game has ended.
scoringMoves wait while the count is being agreed.
not-scoringA scoring function was called while the game was still being played.
not-at-latest-moveIn play mode, moves are played only at the latest move of the main line. Ending a game works only there, in either mode.
not-a-groupmarkGroup was given an empty point.
not-in-reviewMarks, labels, and comments need review mode.
unknown-movegoToMove was given an ID that isn't in this game.
invalid-sgfThe text isn't an SGF record. at is where it stopped making sense.
unplayable-sgfThe file holds a move the rules don't allow. move says which.
invalid-actionapply was given an action it can't use.

A programmer error, such as a point that isn't made of integers, throws instead of returning a refusal.

The functions ​

ToUse
Start a gamenewGame
Playplay, pass, resign
Move through the recordgoToMove, nextMove, previousMove
Annotate (review mode)markPoint, labelPoint, clearMark, setComment
ScoremarkGroup, acceptScore, resumePlay
Switch modesetMode
Read and write filesopenSgf, saveSgf
Call any of them from dataapply

Actions ​

Every function that takes a game also exists as data: an Action. apply calls the function an action names.

js
import { apply } from '@kiranpg/goban';

apply(game, { type: 'play', point: { x: 3, y: 3 } });
apply(game, { type: 'pass' });
apply(game, { type: 'markPoint', point: { x: 3, y: 3 }, shape: 'triangle' });

Actions are what the board hands you when it's controlled, and they're a convenient format for agents, logs, and network messages.

Released under the MIT License.