xref: /original-bsd/games/gomoku/gomoku.h (revision 27393bdf)
1 /*
2  * Copyright (c) 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ralph Campbell.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)gomoku.h	8.1 (Berkeley) 07/24/94
11  */
12 
13 #include <sys/types.h>
14 
15 /* board dimensions */
16 #define BSZ	19
17 #define BSZ1	(BSZ+1)
18 #define BSZ2	(BSZ+2)
19 #define BAREA	(BSZ2*BSZ1+1)
20 
21 /* frame dimentions (based on 5 in a row) */
22 #define FSZ1	BSZ
23 #define FSZ2	(BSZ-4)
24 #define FAREA	(FSZ1*FSZ2 + FSZ2*FSZ2 + FSZ1*FSZ2 + FSZ2*FSZ2)
25 
26 #define MUP	(BSZ1)
27 #define MDOWN	(-BSZ1)
28 #define MLEFT	(-1)
29 #define MRIGHT	(1)
30 
31 /* values for s_occ */
32 #define BLACK	0
33 #define WHITE	1
34 #define EMPTY	2
35 #define BORDER	3
36 
37 /* return values for makemove() */
38 #define MOVEOK	0
39 #define RESIGN	1
40 #define ILLEGAL	2
41 #define WIN	3
42 #define TIE	4
43 #define SAVE	5
44 
45 #define A 1
46 #define B 2
47 #define C 3
48 #define D 4
49 #define E 5
50 #define F 6
51 #define G 7
52 #define H 8
53 #define J 9
54 #define K 10
55 #define L 11
56 #define M 12
57 #define N 13
58 #define O 14
59 #define P 15
60 #define Q 16
61 #define R 17
62 #define S 18
63 #define T 19
64 
65 #define PT(x,y)		((x) + BSZ1 * (y))
66 
67 /*
68  * A 'frame' is a group of five or six contiguous board locations.
69  * An open ended frame is one with spaces on both ends; otherwise, its closed.
70  * A 'combo' is a group of intersecting frames and consists of two numbers:
71  * 'A' is the number of moves to make the combo non-blockable.
72  * 'B' is the minimum number of moves needed to win once it can't be blocked.
73  * A 'force' is a combo that is one move away from being non-blockable
74  *
75  * Single frame combo values:
76  *     <A,B>	board values
77  *	5,0	. . . . . O
78  *	4,1	. . . . . .
79  *	4,0	. . . . X O
80  *	3,1	. . . . X .
81  *	3,0	. . . X X O
82  *	2,1	. . . X X .
83  *	2,0	. . X X X O
84  *	1,1	. . X X X .
85  *	1,0	. X X X X O
86  *	0,1	. X X X X .
87  *	0,0	X X X X X O
88  *
89  * The rule for combining two combos (<A1,B1> <A2,B2>)
90  * with V valid intersection points, is:
91  *	A' = A1 + A2 - 2 - V
92  *	B' = MIN(A1 + B1 - 1, A2 + B2 - 1)
93  * Each time a frame is added to the combo, the number of moves to complete
94  * the force is the number of moves needed to 'fill' the frame plus one at
95  * the intersection point. The number of moves to win is the number of moves
96  * to complete the best frame minus the last move to complete the force.
97  * Note that it doesn't make sense to combine a <1,x> with anything since
98  * it is already a force. Also, the frames have to be independent so a
99  * single move doesn't affect more than one frame making up the combo.
100  *
101  * Rules for comparing which of two combos (<A1,B1> <A2,B2>) is better:
102  * Both the same color:
103  *	<A',B'> = (A1 < A2 || A1 == A2 && B1 <= B2) ? <A1,B1> : <A2,B2>
104  *	We want to complete the force first, then the combo with the
105  *	fewest moves to win.
106  * Different colors, <A1,B1> is the combo for the player with the next move:
107  *	<A',B'> = A2 <= 1 && (A1 > 1 || A2 + B2 < A1 + B1) ? <A2,B2> : <A1,B1>
108  *	We want to block only if we have to (i.e., if they are one move away
109  *	from completing a force and we don't have a force that we can
110  *	complete which takes fewer or the same number of moves to win).
111  */
112 
113 #define MAXA		6
114 #define MAXB		2
115 #define MAXCOMBO	0x600
116 #define MAXDEPTH	5
117 
118 union	combo {
119 	struct {
120 #if BYTE_ORDER == BIG_ENDIAN
121 		u_char	a;	/* # moves to complete force */
122 		u_char	b;	/* # moves to win */
123 #endif
124 #if BYTE_ORDER == LITTLE_ENDIAN
125 		u_char	b;	/* # moves to win */
126 		u_char	a;	/* # moves to complete force */
127 #endif
128 	} c;
129 	u_short	s;
130 };
131 
132 /*
133  * This structure is used to record combinations of two more frames.
134  * This is so we can do it incrementally in makemove() and because
135  * we don't want to combine frames with <1,x> combos.
136  */
137 struct combostr {
138 	struct combostr	*c_next;	/* list of combos at the same level */
139 	struct combostr	*c_prev;	/* list of combos at the same level */
140 	struct combostr	*c_link[2];	/* previous level or NULL if level 1 */
141 	union combo	c_combo;	/* combo value for this level */
142 	u_short		c_vertex;	/* intersection or frame head */
143 	u_char		c_nframes;	/* number of frames in the combo */
144 	u_char		c_dir;		/* which direction */
145 	u_char		c_flg;		/* combo flags */
146 	u_char		c_refcnt;	/* # higher levels that point to us */
147 };
148 
149 /* flag values for s_flg */
150 #define C_LOOP		0x01		/* link[1] intersects previous frame */
151 
152 struct	elist {
153 	struct elist	*e_next;	/* list of combos */
154 	struct combostr	*e_combo;	/* the combo */
155 	struct combostr	*e_frame;	/* the 1st level combo that connects */
156 };
157 
158 /*
159  * One spot structure for each location on the board.
160  * A frame consists of the combination for the current spot plus the five spots
161  * 0: right, 1: right & down, 2: down, 3: down & left.
162  */
163 struct	spotstr {
164 	short		s_occ;		/* color of occupant */
165 	short		s_wval;		/* weighted value */
166 	int		s_flg;		/* flags for graph walks */
167 	union combo	s_fval[2][4];	/* combo value for [color][frame] */
168 	union combo	s_combo[2];	/* minimum combo value for BLK & WHT */
169 	u_char		s_level[2];	/* number of frames in the min combo */
170 	u_char		s_nforce[2];	/* number of <1,x> combos */
171 	struct combostr	*s_frame[4];	/* level 1 combo for frame[dir] */
172 	struct elist	*s_empty[2];	/* level n combo for [color] */
173 };
174 
175 /* flag values for s_flg */
176 #define CFLAG		0x000001	/* frame is part of a combo */
177 #define CFLAGALL	0x00000F	/* all frame directions marked */
178 #define IFLAG		0x000010	/* legal intersection point */
179 #define IFLAGALL	0x0000F0	/* any intersection points? */
180 #define FFLAG		0x000100	/* frame is part of a <1,x> combo */
181 #define FFLAGALL	0x000F00	/* all force frames */
182 #define MFLAG		0x001000	/* frame has already been seen */
183 #define MFLAGALL	0x00F000	/* all frames seen */
184 #define BFLAG		0x010000	/* frame intersects border or dead */
185 #define BFLAGALL	0x0F0000	/* all frames dead */
186 
187 extern	char	*letters;
188 extern	char	*color[];
189 extern	char	fmtbuf[];
190 
191 extern	int     dd[4];
192 extern	struct	spotstr	board[BAREA];		/* info for board */
193 extern	struct	combostr frames[FAREA];		/* storage for all frames */
194 extern	struct	combostr *sortframes[2];	/* sorted, non-empty frames */
195 extern	char	overlap[FAREA * FAREA];		/* frame [a][b] overlap */
196 extern	short	intersect[FAREA * FAREA];	/* frame [a][b] intersection */
197 extern	int	movelog[BSZ * BSZ];		/* history of moves */
198 extern	int	movenum;
199 extern	int	debug;
200 
201 extern	char    *copy();
202 extern	char    *stoc();
203 extern	char    *tail();
204 
205 #define ASSERT(x)
206