1 /*
2  * src/goboard.h, part of Complete Goban (game program)
3  * Copyright (C) 1995 William Shubert.
4  * See "configure.h.in" for more copyright information.
5  */
6 
7 
8 #ifndef  _GOBOARD_H_
9 #define  _GOBOARD_H_  1
10 
11 #ifndef  _GOHASH_H_
12 #include "goHash.h"
13 #endif
14 
15 
16 /**********************************************************************
17  * Constants
18  **********************************************************************/
19 #define  GOBOARD_MAXSIZE  38
20 
21 /**********************************************************************
22  * Data types
23  **********************************************************************/
24 typedef enum  {
25   goStone_white, goStone_black, goStone_edge, goStone_empty
26 } GoStone;
27 
28 
29 typedef struct GoBoardPiece_struct  {
30   GoStone  type;
31   int  libs, size;
32   struct GoBoardPiece_struct  *head, *tail, *next;
33   GoHash  hashStone[goStone_black+1], hashKo[goStone_black+1];
34 } GoBoardPiece;
35 
36 
37 typedef struct  {
38   int  area, dirs[4];
39   GoBoardPiece  *pieces;
40   int  caps[2];
41   GoHash  hashVal;
42   int  koLoc;
43 
44   MAGIC_STRUCT
45 } GoBoard;
46 
47 
48 /**********************************************************************
49  * Iterators
50  **********************************************************************/
51 #define  goStoneIter(s)  for ((s) = goStone_white; \
52                               (s) <= goStone_black;  \
53                               ++(s))
54 
55 typedef GoBoardPiece  *GoBoardGroupIter;
56 #define  goBoardGroupIter(i, b, l)  for ((i) = ((b)->pieces[loc].head);  \
57 					 (i);  \
58 					 (i) = (i)->next)
59 #define  goBoardGroupIter_loc(i, b)   ((i) - &(b)->pieces[0])
60 
61 
62 /**********************************************************************
63  * Functions
64  **********************************************************************/
65 #define  goStone_isStone(t)  ((t) <= goStone_black)
66 #define  goStone_char(t)  ("WB#."[t])
67 #define  goStone_opponent(s)  (goStone_white + goStone_black - (s))
68 
69 extern GoBoard  *goBoard_create(int size);
70 extern void  goBoard_destroy(GoBoard *board);
71 /*
72  * For goBoard_copy, the sizes of src and dest must be the same.
73  */
74 extern void  goBoard_copy(GoBoard *src, GoBoard *dest);
75 extern bool  goBoard_eq(GoBoard *b1, GoBoard *b2);
76 extern void  goBoard_print(GoBoard *b);
77 extern void  goBoard_fprint(GoBoard *b, FILE *fnum);
78 
79 #define  goBoard_xy2Loc(b,x,y)  ((x)+(y)*(b)->dirs[0]+(b)->dirs[0]+1)
80 extern void  goBoard_loc2Str(GoBoard *board, int loc, char *str);
81 extern int  goBoard_str2Loc(GoBoard *board, const char *str);
82 #define  goBoard_loc2X(b,l)  (((l)%(b)->dirs[0])-1)
83 #define  goBoard_loc2Y(b,l)  (((l)/(b)->dirs[0])-1)
84 extern const char  *goBoard_loc2Sgf(GoBoard *b, int l);
85 extern int  goBoard_sgf2Loc(GoBoard *b, const char *l);
86 
87 #define  goBoard_size(b)  ((b)->dirs[0] - 1)
88 #define  goBoard_width(b)  ((b)->dirs[0])
89 #define  goBoard_area(b)  ((b)->area)
90 #define  goBoard_minLoc(b)  ((b)->dirs[0])
91 #define  goBoard_maxLoc(b)  ((b)->area - (b)->dirs[0])
92 #define  goBoard_dir(b, d)  ((b)->dirs[d])
93 
94 #define  goBoard_stone(b, l)  ((b)->pieces[l].type)
95 #define  goBoard_koLoc(b)  ((b)->koLoc)
96 #define  goBoard_hash(b)  ((b)->hashVal)
97 #define  goBoard_hashNoKo(b, s)  goHash_xor((b)->hashVal,  \
98 					    (b)->pieces[(b)->koLoc].hashKo[s])
99 #define  goBoard_caps(b, p)  ((b)->caps[p])
100 #define  goBoard_addCaps(b, p, s)  (((b)->caps[p]) += s)
101 #define  goBoard_liberties(b, l)  ((b)->pieces[l].head->libs)
102 #define  goBoard_groupSize(b, l)  ((b)->pieces[l].head->size)
103 #define  goBoard_groupEq(b, l1, l2)  ((b)->pieces[l1].head == \
104 				      (b)->pieces[l2].head)
105 
106 /* goBoard_addStone(...) returns the number of stones captured. */
107 extern int  goBoard_addStone(GoBoard *board, GoStone stone, int loc,
108 			     int *suicides);
109 extern void  goBoard_rmGroup(GoBoard *board, int loc);
110 /* quickHash() always returns the hashNoKo value. */
111 extern GoHash  goBoard_quickHash(GoBoard *board, GoStone stone, int loc,
112 				 bool *suicide);
113 
114 #endif  /* _GOBOARD_H_ */
115