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.
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:
| Field | What it is |
|---|---|
size, rules, komi, mode | How the game was set up |
stones | Every stone on the board at the move being viewed, as { x, y, color } |
toPlay | 'black' or 'white' |
moveNumber | Moves and passes from the start to the move being viewed |
lastMove | The move that led here, or null at the start |
prisoners | Stones each side has captured |
phase | 'playing', 'scoring' after two passes in a row, or 'over' |
score | The count while scoring and once the game is over; null while playing |
current | The move being viewed: its ID, its parent and children, and its marks and comment |
moves | Every 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:
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.
| Code | When |
|---|---|
occupied | The point already has a stone. |
suicide | The stone would have no liberties and capture nothing. |
ko | The move would retake a ko straight away. |
off-board | The point isn't on the board. |
game-over | The game has ended. |
scoring | Moves wait while the count is being agreed. |
not-scoring | A scoring function was called while the game was still being played. |
not-at-latest-move | In 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-group | markGroup was given an empty point. |
not-in-review | Marks, labels, and comments need review mode. |
unknown-move | goToMove was given an ID that isn't in this game. |
invalid-sgf | The text isn't an SGF record. at is where it stopped making sense. |
unplayable-sgf | The file holds a move the rules don't allow. move says which. |
invalid-action | apply 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
| To | Use |
|---|---|
| Start a game | newGame |
| Play | play, pass, resign |
| Move through the record | goToMove, nextMove, previousMove |
| Annotate (review mode) | markPoint, labelPoint, clearMark, setComment |
| Score | markGroup, acceptScore, resumePlay |
| Switch mode | setMode |
| Read and write files | openSgf, saveSgf |
| Call any of them from data | apply |
Actions
Every function that takes a game also exists as data: an Action. apply calls the function an action names.
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.