Skip to content

Documentation

Board

Utilities surrounding the board centering around an immutable Board object.

Coordinate


new godash.Coordinate(x, y)

Class representing a single location on a Go board. Inherits from Immutable.Record.

Arguments

  • x (number): Location on one axis.
  • y (number): Location on the other axis.

Properties

  • x (number): Location on one axis.
  • y (number): Location on the other axis.

Example

var tengen = new Coordinate(9, 9);

tengen.toString();
// => Coordinate { "x": 9, "y": 9 }

tengen.x;
// => 9

Board


new godash.Board(dimensions = 19, ...moves)

Class representing a Go board. Inherits from Immutable.Record.

Arguments

  • dimensions (number): Size of the board, defaulted to 19x19.
  • ...moves (Coordinate, string): Moves to be placed on the board. They should be provided in pairs of arguments - Coordinate and color.

Properties

Example

var board = new Board();

board.toString();
// => Board { "dimensions": 19, "moves": Map {} }
var smallBoard = new Board(5, new Coordinate(2, 2), BLACK);

smallBoard.toString();
// => Board { "dimensions": 5, "moves": Map { {"x":2,"y":2}: "black" } }

addMove


godash.addMove(board, coordinate, color)

Function to add a move onto a board while respecting the rules. Since no sequence information is available, this function does not respect ko. Use followupKo() if you want to do ko-related things.

Arguments

  • board (Board): Board from which to add the move.
  • coordinate (Coordinate): Location to add the move.
  • color (string): Color of the move - BLACK or WHITE.

Returns

(Board): New board with the move added.

Example

var atari = new Board(3,
    new Coordinate(1, 0), BLACK,
    new Coordinate(0, 1), BLACK,
    new Coordinate(1, 2), BLACK,
    new Coordinate(1, 1), WHITE
);

toAsciiBoard(atari);
// => +O+
//    OXO
//    +++

var killed = addMove(
    atari,
    new Coordinate(2, 1),
    BLACK
);

toAsciiBoard(killed);
// => +O+
//    O+O
//    +O+

constructBoard


godash.constructBoard(coordinates, board = null, startColor = godash.BLACK)

Constructs a board for an array of coordinates. This function iteratively calls addMove while alternating colors.

Arguments

  • coordinates (Array): Members of this array should be of type Coordinate.
  • board (Board): Optional starting board. If omitted, a default Board is created - 19x19 and empty.
  • startColor (string): Optional starting color, defaulted to BLACK.

Returns

(Board): New board constructed from the coordinates.

Example

var tigersMouth = new Board(3,
    new Coordinate(1, 0), BLACK,
    new Coordinate(0, 1), BLACK,
    new Coordinate(1, 2), BLACK
);

toAsciiBoard(tigersMouth);
// => +O+
//    O+O
//    +++

var selfAtari = new Coordinate(1, 1);
var killingMove = new Coordinate(2, 1);

var ponnuki = constructBoard(
    [selfAtari, killingMove],
    tigersMouth,
    WHITE
);

toAsciiBoard(ponnuki);
// => +O+
//    O+O
//    +O+

difference


godash.difference(board1, board2)

Finds the moves on the first board that are not on the second board.

Arguments

  • board1 (Board): First board.
  • board2 (Board): Board with moves to subtract from first board.

Returns

(Immutable.Set): Set containing Immutable.List of Coordinate and color (string).

Example

var atari = new Board(3,
    new Coordinate(1, 0), BLACK,
    new Coordinate(0, 1), BLACK,
    new Coordinate(1, 2), BLACK,
    new Coordinate(1, 1), WHITE,
);

toAsciiBoard(atari);
// => +O+
//    OXO
//    +++

var captured = difference(atari, addMove(atari, new Coordinate(2, 1), BLACK));

captured.toString();
// 'Set { List [ Coordinate { "x": 1, "y": 1 }, "white" ] }'

followupKo


godash.followupKo(board, coordinate, color)

Determines move that would be illegal under the ko rule.

Arguments

Returns

(Coordinate): Position of illegal followup or null if none exists.

Example

const koPosition = new Board(4,
    new Coordinate(1, 0), BLACK,
    new Coordinate(0, 1), BLACK,
    new Coordinate(1, 2), BLACK,
    new Coordinate(1, 1), WHITE,
    new Coordinate(2, 0), WHITE,
    new Coordinate(2, 2), WHITE,
    new Coordinate(3, 1), WHITE,
);

toAsciiBoard(koPosition);
// => +O++
//    OXO+
//    X+X+
//    +X++

const koStart = new Coordinate(2, 1);

followupKo(koPosition, koStart, BLACK).toString();
// => 'Coordinate { "x": 1, "y": 1 }'

group


godash.group(board, coordinate)

Finds the set of coordinates which identifies the fully connected group for the given location.

Arguments

Returns

(Immutable.Set): Set containing Coordinate members for the group.

Example

var board = new Board(3,
    new Coordinate(1, 0), WHITE,
    new Coordinate(0, 2), WHITE,
    new Coordinate(1, 2), BLACK,
    new Coordinate(2, 2), BLACK,
    new Coordinate(2, 1), BLACK
);

toAsciiBoard(board);
// => ++X
//    X+O
//    +OO

group(board, new Coordinate(2, 1)).toString();
// => Set {
//      Coordinate { "x": 2, "y": 1 },
//      Coordinate { "x": 2, "y": 2 },
//      Coordinate { "x": 1, "y": 2 }
//    }

handicapBoard


godash.handicapBoard(size, handicap)

Creates a new Board with the correct number of handicap stones placed. Only standard board sizes (9, 13, 19) are allowed.

Arguments

  • size (number): Size of the board, must be 9, 13, or 19.
  • handicap (number): Number of handicaps, must be 0-9.

Returns

(Board): New board with correct handicaps placed.

Example

var board = handicapBoard(9, 4);

toAsciiBoard(board);
// => +++++++++
//    +++++++++
//    ++O+++O++
//    +++++++++
//    +++++++++
//    +++++++++
//    ++O+++O++
//    +++++++++
//    +++++++++

isLegalMove


godash.isLegalMove(board, coordinate, color)

Determine whether the coordinate-color combination provided is a legal move for the board. Ko is not considered. Use followupKo() if you want to do ko-related things.

Arguments

Returns

(boolean): Whether the move is legal.

Example

var ponnuki = new Board(3,
    new Coordinate(1, 0), BLACK,
    new Coordinate(0, 1), BLACK,
    new Coordinate(1, 2), BLACK,
    new Coordinate(2, 1), BLACK
);

toAsciiBoard(ponnuki);
// => +O+
//    O+O
//    +O+

isLegalMove(ponnuki, new Coordinate(1, 1), BLACK)
// => true

isLegalMove(ponnuki, new Coordinate(1, 1), WHITE)
// => false

liberties


godash.liberties(board, coordinate)

Finds the set of all liberties for the given coordinate. If the coordinate is part of a group, the set of liberties are the liberties for that group.

Arguments

Returns

(Immutable.Set): Set containing Coordinate members for the liberties of the passed coordinate.

Example

var board = new Board(3, new Coordinate(1, 1), BLACK);
var collectedLiberties = liberties(board, new Coordinate(1, 1));

Immutable.Set.of(
    new Coordinate(1, 0),
    new Coordinate(0, 1),
    new Coordinate(1, 2),
    new Coordinate(2, 1)
).equals(collectedLiberties);
// => true

libertyCount


godash.libertyCount(board, coordinate)

Counts the liberties for the given coordinate. If the coordinate is part of a group, liberties for the entire group is counted.

Arguments

Returns

(number): Count of liberties for the passed coordinate.

Example

var board = new Board(3, new Coordinate(1, 1), BLACK);

libertyCount(board, new Coordinate(1, 1)) === 4;
// => true

oppositeColor


godash.oppositeColor(color)

Toggles the passed color.

Arguments

Returns

(string): Color opposite of the one provided.

Example

oppositeColor(BLACK) === WHITE
// => true

oppositeColor(WHITE) === BLACK
// => true

placeStone


godash.placeStone(board, coordinate, color, force = false)

Places a stone on the board, ignoring the rules of Go.

Arguments

  • board (Board): Board to add stone.
  • coordinate (Coordinate): Location to add stone.
  • color (string): Stone color - BLACK or WHITE.
  • force (boolean): Optionally allow placement over existing stones.

Returns

(Board): New board with the stone placed.

Example

var ponnuki = new Board(3,
    new Coordinate(1, 0), BLACK,
    new Coordinate(0, 1), BLACK,
    new Coordinate(1, 2), BLACK,
    new Coordinate(2, 1), BLACK
);

toAsciiBoard(ponnuki);
// => +O+
//    O+O
//    +O+

toAsciiBoard(
    placeStone(ponnuki, new Coordinate(1, 1), WHITE)
);
// => +O+
//    OXO
//    +O+

placeStones


godash.placeStones(board, coordinates, color, force = false)

Places a set of stones onto the board, ignoring the rules of Go.

Arguments

  • board (Board): Board to add stone.
  • coordinate (Array): Stones to place.
  • color (string): Stone color - BLACK or WHITE.
  • force (boolean): Optionally allow placement over existing stones.

Returns

(Board): New board with the stones placed.

Example

var board = new Board(3, new Coordinate(1, 1), WHITE);

toAsciiBoard(board);
// => +++
//    +X+
//    +++

toAsciiBoard(
    placeStones(board, [
        new Coordinate(1, 0),
        new Coordinate(0, 1),
        new Coordinate(1, 2),
        new Coordinate(2, 1)
    ], BLACK)
);
// => +O+
//    OXO
//    +O+

removeStone


godash.removeStone(board, coordinate)

Make a given coordinate empty on the board.

Arguments

  • board (Board): Board from which to remove the stone.
  • coordinate (Coordinate): Location of the stone.

Returns

(Board): New board with the stone removed.

Example

var board = new Board(3, new Coordinate(1, 1), WHITE);

toAsciiBoard(board);
// => +++
//    +X+
//    +++

toAsciiBoard(
    removeStone(board, new Coordinate(1, 1))
);
// => +++
//    +++
//    +++

removeStones


godash.removeStones(board, coordinates)

Makes several coordinates empty on the board.

Arguments

  • board (Board): Board from which to remove the stones.
  • coordinate (Array): Location of the stones.

Returns

(Board): New board with the stones removed.

Example

var board = new Board(3,
    new Coordinate(1, 0), WHITE,
    new Coordinate(1, 1), WHITE,
    new Coordinate(1, 2), BLACK
);

toAsciiBoard(board);
// => +++
//    XXO
//    +++

toAsciiBoard(
    removeStones(board, [
        new Coordinate(1, 1),
        new Coordinate(1, 2)
    ])
);
// => +++
//    X++
//    +++

toAsciiBoard


godash.toAsciiBoard(board)

Constructs an ASCII representation of the board.

Arguments

  • board (Board): Board from which to add the move.

Returns

(string): ASCII representation of the board.

Example

var board = new Board(3,
    new Coordinate(1, 0), BLACK,
    new Coordinate(0, 1), BLACK,
    new Coordinate(1, 2), BLACK,
    new Coordinate(1, 1), WHITE
);

toAsciiBoard(board);
// => +O+
//    OXO
//    +++

BLACK


godash.BLACK = 'black'

Constant representing the stone color black.

WHITE


godash.WHITE = 'white'

Constant representing the stone color white.

EMPTY


godash.EMPTY = null

Constant representing an empty space.

TENGEN_9


godash.TENGEN_9 = new Coordinate(4, 4);

Constant representing tengen on a 9x9 board.

TENGEN_13


godash.TENGEN_13 = new Coordinate(6, 6);

Constant representing tengen on a 13x13 board.

TENGEN_19


godash.TENGEN_19 = new Coordinate(9, 9);

Constant representing tengen on a 19x19 board.

SGF

Utilities to support SGF ingestion.

coordinateToSgfPoint


godash.coordinateToSgfPoint(coordinate)

Converts a Coordinate to an SGF Point in the form of a Javascript String.

Arguments

Returns

(string): 2-character string representing an SGF Point

Example

coordinateToSgfPoint(new Coordinate(0, 0))
// => "aa"

sgfPointToCoordinate


godash.sgfPointToCoordinate(sgfPoint)

Converts an SGF Point to a Coordinate.

Arguments

  • sgfPoint (string): 2-character string representing an SGF Point.

Returns

(Coordinate): Coordinate for the passed string.

Example

sgfPointToCoordinate('hi').toString();
// => Coordinate { "x": 7, "y": 8 }

sgfToJS


godash.sgfToJS(sgf)

Converts a raw SGF string into a plain Javascript array. Note that unlike Board, the results of this function is a mutable object.

Arguments

  • sgf (string): Raw SGF string to be parsed.

Returns

(Array): Plain Javascript array that breaks down SGF variations and turns moves into objects.

Example

var rawSgf = `(
    ;FF[4]GM[1]SZ[19];B[aa];W[bb]
        (;B[cc];W[dd];B[ad];W[bd])
        (;B[hh];W[hg]C[what a move!])
        (;B[gg];W[gh];B[hh]
            (;W[hg];B[kk])
            (;W[kl])
        )
)`;

sgfToJS(rawSgf);
// => [
//        {FF: '4', GM: '1', SZ: '19'}, {B: 'aa'}, {W: 'bb'},
//        [
//            [{B: 'cc'}, {W: 'dd'}, {B: 'ad'}, {W: 'bd'}],
//            [{B: 'hh'}, {W: 'hg', C: 'what a move!'}],
//            [
//                {B: 'gg'}, {W: 'gh'}, {B: 'hh'},
//                [
//                    [{W: 'hg'}, {B: 'kk'}],
//                    [{W: 'kl'}]
//                ]
//            ]
//        ]
//    ];