Next: Influence Utilities, Previous: Print Utilities, Up: Utility Functions
The functions documented in this section are from board.c. Other functions in board.c are described in See Some Board Functions.
void store_board(struct board_state *state)
Save board state.
void restore_board(struct board_state *state)
Restore a saved board state.
void clear_board(void)
Clear the internal board.
void dump_stack(void)
for use under GDB prints the move stack.
void add_stone(int pos, int color)
Place a stone on the board and update the board_hash. This operation destroys all move history.
void remove_stone(int pos)
Remove a stone from the board and update the board_hash. This operation destroys the move history.
int is_pass(int pos)
Test if the move is a pass or not. Return 1 if it is.
int is_legal(int pos, int color)
Determines whether the movecolor
atpos
is legal.
int is_suicide(int pos, int color)
Determines whether the movecolor
atpos
would be a suicide. This is the case if
- There is no neighboring empty intersection.
- There is no neighboring opponent string with exactly one liberty.
- There is no neighboring friendly string with more than one liberty.
int is_illegal_ko_capture(int pos, int color)
Determines whether the movecolor
atpos
would be an illegal ko capture.
int is_edge_vertex(int pos)
Determine whether vertex is on the edge.
int edge_distance(int pos)
Distance to the edge.
int is_corner_vertex(int pos)
Determine whether vertex is a corner.
int get_komaster()
int get_kom_pos()
Public functions to access the variablekomaster
andkom_pos
, which are static in board.c.
Next we come to countlib()
and its allies, which
address the problem of determining how many liberties a
string has. Although countlib()
addresses this
basic question, other functions can often get the needed
information more quickly, so there are a number of
different functions in this family.
int countlib(int str)
Count the number of liberties of the string at pos
. There
must be a stone at this location.
int findlib(int str, int maxlib, int *libs)
Find the liberties of the string atstr
. This location must not be empty. The locations of up to maxlib liberties are written intolibs[]
. The full number of liberties is returned. If you want the locations of all liberties, whatever their number, you should passMAXLIBS
as the value formaxlib
and allocate space forlibs[]
accordingly.
int fastlib(int pos, int color, int ignore_captures)
Count the liberties a stone of the given color would get if played atpos
. The intent of this function is to be as fast as possible, not necessarily complete. But if it returns a positive value (meaning it has succeeded), the value is guaranteed to be correct. Captures are ignored based if theignore_captures
field is nonzero. The locationpos
must be empty. The function fails if there are more than two neighbor strings of the same color. In this case, the return value is -1. Captures are handled in a very limited way, so if ignore_capture is 0, and a capture is required, it will often return -1.
int approxlib(int pos, int color, int maxlib, int *libs)
Find the liberties a stone of the given color would get if played atpos
, ignoring possible captures of opponent stones. The locationpos
must be empty. Iflibs != NULL
, the locations of up tomaxlib
liberties are written intolibs[]
. The counting of liberties may or may not be halted whenmaxlib
is reached. The number of liberties found is returned, which may be less than the total number of liberties ifmaxlib
is small. If you want the number or the locations of all liberties, however many they are, you should passMAXLIBS
as the value for maxlib and allocate space forlibs[]
accordingly.
int accuratelib(int pos, int color, int maxlib, int *libs)
Find the liberties a stone of the given color would get if played atpos
. This function takes into consideration all captures. Its return value is exact in that sense it counts all the liberties, unlessmaxlib
allows it to stop earlier. The locationpos
must be empty. Iflibs != NULL
, the locations of up tomaxlib
liberties are written intolibs[]
. The counting of liberties may or may not be halted whenmaxlib
is reached. The number of found liberties is returned. This function guarantees that liberties which are not results of captures come first inlibs[]
array. To find whether all the liberties starting from a given one are results of captures, one may useif (board[libs[k]] != EMPTY)
construction. If you want the number or the locations of all liberties, however many they are, you should passMAXLIBS
as the value formaxlib
and allocate space forlibs[]
accordingly.
Next we have some general utility functions.
int count_common_libs(int str1, int str2)
Find the number of common liberties of the two strings.
int find_common_libs(int str1, int str2, int maxlib, int *libs)
Find the common liberties of the two strings. The locations of up tomaxlib
common liberties are written intolibs[]
. The full number of common liberties is returned. If you want the locations of all common liberties, whatever their number, you should passMAXLIBS
as the value formaxlib
and allocate space forlibs[]
accordingly.
int have_common_lib(int str1, int str2, int *lib)
Determine whether two strings have at least one common liberty. If they do andlib != NULL
, one common liberty is returned in*lib
.
int countstones(int str)
Report the number of stones in a string.
int findstones(int str, int maxstones, int *stones)
Find the stones of the string atstr
. The location must not be empty. The locations of up to maxstones stones are written intostones[]
. The full number of stones is returned.
int chainlinks(int str, int adj[MAXCHAIN])
This very useful function returns (in theadj
array) the chains surrounding the string atstr
. The number of chains is returned.
int chainlinks2(int str, int adj[MAXCHAIN], int lib)
Returns (inadj
array) those chains surrounding the string atstr
, which has exactlylib
liberties. The number of such chains is returned.
int chainlinks3(int str, int adj[MAXCHAIN], int lib)
Returns (inadj
array) the chains surrounding the string atstr
, which have less or equallib
liberties. The number of such chains is returned.
int extended_chainlinks(int str, int adj[MAXCHAIN], int both_colors)
Returns (in theadj
array) the opponent strings being directly adjacent tostr
or having a common liberty withstr
. The number of such strings is returned. If the both_colors parameter is true, also own strings sharing a liberty are returned.
int find_origin(int str)
Find the origin of a string, i.e. the point with the smallest 1D board coordinate. The idea is to have a canonical reference point for a string.
int is_self_atari(int pos, int color)
Determine whether a move by color at pos
would be a self atari,
i.e. whether it would get more than one liberty. This function
returns true also for the case of a suicide move.
int liberty_of_string(int pos, int str)
Returns true ifpos
is a liberty of the string atstr
.
int second_order_liberty_of_string(int pos, int str)
Returns true if pos
is a second order liberty of the string at str.
int neighbor_of_string(int pos, int str)
Returns true ifpos
is adjacent to the string atstr
.
int has_neighbor(int pos, int color)
Returns true ifpos
has a neighbor ofcolor
.
int same_string(int str1, int str2)
Returns true ifstr1
andstr2
belong to the same string.
int adjacent_strings(int str1, int str2)
Returns true if the strings atstr1
andstr2
are adjacent.
int is_ko(int pos, int color, int *ko_pos)
Return true if the movepos
bycolor
is a ko capture (whether capture is legal on this move or not). If so, and ifko_pos
is not aNULL
pointer, then*ko_pos
returns the location of the captured ko stone. If the move is not a ko capture,*ko_pos
is set to 0. A move is a ko capture if and only if
- All neighbors are opponent stones.
- The number of captured stones is exactly one.
int is_ko_point(int pos)
Return true ifpos
is either a stone, which if captured would give ko, or ifpos
is an empty intersection adjacent to a ko stone.
int does_capture_something(int pos, int color)
Returns 1 if at least one string is captured when color plays at pos
.
void mark_string(int str, char mx[BOARDMAX], char mark)
For each stone in the string at pos, set mx
to value mark. If
some of the stones in the string are marked prior to calling this
function, only the connected unmarked stones starting from pos
are guaranteed to become marked. The rest of the string may or may
not become marked. (In the current implementation, it will.)
int move_in_stack(int pos, int cutoff)
Returns true if at least one move has been played at pos
at deeper than level cutoff
in the reading tree.
int stones_on_board(int color)
Return the number of stones of the indicated color(s) on the board. This only counts stones in the permanent position, not stones placed bytrymove()
ortryko()
. Usestones_on_board(BLACK | WHITE)
to get the total number of stones on the board.