Home | History | Annotate | Line # | Download | only in gomoku
bdinit.c revision 1.16
      1 /*	$NetBSD: bdinit.c,v 1.16 2022/05/18 22:35:13 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 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "from: @(#)bdinit.c	8.2 (Berkeley) 5/3/95";
     39 #else
     40 __RCSID("$NetBSD: bdinit.c,v 1.16 2022/05/18 22:35:13 rillig Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <string.h>
     45 #include "gomoku.h"
     46 
     47 static void init_overlap(void);
     48 
     49 void
     50 bdinit(struct spotstr *bp)
     51 {
     52 	struct spotstr *sp;
     53 	struct combostr *cbp;
     54 
     55 	movenum = 1;
     56 
     57 	/* mark the borders as such */
     58 	sp = bp;
     59 	for (int i = 1 + BSZ + 1; --i >= 0; sp++) {
     60 		sp->s_occ = BORDER;			/* top border */
     61 		sp->s_flags = BFLAGALL;
     62 	}
     63 
     64 	/* fill entire board with EMPTY spots */
     65 	memset(frames, 0, sizeof(frames));
     66 	cbp = frames;
     67 	for (int j = 0; ++j < BSZ + 1; sp++) {		/* for each row */
     68 		for (int i = 0; ++i < BSZ + 1; sp++) {	/* for each column */
     69 			sp->s_occ = EMPTY;
     70 			sp->s_flags = 0;
     71 			sp->s_wval = 0;
     72 			if (j < 5) {
     73 				/* directions 1, 2, 3 are blocked */
     74 				sp->s_flags |= (BFLAG << 1) | (BFLAG << 2) |
     75 				    (BFLAG << 3);
     76 				sp->s_fval[BLACK][1].s = MAXCOMBO;
     77 				sp->s_fval[BLACK][2].s = MAXCOMBO;
     78 				sp->s_fval[BLACK][3].s = MAXCOMBO;
     79 				sp->s_fval[WHITE][1].s = MAXCOMBO;
     80 				sp->s_fval[WHITE][2].s = MAXCOMBO;
     81 				sp->s_fval[WHITE][3].s = MAXCOMBO;
     82 			} else if (j == 5) {
     83 				/* five spaces, blocked on one side */
     84 				sp->s_fval[BLACK][1].s = 0x500;
     85 				sp->s_fval[BLACK][2].s = 0x500;
     86 				sp->s_fval[BLACK][3].s = 0x500;
     87 				sp->s_fval[WHITE][1].s = 0x500;
     88 				sp->s_fval[WHITE][2].s = 0x500;
     89 				sp->s_fval[WHITE][3].s = 0x500;
     90 			} else {
     91 				/* six spaces, not blocked */
     92 				sp->s_fval[BLACK][1].s = 0x401;
     93 				sp->s_fval[BLACK][2].s = 0x401;
     94 				sp->s_fval[BLACK][3].s = 0x401;
     95 				sp->s_fval[WHITE][1].s = 0x401;
     96 				sp->s_fval[WHITE][2].s = 0x401;
     97 				sp->s_fval[WHITE][3].s = 0x401;
     98 			}
     99 			if (i > (BSZ - 4)) {
    100 				/* directions 0, 1 are blocked */
    101 				sp->s_flags |= BFLAG | (BFLAG << 1);
    102 				sp->s_fval[BLACK][0].s = MAXCOMBO;
    103 				sp->s_fval[BLACK][1].s = MAXCOMBO;
    104 				sp->s_fval[WHITE][0].s = MAXCOMBO;
    105 				sp->s_fval[WHITE][1].s = MAXCOMBO;
    106 			} else if (i == (BSZ - 4)) {
    107 				sp->s_fval[BLACK][0].s = 0x500;
    108 				sp->s_fval[WHITE][0].s = 0x500;
    109 				/* if direction 1 is not blocked */
    110 				if ((sp->s_flags & (BFLAG << 1)) == 0) {
    111 					sp->s_fval[BLACK][1].s = 0x500;
    112 					sp->s_fval[WHITE][1].s = 0x500;
    113 				}
    114 			} else {
    115 				sp->s_fval[BLACK][0].s = 0x401;
    116 				sp->s_fval[WHITE][0].s = 0x401;
    117 				if (i < 5) {
    118 					/* direction 3 is blocked */
    119 					sp->s_flags |= (BFLAG << 3);
    120 					sp->s_fval[BLACK][3].s = MAXCOMBO;
    121 					sp->s_fval[WHITE][3].s = MAXCOMBO;
    122 				} else if (i == 5 &&
    123 				    (sp->s_flags & (BFLAG << 3)) == 0) {
    124 					sp->s_fval[BLACK][3].s = 0x500;
    125 					sp->s_fval[WHITE][3].s = 0x500;
    126 				}
    127 			}
    128 			/*
    129 			 * Allocate a frame structure for non-blocked frames.
    130 			 */
    131 			for (int r = 4; --r >= 0; ) {
    132 				if ((sp->s_flags & (BFLAG << r)) != 0)
    133 					continue;
    134 				cbp->c_combo.s = sp->s_fval[BLACK][r].s;
    135 				cbp->c_vertex = (u_short)(sp - board);
    136 				cbp->c_nframes = 1;
    137 				cbp->c_dir = r;
    138 				sp->s_frame[r] = cbp;
    139 				cbp++;
    140 			}
    141 		}
    142 		sp->s_occ = BORDER;		/* left & right border */
    143 		sp->s_flags = BFLAGALL;
    144 	}
    145 
    146 	/* mark the borders as such */
    147 	for (int i = BSZ + 1; --i >= 0; sp++) {
    148 		sp->s_occ = BORDER;			/* bottom border */
    149 		sp->s_flags = BFLAGALL;
    150 	}
    151 
    152 	sortframes[BLACK] = (struct combostr *)0;
    153 	sortframes[WHITE] = (struct combostr *)0;
    154 	init_overlap();
    155 }
    156 
    157 /*
    158  * Initialize the overlap array.
    159  * Each entry in the array is a bit mask with eight bits corresponding
    160  * to whether frame B overlaps frame A (as indexed by overlap[A * FAREA + B]).
    161  * The eight bits correspond to whether A and B are open-ended (length 6) or
    162  * closed (length 5).
    163  *	0	A closed and B closed
    164  *	1	A closed and B open
    165  *	2	A open and B closed
    166  *	3	A open and B open
    167  *	4	A closed and B closed and overlaps in more than one spot
    168  *	5	A closed and B open and overlaps in more than one spot
    169  *	6	A open and B closed and overlaps in more than one spot
    170  *	7	A open and B open and overlaps in more than one spot
    171  * As pieces are played, it can make frames not overlap if there are no
    172  * common open spaces shared between the two frames.
    173  */
    174 static void
    175 init_overlap(void)
    176 {
    177 
    178 	memset(overlap, 0, sizeof(overlap));
    179 	memset(intersect, 0, sizeof(intersect));
    180 	u_char *op = &overlap[FAREA * FAREA];
    181 	short *ip = &intersect[FAREA * FAREA];
    182 
    183 	for (unsigned fi = FAREA; fi-- > 0; ) {	/* each frame */
    184 	    struct combostr *cbp = &frames[fi];
    185 	    op -= FAREA;
    186 	    ip -= FAREA;
    187 	    int vertex = cbp->c_vertex;
    188 	    struct spotstr *sp1 = &board[vertex];
    189 	    int d1 = dd[cbp->c_dir];
    190 	    /*
    191 	     * s = 5 if closed, 6 if open.
    192 	     * At this point black & white are the same.
    193 	     */
    194 	    int s = 5 + sp1->s_fval[BLACK][cbp->c_dir].c.b;
    195 	    /* for each spot in frame A */
    196 	    for (int i = 0; i < s; i++, sp1 += d1, vertex += d1) {
    197 		/* the sixth spot in frame A only overlaps if it is open */
    198 		int mask = (i == 5) ? 0xC : 0xF;
    199 		/* for each direction */
    200 		for (int r = 4; --r >= 0; ) {
    201 		    struct spotstr *sp2 = sp1;
    202 		    int d2 = dd[r];
    203 		    /* for each frame that intersects at spot sp1 */
    204 		    for (int f = 0; f < 6; f++, sp2 -= d2) {
    205 			if (sp2->s_occ == BORDER)
    206 			    break;
    207 			if ((sp2->s_flags & BFLAG << r) != 0)
    208 			    continue;
    209 			int n = (int)(sp2->s_frame[r] - frames);
    210 			ip[n] = (short)vertex;
    211 			op[n] |= (f == 5) ? mask & 0xA : mask;
    212 			if (r == cbp->c_dir) {
    213 			    /* compute the multiple spot overlap values */
    214 			    switch (i) {
    215 			    case 0:	/* sp1 is the first spot in A */
    216 				if (f == 4)
    217 				    op[n] |= 0xA0;
    218 				else if (f != 5)
    219 				    op[n] |= 0xF0;
    220 				break;
    221 			    case 1:	/* sp1 is the second spot in A */
    222 				if (f == 5)
    223 				    op[n] |= 0xA0;
    224 				else
    225 				    op[n] |= 0xF0;
    226 				break;
    227 			    case 4:	/* sp1 is the penultimate spot in A */
    228 				if (f == 0)
    229 				    op[n] |= 0xC0;
    230 				else
    231 				    op[n] |= 0xF0;
    232 				break;
    233 			    case 5:	/* sp1 is the last spot in A */
    234 				if (f == 1)
    235 				    op[n] |= 0xC0;
    236 				else if (f != 0)
    237 				    op[n] |= 0xF0;
    238 				break;
    239 			    default:
    240 				op[n] |= 0xF0;
    241 			    }
    242 			}
    243 		    }
    244 		}
    245 	    }
    246 	}
    247 }
    248