Home | History | Annotate | Line # | Download | only in gomoku
bdinit.c revision 1.26
      1  1.26    rillig /*	$NetBSD: bdinit.c,v 1.26 2022/05/28 18:55:16 rillig Exp $	*/
      2   1.3       cgd 
      3   1.1       tls /*
      4   1.1       tls  * Copyright (c) 1994
      5   1.1       tls  *	The Regents of the University of California.  All rights reserved.
      6   1.1       tls  *
      7   1.1       tls  * This code is derived from software contributed to Berkeley by
      8   1.1       tls  * Ralph Campbell.
      9   1.1       tls  *
     10   1.1       tls  * Redistribution and use in source and binary forms, with or without
     11   1.1       tls  * modification, are permitted provided that the following conditions
     12   1.1       tls  * are met:
     13   1.1       tls  * 1. Redistributions of source code must retain the above copyright
     14   1.1       tls  *    notice, this list of conditions and the following disclaimer.
     15   1.1       tls  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       tls  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       tls  *    documentation and/or other materials provided with the distribution.
     18   1.5       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       tls  *    may be used to endorse or promote products derived from this software
     20   1.1       tls  *    without specific prior written permission.
     21   1.1       tls  *
     22   1.1       tls  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       tls  * SUCH DAMAGE.
     33   1.1       tls  */
     34   1.1       tls 
     35   1.4     lukem #include <sys/cdefs.h>
     36  1.18    rillig /*	from: @(#)bdinit.c	8.2 (Berkeley) 5/3/95	*/
     37  1.26    rillig __RCSID("$NetBSD: bdinit.c,v 1.26 2022/05/28 18:55:16 rillig Exp $");
     38   1.1       tls 
     39   1.1       tls #include <string.h>
     40   1.1       tls #include "gomoku.h"
     41   1.1       tls 
     42   1.8  dholland static void init_overlap(void);
     43   1.8  dholland 
     44  1.25    rillig static void
     45  1.25    rillig init_spot_flags_and_fval(struct spotstr *sp, int i, int j)
     46  1.25    rillig {
     47  1.25    rillig 
     48  1.25    rillig 	sp->s_flags = 0;
     49  1.25    rillig 	if (j < 5) {
     50  1.25    rillig 		/* directions 1, 2, 3 are blocked */
     51  1.25    rillig 		sp->s_flags |= (BFLAG << 1) | (BFLAG << 2) |
     52  1.25    rillig 		    (BFLAG << 3);
     53  1.25    rillig 		sp->s_fval[BLACK][1].s = 0x600;
     54  1.25    rillig 		sp->s_fval[BLACK][2].s = 0x600;
     55  1.25    rillig 		sp->s_fval[BLACK][3].s = 0x600;
     56  1.25    rillig 		sp->s_fval[WHITE][1].s = 0x600;
     57  1.25    rillig 		sp->s_fval[WHITE][2].s = 0x600;
     58  1.25    rillig 		sp->s_fval[WHITE][3].s = 0x600;
     59  1.25    rillig 	} else if (j == 5) {
     60  1.25    rillig 		/* five spaces, blocked on one side */
     61  1.25    rillig 		sp->s_fval[BLACK][1].s = 0x500;
     62  1.25    rillig 		sp->s_fval[BLACK][2].s = 0x500;
     63  1.25    rillig 		sp->s_fval[BLACK][3].s = 0x500;
     64  1.25    rillig 		sp->s_fval[WHITE][1].s = 0x500;
     65  1.25    rillig 		sp->s_fval[WHITE][2].s = 0x500;
     66  1.25    rillig 		sp->s_fval[WHITE][3].s = 0x500;
     67  1.25    rillig 	} else {
     68  1.25    rillig 		/* six spaces, not blocked */
     69  1.25    rillig 		sp->s_fval[BLACK][1].s = 0x401;
     70  1.25    rillig 		sp->s_fval[BLACK][2].s = 0x401;
     71  1.25    rillig 		sp->s_fval[BLACK][3].s = 0x401;
     72  1.25    rillig 		sp->s_fval[WHITE][1].s = 0x401;
     73  1.25    rillig 		sp->s_fval[WHITE][2].s = 0x401;
     74  1.25    rillig 		sp->s_fval[WHITE][3].s = 0x401;
     75  1.25    rillig 	}
     76  1.25    rillig 	if (i > (BSZ - 4)) {
     77  1.25    rillig 		/* directions 0, 1 are blocked */
     78  1.25    rillig 		sp->s_flags |= BFLAG | (BFLAG << 1);
     79  1.25    rillig 		sp->s_fval[BLACK][0].s = 0x600;
     80  1.25    rillig 		sp->s_fval[BLACK][1].s = 0x600;
     81  1.25    rillig 		sp->s_fval[WHITE][0].s = 0x600;
     82  1.25    rillig 		sp->s_fval[WHITE][1].s = 0x600;
     83  1.25    rillig 	} else if (i == (BSZ - 4)) {
     84  1.25    rillig 		sp->s_fval[BLACK][0].s = 0x500;
     85  1.25    rillig 		sp->s_fval[WHITE][0].s = 0x500;
     86  1.25    rillig 		/* if direction 1 is not blocked */
     87  1.25    rillig 		if ((sp->s_flags & (BFLAG << 1)) == 0) {
     88  1.25    rillig 			sp->s_fval[BLACK][1].s = 0x500;
     89  1.25    rillig 			sp->s_fval[WHITE][1].s = 0x500;
     90  1.25    rillig 		}
     91  1.25    rillig 	} else {
     92  1.25    rillig 		sp->s_fval[BLACK][0].s = 0x401;
     93  1.25    rillig 		sp->s_fval[WHITE][0].s = 0x401;
     94  1.25    rillig 		if (i < 5) {
     95  1.25    rillig 			/* direction 3 is blocked */
     96  1.25    rillig 			sp->s_flags |= (BFLAG << 3);
     97  1.25    rillig 			sp->s_fval[BLACK][3].s = 0x600;
     98  1.25    rillig 			sp->s_fval[WHITE][3].s = 0x600;
     99  1.25    rillig 		} else if (i == 5 &&
    100  1.25    rillig 		    (sp->s_flags & (BFLAG << 3)) == 0) {
    101  1.25    rillig 			sp->s_fval[BLACK][3].s = 0x500;
    102  1.25    rillig 			sp->s_fval[WHITE][3].s = 0x500;
    103  1.25    rillig 		}
    104  1.25    rillig 	}
    105  1.25    rillig }
    106  1.25    rillig 
    107  1.25    rillig /* Allocate one of the pre-allocated frames for each non-blocked frame. */
    108  1.25    rillig static void
    109  1.25    rillig init_spot_frame(struct spotstr *sp, struct combostr **cbpp)
    110  1.25    rillig {
    111  1.25    rillig 
    112  1.25    rillig 	for (int r = 4; --r >= 0; ) {
    113  1.25    rillig 		if ((sp->s_flags & (BFLAG << r)) != 0)
    114  1.25    rillig 			continue;
    115  1.25    rillig 
    116  1.25    rillig 		struct combostr *cbp = (*cbpp)++;
    117  1.25    rillig 		cbp->c_combo.s = sp->s_fval[BLACK][r].s;
    118  1.25    rillig 		cbp->c_vertex = (u_short)(sp - board);
    119  1.25    rillig 		cbp->c_nframes = 1;
    120  1.25    rillig 		cbp->c_dir = r;
    121  1.25    rillig 		sp->s_frame[r] = cbp;
    122  1.25    rillig 	}
    123  1.25    rillig }
    124  1.25    rillig 
    125   1.4     lukem void
    126  1.25    rillig init_board(void)
    127   1.1       tls {
    128   1.1       tls 
    129  1.23    rillig 	game.nmoves = 0;
    130  1.24    rillig 	game.winning_spot = 0;
    131   1.1       tls 
    132  1.25    rillig 	struct spotstr *sp = board;
    133  1.17    rillig 	for (int i = 0; i < 1 + BSZ + 1; i++, sp++) {
    134  1.25    rillig 		sp->s_occ = BORDER;	/* bottom border and corners */
    135   1.7  dholland 		sp->s_flags = BFLAGALL;
    136   1.1       tls 	}
    137   1.1       tls 
    138  1.25    rillig 	/* fill the playing area of the board with EMPTY spots */
    139  1.25    rillig 	struct combostr *cbp = frames;
    140   1.1       tls 	memset(frames, 0, sizeof(frames));
    141  1.25    rillig 	for (int row = 1; row <= BSZ; row++, sp++) {
    142  1.25    rillig 		for (int col = 1; col <= BSZ; col++, sp++) {
    143   1.1       tls 			sp->s_occ = EMPTY;
    144   1.1       tls 			sp->s_wval = 0;
    145  1.25    rillig 			init_spot_flags_and_fval(sp, col, row);
    146  1.25    rillig 			init_spot_frame(sp, &cbp);
    147   1.1       tls 		}
    148  1.25    rillig 		sp->s_occ = BORDER;	/* combined left and right border */
    149   1.7  dholland 		sp->s_flags = BFLAGALL;
    150   1.1       tls 	}
    151   1.1       tls 
    152  1.17    rillig 	for (int i = 0; i < BSZ + 1; i++, sp++) {
    153  1.25    rillig 		sp->s_occ = BORDER;	/* top border and top-right corner */
    154   1.7  dholland 		sp->s_flags = BFLAGALL;
    155   1.1       tls 	}
    156   1.1       tls 
    157  1.19    rillig 	sortframes[BLACK] = NULL;
    158  1.19    rillig 	sortframes[WHITE] = NULL;
    159  1.25    rillig 
    160   1.1       tls 	init_overlap();
    161   1.1       tls }
    162   1.1       tls 
    163  1.26    rillig /*-
    164  1.26    rillig  * ra	direction of frame A
    165  1.26    rillig  * ia	index of the spot in frame A (0 to 5)
    166  1.26    rillig  * rb	direction of frame B
    167  1.26    rillig  * ib	index of the spot in frame B (0 to 5)
    168  1.26    rillig  */
    169  1.26    rillig static u_char
    170  1.26    rillig adjust_overlap(u_char ov, int ra, int ia, int rb, int ib, int mask)
    171  1.26    rillig {
    172  1.26    rillig 	ov |= (ib == 5) ? mask & 0xA : mask;
    173  1.26    rillig 	if (rb != ra)
    174  1.26    rillig 		return ov;
    175  1.26    rillig 
    176  1.26    rillig 	/* compute the multiple spot overlap values */
    177  1.26    rillig 	switch (ia) {
    178  1.26    rillig 	case 0:
    179  1.26    rillig 		if (ib == 4)
    180  1.26    rillig 			ov |= 0xA0;
    181  1.26    rillig 		else if (ib != 5)
    182  1.26    rillig 			ov |= 0xF0;
    183  1.26    rillig 		break;
    184  1.26    rillig 	case 1:
    185  1.26    rillig 		if (ib == 5)
    186  1.26    rillig 			ov |= 0xA0;
    187  1.26    rillig 		else
    188  1.26    rillig 			ov |= 0xF0;
    189  1.26    rillig 		break;
    190  1.26    rillig 	case 4:
    191  1.26    rillig 		if (ib == 0)
    192  1.26    rillig 			ov |= 0xC0;
    193  1.26    rillig 		else
    194  1.26    rillig 			ov |= 0xF0;
    195  1.26    rillig 		break;
    196  1.26    rillig 	case 5:
    197  1.26    rillig 		if (ib == 1)
    198  1.26    rillig 			ov |= 0xC0;
    199  1.26    rillig 		else if (ib != 0)
    200  1.26    rillig 			ov |= 0xF0;
    201  1.26    rillig 		break;
    202  1.26    rillig 	default:
    203  1.26    rillig 		ov |= 0xF0;
    204  1.26    rillig 	}
    205  1.26    rillig 
    206  1.26    rillig 	return ov;
    207  1.26    rillig }
    208  1.26    rillig 
    209   1.1       tls /*
    210   1.1       tls  * Initialize the overlap array.
    211   1.1       tls  * Each entry in the array is a bit mask with eight bits corresponding
    212   1.1       tls  * to whether frame B overlaps frame A (as indexed by overlap[A * FAREA + B]).
    213  1.16    rillig  * The eight bits correspond to whether A and B are open-ended (length 6) or
    214   1.1       tls  * closed (length 5).
    215   1.1       tls  *	0	A closed and B closed
    216   1.1       tls  *	1	A closed and B open
    217   1.1       tls  *	2	A open and B closed
    218   1.1       tls  *	3	A open and B open
    219   1.1       tls  *	4	A closed and B closed and overlaps in more than one spot
    220   1.1       tls  *	5	A closed and B open and overlaps in more than one spot
    221   1.1       tls  *	6	A open and B closed and overlaps in more than one spot
    222   1.1       tls  *	7	A open and B open and overlaps in more than one spot
    223   1.1       tls  * As pieces are played, it can make frames not overlap if there are no
    224   1.1       tls  * common open spaces shared between the two frames.
    225   1.1       tls  */
    226   1.8  dholland static void
    227   1.6  dholland init_overlap(void)
    228   1.1       tls {
    229   1.1       tls 
    230   1.1       tls 	memset(overlap, 0, sizeof(overlap));
    231   1.1       tls 	memset(intersect, 0, sizeof(intersect));
    232  1.16    rillig 
    233  1.26    rillig 	/*-
    234  1.26    rillig 	 * Variables for frames A and B:
    235  1.26    rillig 	 *
    236  1.26    rillig 	 * fi	index of the frame in the global 'frames'
    237  1.26    rillig 	 * r	direction: 0 = right, 1 = down right, 2 = down, 3 = down left
    238  1.26    rillig 	 * d	direction delta, difference between adjacent spot indexes
    239  1.26    rillig 	 * si	index of the spot in the frame, 0 to 5
    240  1.26    rillig 	 * sp	data of the spot at index i
    241  1.26    rillig 	 */
    242  1.26    rillig 
    243  1.26    rillig 	for (unsigned fia = FAREA; fia-- > 0; ) {
    244  1.26    rillig 	    struct combostr *fa = &frames[fia];
    245  1.26    rillig 	    int vertex = fa->c_vertex;
    246  1.26    rillig 	    struct spotstr *spa = &board[vertex];
    247  1.26    rillig 	    u_char ra = fa->c_dir;
    248  1.26    rillig 	    int da = dd[ra];
    249  1.26    rillig 
    250   1.1       tls 	    /*
    251  1.26    rillig 	     * len = 5 if closed, 6 if open.
    252  1.26    rillig 	     * At this point, Black and White have the same values.
    253   1.1       tls 	     */
    254  1.26    rillig 	    int len = 5 + spa->s_fval[BLACK][ra].cv_win;
    255  1.26    rillig 
    256  1.26    rillig 	    for (int sia = 0; sia < len; sia++, spa += da, vertex += da) {
    257   1.1       tls 		/* the sixth spot in frame A only overlaps if it is open */
    258  1.26    rillig 		int mask = (sia == 5) ? 0xC : 0xF;
    259  1.26    rillig 
    260  1.26    rillig 		for (int rb = 4; --rb >= 0; ) {
    261  1.26    rillig 		    struct spotstr *spb = spa;
    262  1.26    rillig 		    int db = dd[rb];
    263  1.26    rillig 
    264  1.26    rillig 		    /* for each frame that intersects at spot spa */
    265  1.26    rillig 		    for (int sib = 0; sib < 6; sib++, spb -= db) {
    266  1.26    rillig 			if (spb->s_occ == BORDER)
    267   1.1       tls 			    break;
    268  1.26    rillig 			if ((spb->s_flags & BFLAG << rb) != 0)
    269   1.1       tls 			    continue;
    270  1.26    rillig 
    271  1.26    rillig 			int fib = (int)(spb->s_frame[rb] - frames);
    272  1.26    rillig 			intersect[fia * FAREA + fib] = (short)vertex;
    273  1.26    rillig 			u_char *op = &overlap[fia * FAREA + fib];
    274  1.26    rillig 			*op = adjust_overlap(*op, ra, sia, rb, sib, mask);
    275   1.1       tls 		    }
    276   1.1       tls 		}
    277   1.1       tls 	    }
    278   1.1       tls 	}
    279   1.1       tls }
    280