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