Home | History | Annotate | Line # | Download | only in gomoku
bdinit.c revision 1.30
      1 /*	$NetBSD: bdinit.c,v 1.30 2022/05/29 10:37:21 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.30 2022/05/29 10:37:21 rillig Exp $");
     38 
     39 #include <string.h>
     40 #include "gomoku.h"
     41 
     42 static void init_overlap(void);
     43 
     44 static void
     45 init_spot_flags_and_fval(struct spotstr *sp, int i, int j)
     46 {
     47 
     48 	sp->s_flags = 0;
     49 	if (j < 5) {
     50 		/* directions 1, 2, 3 are blocked */
     51 		sp->s_flags |= (BFLAG << 1) | (BFLAG << 2) |
     52 		    (BFLAG << 3);
     53 		sp->s_fval[BLACK][1].s = 0x600;
     54 		sp->s_fval[BLACK][2].s = 0x600;
     55 		sp->s_fval[BLACK][3].s = 0x600;
     56 		sp->s_fval[WHITE][1].s = 0x600;
     57 		sp->s_fval[WHITE][2].s = 0x600;
     58 		sp->s_fval[WHITE][3].s = 0x600;
     59 	} else if (j == 5) {
     60 		/* five spaces, blocked on one side */
     61 		sp->s_fval[BLACK][1].s = 0x500;
     62 		sp->s_fval[BLACK][2].s = 0x500;
     63 		sp->s_fval[BLACK][3].s = 0x500;
     64 		sp->s_fval[WHITE][1].s = 0x500;
     65 		sp->s_fval[WHITE][2].s = 0x500;
     66 		sp->s_fval[WHITE][3].s = 0x500;
     67 	} else {
     68 		/* six spaces, not blocked */
     69 		sp->s_fval[BLACK][1].s = 0x401;
     70 		sp->s_fval[BLACK][2].s = 0x401;
     71 		sp->s_fval[BLACK][3].s = 0x401;
     72 		sp->s_fval[WHITE][1].s = 0x401;
     73 		sp->s_fval[WHITE][2].s = 0x401;
     74 		sp->s_fval[WHITE][3].s = 0x401;
     75 	}
     76 	if (i > (BSZ - 4)) {
     77 		/* directions 0, 1 are blocked */
     78 		sp->s_flags |= BFLAG | (BFLAG << 1);
     79 		sp->s_fval[BLACK][0].s = 0x600;
     80 		sp->s_fval[BLACK][1].s = 0x600;
     81 		sp->s_fval[WHITE][0].s = 0x600;
     82 		sp->s_fval[WHITE][1].s = 0x600;
     83 	} else if (i == (BSZ - 4)) {
     84 		sp->s_fval[BLACK][0].s = 0x500;
     85 		sp->s_fval[WHITE][0].s = 0x500;
     86 		/* if direction 1 is not blocked */
     87 		if ((sp->s_flags & (BFLAG << 1)) == 0) {
     88 			sp->s_fval[BLACK][1].s = 0x500;
     89 			sp->s_fval[WHITE][1].s = 0x500;
     90 		}
     91 	} else {
     92 		sp->s_fval[BLACK][0].s = 0x401;
     93 		sp->s_fval[WHITE][0].s = 0x401;
     94 		if (i < 5) {
     95 			/* direction 3 is blocked */
     96 			sp->s_flags |= (BFLAG << 3);
     97 			sp->s_fval[BLACK][3].s = 0x600;
     98 			sp->s_fval[WHITE][3].s = 0x600;
     99 		} else if (i == 5 &&
    100 		    (sp->s_flags & (BFLAG << 3)) == 0) {
    101 			sp->s_fval[BLACK][3].s = 0x500;
    102 			sp->s_fval[WHITE][3].s = 0x500;
    103 		}
    104 	}
    105 }
    106 
    107 /* Allocate one of the pre-allocated frames for each non-blocked frame. */
    108 static void
    109 init_spot_frame(struct spotstr *sp, frame_index *fip)
    110 {
    111 
    112 	for (int r = 4; --r >= 0; ) {
    113 		if ((sp->s_flags & (BFLAG << r)) != 0)
    114 			continue;
    115 
    116 		frame_index fi = (*fip)++;
    117 		sp->s_frame[r] = fi;
    118 
    119 		struct combostr *cbp = &frames[fi];
    120 		cbp->c_combo.s = sp->s_fval[BLACK][r].s;
    121 		cbp->c_vertex = (u_short)(sp - board);
    122 		cbp->c_nframes = 1;
    123 		cbp->c_dir = r;
    124 	}
    125 }
    126 
    127 void
    128 init_board(void)
    129 {
    130 
    131 	game.nmoves = 0;
    132 	game.winning_spot = 0;
    133 
    134 	struct spotstr *sp = board;
    135 	for (int i = 0; i < 1 + BSZ + 1; i++, sp++) {
    136 		sp->s_occ = BORDER;	/* bottom border and corners */
    137 		sp->s_flags = BFLAGALL;
    138 	}
    139 
    140 	/* fill the playing area of the board with EMPTY spots */
    141 	frame_index fi = 0;
    142 	memset(frames, 0, sizeof(frames));
    143 	for (int row = 1; row <= BSZ; row++, sp++) {
    144 		for (int col = 1; col <= BSZ; col++, sp++) {
    145 			sp->s_occ = EMPTY;
    146 			sp->s_wval = 0;
    147 			init_spot_flags_and_fval(sp, col, row);
    148 			init_spot_frame(sp, &fi);
    149 		}
    150 		sp->s_occ = BORDER;	/* combined left and right border */
    151 		sp->s_flags = BFLAGALL;
    152 	}
    153 
    154 	for (int i = 0; i < BSZ + 1; i++, sp++) {
    155 		sp->s_occ = BORDER;	/* top border and top-right corner */
    156 		sp->s_flags = BFLAGALL;
    157 	}
    158 
    159 	sortframes[BLACK] = NULL;
    160 	sortframes[WHITE] = NULL;
    161 
    162 	init_overlap();
    163 }
    164 
    165 /*
    166  * Variable names for frames A and B:
    167  *
    168  * fi	index of the frame in the global 'frames'
    169  * r	direction: 0 = right, 1 = down right, 2 = down, 3 = down left
    170  * d	direction delta, difference between adjacent spot indexes
    171  * si	index of the spot in the frame, 0 to 5
    172  */
    173 
    174 /*
    175  * Each entry in the overlap array is a bit mask with eight bits corresponding
    176  * to whether frame B overlaps frame A (as indexed by overlap[A * FAREA + B]).
    177  *
    178  * The eight bits correspond to whether A and B are open-ended (length 6) or
    179  * closed (length 5).
    180  *
    181  *	0	A closed and B closed
    182  *	1	A closed and B open
    183  *	2	A open and B closed
    184  *	3	A open and B open
    185  *	4	A closed and B closed and overlaps in more than one spot
    186  *	5	A closed and B open and overlaps in more than one spot
    187  *	6	A open and B closed and overlaps in more than one spot
    188  *	7	A open and B open and overlaps in more than one spot
    189  *
    190  * As pieces are played during the game, frames that no longer share an empty
    191  * spot will be removed from the overlap array by setting the entry to 0.
    192  */
    193 static u_char
    194 adjust_overlap(u_char ov, int ra, int sia, int rb, int sib, int mask)
    195 {
    196 	ov |= (sib == 5) ? mask & 0xA : mask;
    197 	if (rb != ra)
    198 		return ov;
    199 
    200 	/* compute the multiple spot overlap values */
    201 	switch (sia) {
    202 	case 0:
    203 		if (sib == 4)
    204 			ov |= 0xA0;
    205 		else if (sib != 5)
    206 			ov |= 0xF0;
    207 		break;
    208 	case 1:
    209 		if (sib == 5)
    210 			ov |= 0xA0;
    211 		else
    212 			ov |= 0xF0;
    213 		break;
    214 	case 4:
    215 		if (sib == 0)
    216 			ov |= 0xC0;
    217 		else
    218 			ov |= 0xF0;
    219 		break;
    220 	case 5:
    221 		if (sib == 1)
    222 			ov |= 0xC0;
    223 		else if (sib != 0)
    224 			ov |= 0xF0;
    225 		break;
    226 	default:
    227 		ov |= 0xF0;
    228 	}
    229 
    230 	return ov;
    231 }
    232 
    233 /*
    234  * Given a single spot 's' of frame A, update the overlap information for
    235  * each frame B that overlaps frame A in that spot.
    236  */
    237 static void
    238 init_overlap_frame(int fia, int ra, int sia, spot_index s, int mask)
    239 {
    240 
    241 	for (int rb = 4; --rb >= 0;) {
    242 		int db = dd[rb];
    243 
    244 		for (int sib = 0; sib < 6; sib++) {
    245 			/* spb0 is the spot where frame B starts. */
    246 			const struct spotstr *spb0 = &board[s - sib * db];
    247 			if (spb0->s_occ == BORDER)
    248 				break;
    249 			if ((spb0->s_flags & BFLAG << rb) != 0)
    250 				continue;
    251 
    252 			frame_index fib = spb0->s_frame[rb];
    253 			intersect[fia * FAREA + fib] = s;
    254 			u_char *op = &overlap[fia * FAREA + fib];
    255 			*op = adjust_overlap(*op, ra, sia, rb, sib, mask);
    256 		}
    257 	}
    258 }
    259 
    260 static void
    261 init_overlap(void)
    262 {
    263 
    264 	memset(overlap, 0, sizeof(overlap));
    265 	memset(intersect, 0, sizeof(intersect));
    266 
    267 	for (int fia = FAREA; fia-- > 0;) {
    268 		const struct combostr *fa = &frames[fia];
    269 		spot_index s = fa->c_vertex;
    270 		u_char ra = fa->c_dir;
    271 		int da = dd[ra];
    272 
    273 		/*
    274 		 * len = 5 if closed, 6 if open. At this early stage, Black
    275 		 * and White have the same value for cv_win.
    276 		 */
    277 		int len = 5 + board[s].s_fval[BLACK][ra].cv_win;
    278 
    279 		for (int sia = 0; sia < len; sia++) {
    280 			/* spot[5] in frame A only overlaps if it is open */
    281 			int mask = (sia == 5) ? 0xC : 0xF;
    282 
    283 			init_overlap_frame(fia, ra, sia, s + sia * da, mask);
    284 		}
    285 	}
    286 }
    287