gomoku.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* $NetBSD: gomoku.h,v 1.10 2004/01/27 20:30:29 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Ralph Campbell.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)gomoku.h 8.2 (Berkeley) 5/3/95
  34. */
  35. #include <sys/types.h>
  36. #include <sys/endian.h>
  37. #include <stdio.h>
  38. /* board dimensions */
  39. #define BSZ 19
  40. #define BSZ1 (BSZ+1)
  41. #define BSZ2 (BSZ+2)
  42. #define BAREA (BSZ2*BSZ1+1)
  43. /* frame dimentions (based on 5 in a row) */
  44. #define FSZ1 BSZ
  45. #define FSZ2 (BSZ-4)
  46. #define FAREA (FSZ1*FSZ2 + FSZ2*FSZ2 + FSZ1*FSZ2 + FSZ2*FSZ2)
  47. #define MUP (BSZ1)
  48. #define MDOWN (-BSZ1)
  49. #define MLEFT (-1)
  50. #define MRIGHT (1)
  51. /* values for s_occ */
  52. #define BLACK 0
  53. #define WHITE 1
  54. #define EMPTY 2
  55. #define BORDER 3
  56. /* return values for makemove() */
  57. #define MOVEOK 0
  58. #define RESIGN 1
  59. #define ILLEGAL 2
  60. #define WIN 3
  61. #define TIE 4
  62. #define SAVE 5
  63. #define A 1
  64. #define B 2
  65. #define C 3
  66. #define D 4
  67. #define E 5
  68. #define F 6
  69. #define G 7
  70. #define H 8
  71. #define J 9
  72. #define K 10
  73. #define L 11
  74. #define M 12
  75. #define N 13
  76. #define O 14
  77. #define P 15
  78. #define Q 16
  79. #define R 17
  80. #define S 18
  81. #define T 19
  82. #define PT(x,y) ((x) + BSZ1 * (y))
  83. /*
  84. * A 'frame' is a group of five or six contiguous board locations.
  85. * An open ended frame is one with spaces on both ends; otherwise, its closed.
  86. * A 'combo' is a group of intersecting frames and consists of two numbers:
  87. * 'A' is the number of moves to make the combo non-blockable.
  88. * 'B' is the minimum number of moves needed to win once it can't be blocked.
  89. * A 'force' is a combo that is one move away from being non-blockable
  90. *
  91. * Single frame combo values:
  92. * <A,B> board values
  93. * 5,0 . . . . . O
  94. * 4,1 . . . . . .
  95. * 4,0 . . . . X O
  96. * 3,1 . . . . X .
  97. * 3,0 . . . X X O
  98. * 2,1 . . . X X .
  99. * 2,0 . . X X X O
  100. * 1,1 . . X X X .
  101. * 1,0 . X X X X O
  102. * 0,1 . X X X X .
  103. * 0,0 X X X X X O
  104. *
  105. * The rule for combining two combos (<A1,B1> <A2,B2>)
  106. * with V valid intersection points, is:
  107. * A' = A1 + A2 - 2 - V
  108. * B' = MIN(A1 + B1 - 1, A2 + B2 - 1)
  109. * Each time a frame is added to the combo, the number of moves to complete
  110. * the force is the number of moves needed to 'fill' the frame plus one at
  111. * the intersection point. The number of moves to win is the number of moves
  112. * to complete the best frame minus the last move to complete the force.
  113. * Note that it doesn't make sense to combine a <1,x> with anything since
  114. * it is already a force. Also, the frames have to be independent so a
  115. * single move doesn't affect more than one frame making up the combo.
  116. *
  117. * Rules for comparing which of two combos (<A1,B1> <A2,B2>) is better:
  118. * Both the same color:
  119. * <A',B'> = (A1 < A2 || A1 == A2 && B1 <= B2) ? <A1,B1> : <A2,B2>
  120. * We want to complete the force first, then the combo with the
  121. * fewest moves to win.
  122. * Different colors, <A1,B1> is the combo for the player with the next move:
  123. * <A',B'> = A2 <= 1 && (A1 > 1 || A2 + B2 < A1 + B1) ? <A2,B2> : <A1,B1>
  124. * We want to block only if we have to (i.e., if they are one move away
  125. * from completing a force and we don't have a force that we can
  126. * complete which takes fewer or the same number of moves to win).
  127. */
  128. #define MAXA 6
  129. #define MAXB 2
  130. #define MAXCOMBO 0x600
  131. union comboval {
  132. struct {
  133. #if BYTE_ORDER == BIG_ENDIAN
  134. u_char a; /* # moves to complete force */
  135. u_char b; /* # moves to win */
  136. #endif
  137. #if BYTE_ORDER == LITTLE_ENDIAN
  138. u_char b; /* # moves to win */
  139. u_char a; /* # moves to complete force */
  140. #endif
  141. } c;
  142. u_short s;
  143. };
  144. /*
  145. * This structure is used to record information about single frames (F) and
  146. * combinations of two more frames (C).
  147. * For combinations of two or more frames, there is an additional
  148. * array of pointers to the frames of the combination which is sorted
  149. * by the index into the frames[] array. This is used to prevent duplication
  150. * since frame A combined with B is the same as B with A.
  151. * struct combostr *c_sort[size c_nframes];
  152. * The leaves of the tree (frames) are numbered 0 (bottom, leftmost)
  153. * to c_nframes - 1 (top, right). This is stored in c_frameindex and
  154. * c_dir if C_LOOP is set.
  155. */
  156. struct combostr {
  157. struct combostr *c_next; /* list of combos at the same level */
  158. struct combostr *c_prev; /* list of combos at the same level */
  159. struct combostr *c_link[2]; /* C:previous level or F:NULL */
  160. union comboval c_linkv[2]; /* C:combo value for link[0,1] */
  161. union comboval c_combo; /* C:combo value for this level */
  162. u_short c_vertex; /* C:intersection or F:frame head */
  163. u_char c_nframes; /* number of frames in the combo */
  164. u_char c_dir; /* C:loop frame or F:frame direction */
  165. u_char c_flg; /* C:combo flags */
  166. u_char c_frameindex; /* C:intersection frame index */
  167. u_char c_framecnt[2]; /* number of frames left to attach */
  168. u_char c_emask[2]; /* C:bit mask of completion spots for
  169. * link[0] and link[1] */
  170. u_char c_voff[2]; /* C:vertex offset within frame */
  171. };
  172. /* flag values for c_flg */
  173. #define C_OPEN_0 0x01 /* link[0] is an open ended frame */
  174. #define C_OPEN_1 0x02 /* link[1] is an open ended frame */
  175. #define C_LOOP 0x04 /* link[1] intersects previous frame */
  176. #define C_MARK 0x08 /* indicates combo processed */
  177. /*
  178. * This structure is used for recording the completion points of
  179. * multi frame combos.
  180. */
  181. struct elist {
  182. struct elist *e_next; /* list of completion points */
  183. struct combostr *e_combo; /* the whole combo */
  184. u_char e_off; /* offset in frame of this empty spot */
  185. u_char e_frameindex; /* intersection frame index */
  186. u_char e_framecnt; /* number of frames left to attach */
  187. u_char e_emask; /* real value of the frame's emask */
  188. union comboval e_fval; /* frame combo value */
  189. };
  190. /*
  191. * One spot structure for each location on the board.
  192. * A frame consists of the combination for the current spot plus the five spots
  193. * 0: right, 1: right & down, 2: down, 3: down & left.
  194. */
  195. struct spotstr {
  196. short s_occ; /* color of occupant */
  197. short s_wval; /* weighted value */
  198. int s_flg; /* flags for graph walks */
  199. struct combostr *s_frame[4]; /* level 1 combo for frame[dir] */
  200. union comboval s_fval[2][4]; /* combo value for [color][frame] */
  201. union comboval s_combo[2]; /* minimum combo value for BLK & WHT */
  202. u_char s_level[2]; /* number of frames in the min combo */
  203. u_char s_nforce[2]; /* number of <1,x> combos */
  204. struct elist *s_empty; /* level n combo completion spots */
  205. struct elist *s_nempty; /* level n+1 combo completion spots */
  206. int dummy[2]; /* XXX */
  207. };
  208. /* flag values for s_flg */
  209. #define CFLAG 0x000001 /* frame is part of a combo */
  210. #define CFLAGALL 0x00000F /* all frame directions marked */
  211. #define IFLAG 0x000010 /* legal intersection point */
  212. #define IFLAGALL 0x0000F0 /* any intersection points? */
  213. #define FFLAG 0x000100 /* frame is part of a <1,x> combo */
  214. #define FFLAGALL 0x000F00 /* all force frames */
  215. #define MFLAG 0x001000 /* frame has already been seen */
  216. #define MFLAGALL 0x00F000 /* all frames seen */
  217. #define BFLAG 0x010000 /* frame intersects border or dead */
  218. #define BFLAGALL 0x0F0000 /* all frames dead */
  219. /*
  220. * This structure is used to store overlap information between frames.
  221. */
  222. struct ovlp_info {
  223. int o_intersect; /* intersection spot */
  224. struct combostr *o_fcombo; /* the connecting combo */
  225. u_char o_link; /* which link to update (0 or 1) */
  226. u_char o_off; /* offset in frame of intersection */
  227. u_char o_frameindex; /* intersection frame index */
  228. };
  229. extern const char *letters;
  230. extern char fmtbuf[];
  231. extern const char pdir[];
  232. extern const int dd[4];
  233. extern struct spotstr board[BAREA]; /* info for board */
  234. extern struct combostr frames[FAREA]; /* storage for single frames */
  235. extern struct combostr *sortframes[2]; /* sorted, non-empty frames */
  236. extern u_char overlap[FAREA * FAREA]; /* frame [a][b] overlap */
  237. extern short intersect[FAREA * FAREA]; /* frame [a][b] intersection */
  238. extern int movelog[BSZ * BSZ]; /* history of moves */
  239. extern int movenum;
  240. extern int debug;
  241. #define ASSERT(x)
  242. void bdinit(struct spotstr *);
  243. void init_overlap(void);
  244. int getline(char *, int);
  245. void ask(const char *);
  246. void dislog(const char *);
  247. void bdump(FILE *);
  248. void bdisp(void);
  249. void bdisp_init(void);
  250. void cursfini(void);
  251. void cursinit(void);
  252. void bdwho(int);
  253. void panic(const char *) __attribute__((__noreturn__));
  254. void glog(const char *);
  255. void dlog(const char *);
  256. void quit(void) __attribute__((__noreturn__));
  257. void quitsig(int) __attribute__((__noreturn__));
  258. void whatsup(int);
  259. int readinput(FILE *);
  260. const char *stoc(int);
  261. int lton(int);
  262. int ctos(const char *);
  263. void update_overlap(struct spotstr *);
  264. int makemove(int, int);
  265. int list_eq(struct combostr **, struct combostr **, int);
  266. void clearcombo(struct combostr *, int);
  267. void makeempty(struct combostr *);
  268. void appendcombo(struct combostr *, int);
  269. void updatecombo(struct combostr *, int);
  270. void markcombo(struct combostr *);
  271. void printcombo(struct combostr *, char *);
  272. void makecombo(struct combostr *, struct spotstr *, int, int);
  273. void makecombo2(struct combostr *, struct spotstr *, int, int);
  274. int sortcombo(struct combostr **, struct combostr **, struct combostr *);
  275. int checkframes(struct combostr *, struct combostr *, struct spotstr *,
  276. int, struct ovlp_info *);
  277. void addframes(int);
  278. void scanframes(int);
  279. int better(const struct spotstr *, const struct spotstr *, int);
  280. int pickmove(int);