[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

17. Application Programmers Interface to GNU Go

If you want to write your own interface to GNU Go, or if you want to create a go application using the GNU Go engine, this chapter is of interest to you.

First an overview: GNU Go consists of two parts: the GNU Go engine and a program (user interface) which uses this engine. These are linked together into one binary. The current program implements the following user modes:

The GNU Go engine can be used in other applications. For example, supplied with GNU Go is another program using the engine, called ‘debugboard’, in the directory ‘interface/debugboard/’. The program debugboard lets the user load SGF files and can then interactively look at different properties of the position such as group status and eye status.

The purpose of this Chapter is to show how to interface your own program such as debugboard with the GNU Go engine.

Figure 1 describes the structure of a program using the GNU Go engine.

                 |                                   |
                 |          Go application           |
                 |                                   |
                 +-----+----------+------+           |
                 |     |          |      |           |
                 |     |   Game   |      |           |
                 |     | handling |      |           |
                 |     |          |      |           |
                 |     +----+-----+      |           |
                 |   SGF    |    Move    |           |
                 | handling | generation |           |
                 |          |            |           |
                 |                                   |
                 |           Board handling          |
                 |                                   |
        Figure 1: The structure of a program using the GNU Go engine

The foundation is a library called libboard.a which provides efficient handling of a go board with rule checks for moves, with incremental handling of connected strings of stones and with methods to efficiently hash go positions.

On top of this, there is a library which helps the application use Smart Game Format (SGF) files, with complete handling of game trees in memory and in files. This library is called libsgf.a

The main part of the code within GNU Go is the move generation library which given a position generates a move. This part of the engine can also be used to manipulate a go position, add or remove stones, do tactical and strategic reading and to query the engine for legal moves. These functions are collected into libengine.a.

The game handling code helps the application programmer keep tracks of the moves in a game. Games can be saved to SGF files and then later be read back again. These are also within libengine.a.

The responsibility of the application is to provide the user with a user interface, graphical or not, and let the user interact with the engine.

17.1 How to use the engine in your own program: getting started

To use the GNU Go engine in your own program you must include the file ‘gnugo.h’. This file describes the whole public API. There is another file, ‘liberty.h’, which describes the internal interface within the engine. If you want to make a new module within the engine, e.g. for suggesting moves you will have to include this file also. In this section we will only describe the public interface.

Before you do anything else, you have to call the function init_gnugo(). This function initializes everything within the engine. It takes one parameter: the number of megabytes the engine can use for the internal hash table. In addition to this the engine will use a few megabytes for other purposes such as data describing groups (liberties, life status, etc), eyes and so on.

17.2 Basic Data Structures in the Engine

There are some basic definitions in gnugo.h which are used everywhere. The most important of these are the numeric declarations of colors. Each intersection on the board is represented by one of these:

     color              value
     EMPTY                0
     WHITE                1
     BLACK                2

There is a macro, OTHER_COLOR(color) which can be used to get the other color than the parameter. This macro can only be used on WHITE or BLACK, but not on EMPTY.

GNU Go uses two different representations of the board, for most purposes a one-dimensional one, but for a few purposes a two dimensional one (see section The Board Library). The one-dimensional board was introduced before GNU Go 3.2, while the two-dimensional board dates back to the ancestral program written by Man Lung Li before 1995. The API still uses the two-dimensional board, so the API functions have not changed much since GNU Go 3.0.

17.3 The board_state struct

A basic data structure in the engine is the board_state struct. This structure is internal to the engine and is defined in ‘liberty.h’.

     typedef unsigned char Intersection;
     struct board_state {
       int board_size;
       Intersection board[BOARDSIZE];
       int board_ko_pos;
       int black_captured;
       int white_captured;
       Intersection initial_board[BOARDSIZE];
       int initial_board_ko_pos;
       int initial_white_captured;
       int initial_black_captured;
       int move_history_color[MAX_MOVE_HISTORY];
       int move_history_pos[MAX_MOVE_HISTORY];
       int move_history_pointer;
       float komi;
       int move_number;

Here Intersection stores EMPTY, WHITE or BLACK. It is currently defined as an unsigned char to make it reasonably efficient in both storage and access time. The board state contains an array of Intersection's representing the board. The move history is contained in the struct. Also contained in the struct is the location of a ko (EMPTY) if the last move was not a ko capture, the komi, the number of captures, and corresponding data for the initial position at the beginning of the move history.

17.4 Functions which manipulate a Position

All the functions in the engine that manipulate Positions have names prefixed by gnugo_. These functions still use the two-dimensional representation of the board (see section The Board Array). Here is a complete list, as prototyped in ‘gnugo.h’:

17.5 Game handling

The functions (in see section Functions which manipulate a Position) are all that are needed to create a fully functional go program. But to make the life easier for the programmer, there is a small set of functions specially designed for handling ongoing games.

The data structure describing an ongoing game is the Gameinfo. It is defined as follows:

typedef struct {
  int       handicap;

  int       to_move;            /* whose move it currently is */
  SGFTree   game_record;        /* Game record in sgf format. */

  int       computer_player;    /* BLACK, WHITE, or EMPTY (used as BOTH) */

  char      outfilename[128];   /* Trickle file */
  FILE      *outfile;
} Gameinfo;

The meaning of handicap should be obvious. to_move is the color of the side whose turn it is to move.

The SGF tree game_record is used to store all the moves in the entire game, including a header node which contains, among other things, komi and handicap.

If one or both of the opponents is the computer, the field computer_player is used. Otherwise it can be ignored.

GNU Go can use a trickle file to continuously save all the moves of an ongoing game. This file can also contain information about internal state of the engine such as move reasons for various locations or move valuations. The name of this file should be stored in outfilename and the file pointer to the open file is stored in outfile. If no trickle file is used, outfilename[0] will contain a null character and outfile will be set to NULL.

17.5.1 Functions which manipulate a Gameinfo

All the functions in the engine that manipulate Gameinfos have names prefixed by gameinfo_. Here is a complete list, as prototyped in ‘gnugo.h’:

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Daniel Bump on February, 19 2009 using texi2html 1.78.