1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* This is GNU Go, a Go program. Contact gnugo@gnu.org, or see *
* http://www.gnu.org/software/gnugo/ for more information. *
* *
* Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
* 2008 and 2009 by the Free Software Foundation. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation - version 3 or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License in file COPYING for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02111, USA. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef _BOARD_H_
#define _BOARD_H_
#include <stdarg.h>
#include "config.h"
#include "sgftree.h"
#include "winsocket.h"
/* This type is used to store each intersection on the board.
*
* On a 486, char is best, since the time taken to push and pop
* becomes significant otherwise. On other platforms, an int may
* be better, e.g. if memcpy() is particularly fast, or if
* character access is very slow.
*/
typedef unsigned char Intersection;
/* FIXME: This is very ugly but we can't include hash.h until we have
* defined Intersection. And we do need to include it before using
* Hash_data.
*/
#include "hash.h"
/* local versions of absolute value, min and max */
#define gg_abs(x) ((x) < 0 ? -(x) : (x))
#define gg_min(a, b) ((a)<(b) ? (a) : (b))
#define gg_max(a, b) ((a)<(b) ? (b) : (a))
/* Avoid compiler warnings with unused parameters */
#define UNUSED(x) (void)x
/* A string with n stones can have at most 2(n+1) liberties. From this
* follows that an upper bound on the number of liberties of a string
* on a board of size N^2 is 2/3 (N^2+1).
*/
#define MAXLIBS (2*(MAX_BOARD*MAX_BOARD + 1)/3)
/* This is a smaller, practical number of liberties that we care to keep track of. */
#define MAX_LIBERTIES 8
/* This is an upper bound on the number of strings that can exist on
* the board simultaneously. Since each string must have at least one
* liberty and each empty point can provide a liberty to at most four
* strings, at least one out of five board points must be empty.
*
* FIXME: This is not sufficiently large. Above stackp==0, the
* incremental board code doesn't re-use the entries for
* removed or merged strings, while new strings require new
* entries. This is a problem only in very pathological cases,
* and is extremely unlikely to occur in practice.
*
* Actually, in the not all that pathological case of a
* repeated triple ko cycle, each move creates a new string and
* thus makes use of one more string, which relatively quickly
* will exhaust the available strings. For a safe upper bound
* MAX_STRINGS should be set to
* MAX_STACK + 4 * MAX_BOARD * MAX_BOARD / 5.
* It's not clear that it's worth the extra memory, however.
*/
#define MAX_STRINGS (4 * MAX_BOARD * MAX_BOARD / 5)
/* Per gf: Unconditional_life() can get very close to filling the
* entire board under certain circumstances. This was discussed in
* the list around August 21, 2001, in a thread with the subject
* "gnugo bug logs".
*/
#define MAXSTACK MAX_BOARD * MAX_BOARD
#define MAXCHAIN 160
#define HASH_RANDOM_SEED 12345
/* ================================================================ *
* One-dimensional board *
* ================================================================ */
/* Board sizes */
#define MIN_BOARD 1 /* Minimum supported board size. */
#define MAX_BOARD 19 /* Maximum supported board size. */
#define MAX_HANDICAP 9 /* Maximum supported handicap. */
#define MAX_MOVE_HISTORY 500 /* Max number of moves remembered. */
#define DEFAULT_BOARD_SIZE MAX_BOARD
/* Colors and komaster states. */
enum colors {
EMPTY,
WHITE,
BLACK,
GRAY,
GRAY_WHITE,
GRAY_BLACK,
WEAK_KO,
NUM_KOMASTER_STATES
};
#define COLOR_NAMES \
"empty", \
"white", \
"black", \
"gray", \
"gray_white", \
"gray_black", \
"weak_ko"
const char *color_to_string(int color);
#define OTHER_COLOR(color) (WHITE+BLACK-(color))
#define IS_STONE(arg) ((arg) == WHITE || (arg) == BLACK)
/* Note that POS(-1, -1) == 0
* DELTA() is defined so that POS(i+di, j+dj) = POS(i, j) + DELTA(di, dj).
*/
#define BOARDSIZE ((MAX_BOARD + 2) * (MAX_BOARD + 1) + 1)
#define BOARDMIN (MAX_BOARD + 2)
#define BOARDMAX (MAX_BOARD + 1) * (MAX_BOARD + 1)
#define POS(i, j) ((MAX_BOARD + 2) + (i) * (MAX_BOARD + 1) + (j))
#define DELTA(di, dj) ((di) * (MAX_BOARD + 1) + (dj))
#define I(pos) ((pos) / (MAX_BOARD + 1) - 1)
#define J(pos) ((pos) % (MAX_BOARD + 1) - 1)
#define PASS_MOVE 0
#define NO_MOVE PASS_MOVE
#define NS (MAX_BOARD + 1)
#define WE 1
#define SOUTH(pos) ((pos) + NS)
#define WEST(pos) ((pos) - 1)
#define NORTH(pos) ((pos) - NS)
#define EAST(pos) ((pos) + 1)
#define SW(pos) ((pos) + NS - 1)
#define NW(pos) ((pos) - NS - 1)
#define NE(pos) ((pos) - NS + 1)
#define SE(pos) ((pos) + NS + 1)
#define SS(pos) ((pos) + 2 * NS)
#define WW(pos) ((pos) - 2)
#define NN(pos) ((pos) - 2 * NS)
#define EE(pos) ((pos) + 2)
#define DIRECT_NEIGHBORS(pos1, pos2) \
((pos1) == SOUTH(pos2) \
|| (pos1) == WEST(pos2) \
|| (pos1) == NORTH(pos2) \
|| (pos1) == EAST(pos2))
#define DIAGONAL_NEIGHBORS(pos1, pos2) \
((pos1) == SW(pos2) \
|| (pos1) == NW(pos2) \
|| (pos1) == NE(pos2) \
|| (pos1) == SE(pos2))
#define BOARD(i, j) board[POS(i, j)]
#define MIRROR_MOVE(pos) POS(board_size - 1 - I(pos), board_size - 1 - J(pos))
/* ================================================================ */
/* global variables */
/* ================================================================ */
/* The board and the other parameters deciding the current position. */
extern int board_size; /* board size (usually 19) */
extern Intersection board[BOARDSIZE]; /* go board */
extern int board_ko_pos;
extern int black_captured; /* num. of black stones captured */
extern int white_captured;
extern Intersection initial_board[BOARDSIZE];
extern int initial_board_ko_pos;
extern int initial_white_captured;
extern int initial_black_captured;
extern int move_history_color[MAX_MOVE_HISTORY];
extern int move_history_pos[MAX_MOVE_HISTORY];
extern Hash_data move_history_hash[MAX_MOVE_HISTORY];
extern int move_history_pointer;
extern float komi;
extern int handicap; /* used internally in chinese scoring */
extern int movenum; /* movenumber - used for debug output */
extern signed char shadow[BOARDMAX]; /* reading tree shadow */
enum suicide_rules {
FORBIDDEN,
ALLOWED,
ALL_ALLOWED
};
extern enum suicide_rules suicide_rule;
enum ko_rules {
SIMPLE,
NONE,
PSK,
SSK
};
extern enum ko_rules ko_rule;
extern int stackp; /* stack pointer */
extern int count_variations; /* count (decidestring) */
extern SGFTree *sgf_dumptree;
/* This struct holds the internal board state. */
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];
Hash_data move_history_hash[MAX_MOVE_HISTORY];
int move_history_pointer;
float komi;
int handicap;
int move_number;
};
/* This is increased by one anytime a move is (permanently) played or
* the board is cleared.
*/
extern int position_number;
/* ================================================================ */
/* board.c functions */
/* ================================================================ */
/* Functions handling the permanent board state. */
void clear_board(void);
int test_gray_border(void);
void setup_board(Intersection new_board[MAX_BOARD][MAX_BOARD], int ko_pos,
int *last, float new_komi, int w_captured, int b_captured);
void add_stone(int pos, int color);
void remove_stone(int pos);
void play_move(int pos, int color);
int undo_move(int n);
void store_board(struct board_state *state);
void restore_board(struct board_state *state);
/* Information about the permanent board. */
int get_last_move(void);
int get_last_player(void);
int get_last_opponent_move(int color);
int stones_on_board(int color);
/* Functions handling the variable board state. */
int trymove(int pos, int color, const char *message, int str);
int tryko(int pos, int color, const char *message);
void popgo(void);
int komaster_trymove(int pos, int color,
const char *message, int str,
int *is_conditional_ko, int consider_conditional_ko);
int get_komaster(void);
int get_kom_pos(void);
int move_in_stack(int pos, int cutoff);
void get_move_from_stack(int k, int *move, int *color);
void dump_stack(void);
void do_dump_stack(void);
void reset_trymove_counter(void);
int get_trymove_counter(void);
/* move properties */
int is_pass(int pos);
int is_legal(int pos, int color);
int is_suicide(int pos, int color);
int is_illegal_ko_capture(int pos, int color);
int is_allowed_move(int pos, int color);
int is_ko(int pos, int color, int *ko_pos);
int is_ko_point(int pos);
int does_capture_something(int pos, int color);
int is_self_atari(int pos, int color);
/* Purely geometric functions. */
int is_edge_vertex(int pos);
int is_corner_vertex(int pos);
int edge_distance(int pos);
int square_dist(int pos1, int pos2);
int rotate1(int pos, int rot);
/* Basic string information. */
int find_origin(int str);
int chainlinks(int str, int adj[MAXCHAIN]);
int chainlinks2(int str, int adj[MAXCHAIN], int lib);
int chainlinks3(int str, int adj[MAXCHAIN], int lib);
int extended_chainlinks(int str, int adj[MAXCHAIN], int both_colors);
int liberty_of_string(int pos, int str);
int second_order_liberty_of_string(int pos, int str);
int neighbor_of_string(int pos, int str);
int has_neighbor(int pos, int color);
int same_string(int str1, int str2);
int adjacent_strings(int str1, int str2);
void mark_string(int str, signed char mx[BOARDMAX], signed char mark);
int are_neighbors(int pos1, int pos2);
/* Count and/or find liberties at (pos). */
int countlib(int str);
int findlib(int str, int maxlib, int *libs);
int fastlib(int pos, int color, int ignore_captures);
int approxlib(int pos, int color, int maxlib, int *libs);
int accuratelib(int pos, int color, int maxlib, int *libs);
int count_common_libs(int str1, int str2);
int find_common_libs(int str1, int str2, int maxlib, int *libs);
int have_common_lib(int str1, int str2, int *lib);
/* Count the number of stones in a string. */
int countstones(int str);
int findstones(int str, int maxstones, int *stones);
int count_adjacent_stones(int str1, int str2, int maxstones);
/* Detect a special shape. */
int send_two_return_one(int move, int color);
/* Special function for reading.c */
void incremental_order_moves(int move, int color, int string,
int *number_edges, int *number_same_string,
int *number_own, int *number_opponent,
int *captured_stones, int *threatened_stones,
int *saved_stones, int *number_open);
/* Board caches initialization functions. */
void clear_approxlib_cache(void);
void clear_accuratelib_cache(void);
/* Is this point inside the board? */
#if 0
#define ON_BOARD2(i, j) ((i)>=0 && (j)>=0 && (i)<board_size && (j)<board_size)
#else
/*
* For the case when expr can only be slightly negative,
* if (expr < 0 || expr > something)
* is equivalent to
* if ((unsigned) expr > something)
*
* (I think gcc knows this trick, but it does no harm to
* encode it explicitly since it saves typing !)
*/
#define ON_BOARD2(i, j) ((unsigned) (i) < (unsigned) board_size &&\
(unsigned) (j) < (unsigned) board_size)
#endif
#define ASSERT_ON_BOARD2(i, j) ASSERT2(ON_BOARD2((i), (j)), (i), (j))
#define ON_BOARD1(pos) (((unsigned) (pos) < BOARDSIZE) && board[pos] != GRAY)
#define ON_BOARD(pos) (board[pos] != GRAY)
#define ASSERT_ON_BOARD1(pos) ASSERT1(ON_BOARD1(pos), (pos))
/* Coordinates for the eight directions, ordered
* south, west, north, east, southwest, northwest, northeast, southeast.
* Defined in board.c.
*/
extern int deltai[8]; /* = { 1, 0, -1, 0, 1, -1, -1, 1}; */
extern int deltaj[8]; /* = { 0, -1, 0, 1, -1, -1, 1, 1}; */
extern int delta[8]; /* = { NS, -1, -NS, 1, NS-1, -NS-1, -NS+1, NS+1}; */
/* ================================================================ */
/* Other functions */
/* ================================================================ */
/* SGF routines for debugging purposes in sgffile.c */
void sgffile_begindump(struct SGFTree_t *tree);
void sgffile_enddump(const char *filename);
/* Hashing and Caching statistics. */
struct stats_data {
int nodes; /* Number of visited nodes while reading */
int read_result_entered; /* Number of read results entered. */
int read_result_hits; /* Number of hits of read results. */
int trusted_read_result_hits; /* Number of hits of read results */
/* with sufficient remaining depth. */
};
extern struct stats_data stats;
/* printutils.c */
int gprintf(const char *fmt, ...);
void vgprintf(FILE *outputfile, const char *fmt, va_list ap);
void mprintf(const char *fmt, ...);
void gfprintf(FILE *outfile, const char *fmt, ...);
const char *color_to_string(int color);
const char *location_to_string(int pos);
void location_to_buffer(int pos, char *buf);
int string_to_location(int boardsize, const char *str);
int is_hoshi_point(int m, int n);
void draw_letter_coordinates(FILE *outfile);
void simple_showboard(FILE *outfile);
void mark_goal_in_sgf(signed char goal[BOARDMAX]);
/* ================================================================ */
/* assertions */
/* ================================================================ */
/* Our own abort() which prints board state on the way out.
* (pos) is a "relevant" board position for info.
*/
void abortgo(const char *file, int line, const char *msg, int pos)
#ifdef __GNUC__
__attribute__ ((noreturn))
#endif
;
#ifdef GG_TURN_OFF_ASSERTS
#define ASSERT2(x, i, j)
#define ASSERT1(x, pos)
#else
/* avoid dangling else */
/* FIXME: Should probably re-write these using do {...} while (0) idiom. */
#define ASSERT2(x, i, j) if (x) ; else abortgo(__FILE__, __LINE__, #x, POS(i, j))
#define ASSERT1(x, pos) if (x) ; else abortgo(__FILE__, __LINE__, #x, pos)
#endif
#define gg_assert(x) ASSERT1(x, NO_MOVE)
/* Are we using valgrind memory checking? */
#if USE_VALGRIND
#include <valgrind/memcheck.h>
#else
#define VALGRIND_MAKE_WRITABLE(a, b)
#endif
#endif /* _BOARD_H_ */
/*
* Local Variables:
* tab-width: 8
* c-basic-offset: 2
* End:
*/
|