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