Sun May 15 22:18:36 2022 UTC ()
gomoku: remove column macros

Of the 19 macros, only 3 were actually used.  Replace their uses with
expressions based on the board size.  It's a small step to making the
board size adjustable.  There are still other places using hard-coded
numbers.

No binary change.


(rillig)
diff -r1.22 -r1.23 src/games/gomoku/gomoku.h
diff -r1.25 -r1.26 src/games/gomoku/pickmove.c

cvs diff -r1.22 -r1.23 src/games/gomoku/gomoku.h (expand / switch to unified diff)

--- src/games/gomoku/gomoku.h 2022/05/15 22:00:11 1.22
+++ src/games/gomoku/gomoku.h 2022/05/15 22:18:36 1.23
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: gomoku.h,v 1.22 2022/05/15 22:00:11 rillig Exp $ */ 1/* $NetBSD: gomoku.h,v 1.23 2022/05/15 22:18:36 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994 4 * Copyright (c) 1994
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Ralph Campbell. 8 * Ralph Campbell.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -65,46 +65,26 @@ @@ -65,46 +65,26 @@
65#define BLACK 0 65#define BLACK 0
66#define WHITE 1 66#define WHITE 1
67#define EMPTY 2 67#define EMPTY 2
68#define BORDER 3 68#define BORDER 3
69 69
70/* return values for makemove() */ 70/* return values for makemove() */
71#define MOVEOK 0 71#define MOVEOK 0
72#define RESIGN 1 72#define RESIGN 1
73#define ILLEGAL 2 73#define ILLEGAL 2
74#define WIN 3 74#define WIN 3
75#define TIE 4 75#define TIE 4
76#define SAVE 5 76#define SAVE 5
77 77
78#define A 1 
79#define B 2 
80#define C 3 
81#define D 4 
82#define E 5 
83#define F 6 
84#define G 7 
85#define H 8 
86#define J 9 
87#define K 10 
88#define L 11 
89#define M 12 
90#define N 13 
91#define O 14 
92#define P 15 
93#define Q 16 
94#define R 17 
95#define S 18 
96#define T 19 
97 
98#define PT(x, y) ((x) + BSZ1 * (y)) 78#define PT(x, y) ((x) + BSZ1 * (y))
99 79
100/* 80/*
101 * A 'frame' is a group of five or six contiguous board locations. 81 * A 'frame' is a group of five or six contiguous board locations.
102 * An open ended frame is one with spaces on both ends; otherwise, its closed. 82 * An open ended frame is one with spaces on both ends; otherwise, its closed.
103 * A 'combo' is a group of intersecting frames and consists of two numbers: 83 * A 'combo' is a group of intersecting frames and consists of two numbers:
104 * 'A' is the number of moves to make the combo non-blockable. 84 * 'A' is the number of moves to make the combo non-blockable.
105 * 'B' is the minimum number of moves needed to win once it can't be blocked. 85 * 'B' is the minimum number of moves needed to win once it can't be blocked.
106 * A 'force' is a combo that is one move away from being non-blockable 86 * A 'force' is a combo that is one move away from being non-blockable
107 * 87 *
108 * Single frame combo values: 88 * Single frame combo values:
109 * <A,B> board values 89 * <A,B> board values
110 * 5,0 . . . . . O 90 * 5,0 . . . . . O

cvs diff -r1.25 -r1.26 src/games/gomoku/pickmove.c (expand / switch to unified diff)

--- src/games/gomoku/pickmove.c 2022/05/15 22:08:05 1.25
+++ src/games/gomoku/pickmove.c 2022/05/15 22:18:36 1.26
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pickmove.c,v 1.25 2022/05/15 22:08:05 rillig Exp $ */ 1/* $NetBSD: pickmove.c,v 1.26 2022/05/15 22:18:36 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994 4 * Copyright (c) 1994
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Ralph Campbell. 8 * Ralph Campbell.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include <sys/cdefs.h> 35#include <sys/cdefs.h>
36#ifndef lint 36#ifndef lint
37#if 0 37#if 0
38static char sccsid[] = "@(#)pickmove.c 8.2 (Berkeley) 5/3/95"; 38static char sccsid[] = "@(#)pickmove.c 8.2 (Berkeley) 5/3/95";
39#else 39#else
40__RCSID("$NetBSD: pickmove.c,v 1.25 2022/05/15 22:08:05 rillig Exp $"); 40__RCSID("$NetBSD: pickmove.c,v 1.26 2022/05/15 22:18:36 rillig Exp $");
41#endif 41#endif
42#endif /* not lint */ 42#endif /* not lint */
43 43
44#include <stdlib.h> 44#include <stdlib.h>
45#include <string.h> 45#include <string.h>
46#include <curses.h> 46#include <curses.h>
47#include <limits.h> 47#include <limits.h>
48 48
49#include "gomoku.h" 49#include "gomoku.h"
50 50
51#define BITS_PER_INT (sizeof(int) * CHAR_BIT) 51#define BITS_PER_INT (sizeof(int) * CHAR_BIT)
52#define MAPSZ (BAREA / BITS_PER_INT) 52#define MAPSZ (BAREA / BITS_PER_INT)
53 53
@@ -78,51 +78,51 @@ static int checkframes(struct combostr * @@ -78,51 +78,51 @@ static int checkframes(struct combostr *
78static int sortcombo(struct combostr **, struct combostr **, struct combostr *); 78static int sortcombo(struct combostr **, struct combostr **, struct combostr *);
79static void printcombo(struct combostr *, char *, size_t); 79static void printcombo(struct combostr *, char *, size_t);
80 80
81int 81int
82pickmove(int us) 82pickmove(int us)
83{ 83{
84 struct spotstr *sp, *sp1, *sp2; 84 struct spotstr *sp, *sp1, *sp2;
85 union comboval *Ocp, *Tcp; 85 union comboval *Ocp, *Tcp;
86 unsigned pos; 86 unsigned pos;
87 int m; 87 int m;
88 88
89 /* first move is easy */ 89 /* first move is easy */
90 if (movenum == 1) 90 if (movenum == 1)
91 return PT(K, 10); 91 return PT((BSZ + 1) / 2, (BSZ + 1) / 2);
92 92
93 /* initialize all the board values */ 93 /* initialize all the board values */
94 for (pos = PT(T, 20); pos-- > PT(A, 1); ) { 94 for (pos = PT(BSZ, BSZ + 1); pos-- > PT(1, 1); ) {
95 sp = &board[pos]; 95 sp = &board[pos];
96 sp->s_combo[BLACK].s = MAXCOMBO + 1; 96 sp->s_combo[BLACK].s = MAXCOMBO + 1;
97 sp->s_combo[WHITE].s = MAXCOMBO + 1; 97 sp->s_combo[WHITE].s = MAXCOMBO + 1;
98 sp->s_level[BLACK] = 255; 98 sp->s_level[BLACK] = 255;
99 sp->s_level[WHITE] = 255; 99 sp->s_level[WHITE] = 255;
100 sp->s_nforce[BLACK] = 0; 100 sp->s_nforce[BLACK] = 0;
101 sp->s_nforce[WHITE] = 0; 101 sp->s_nforce[WHITE] = 0;
102 sp->s_flags &= ~(FFLAGALL | MFLAGALL); 102 sp->s_flags &= ~(FFLAGALL | MFLAGALL);
103 } 103 }
104 nforce = 0; 104 nforce = 0;
105 memset(forcemap, 0, sizeof(forcemap)); 105 memset(forcemap, 0, sizeof(forcemap));
106 106
107 /* compute new values */ 107 /* compute new values */
108 nextcolor = us; 108 nextcolor = us;
109 scanframes(BLACK); 109 scanframes(BLACK);
110 scanframes(WHITE); 110 scanframes(WHITE);
111 111
112 /* find the spot with the highest value */ 112 /* find the spot with the highest value */
113 pos = PT(T, 19); 113 pos = PT(BSZ, BSZ);
114 sp1 = sp2 = &board[pos]; 114 sp1 = sp2 = &board[pos];
115 for ( ; pos-- > PT(A, 1); ) { 115 for ( ; pos-- > PT(1, 1); ) {
116 sp = &board[pos]; 116 sp = &board[pos];
117 if (sp->s_occ != EMPTY) 117 if (sp->s_occ != EMPTY)
118 continue; 118 continue;
119 if (debug && (sp->s_combo[BLACK].c.a == 1 || 119 if (debug && (sp->s_combo[BLACK].c.a == 1 ||
120 sp->s_combo[WHITE].c.a == 1)) { 120 sp->s_combo[WHITE].c.a == 1)) {
121 debuglog("- %s %x/%d %d %x/%d %d %d", 121 debuglog("- %s %x/%d %d %x/%d %d %d",
122 stoc(sp - board), 122 stoc(sp - board),
123 sp->s_combo[BLACK].s, sp->s_level[BLACK], 123 sp->s_combo[BLACK].s, sp->s_level[BLACK],
124 sp->s_nforce[BLACK], 124 sp->s_nforce[BLACK],
125 sp->s_combo[WHITE].s, sp->s_level[WHITE], 125 sp->s_combo[WHITE].s, sp->s_level[WHITE],
126 sp->s_nforce[WHITE], 126 sp->s_nforce[WHITE],
127 sp->s_wval); 127 sp->s_wval);
128 } 128 }
@@ -342,27 +342,27 @@ scanframes(int color) @@ -342,27 +342,27 @@ scanframes(int color)
342 d = 2; 342 d = 2;
343 while (d <= ((movenum + 1) >> 1) && combolen > n) { 343 while (d <= ((movenum + 1) >> 1) && combolen > n) {
344 if (debug) { 344 if (debug) {
345 debuglog("%cL%d %d %d %d", "BW"[color], 345 debuglog("%cL%d %d %d %d", "BW"[color],
346 d, combolen - n, combocnt, elistcnt); 346 d, combolen - n, combocnt, elistcnt);
347 refresh(); 347 refresh();
348 } 348 }
349 n = combolen; 349 n = combolen;
350 addframes(d); 350 addframes(d);
351 d++; 351 d++;
352 } 352 }
353 353
354 /* scan for combos at empty spots */ 354 /* scan for combos at empty spots */
355 for (pos = PT(T, 20); pos-- > PT(A, 1); ) { 355 for (pos = PT(BSZ, BSZ + 1); pos-- > PT(1, 1); ) {
356 sp = &board[pos]; 356 sp = &board[pos];
357 for (ep = sp->s_empty; ep; ep = nep) { 357 for (ep = sp->s_empty; ep; ep = nep) {
358 cbp = ep->e_combo; 358 cbp = ep->e_combo;
359 if (cbp->c_combo.s <= sp->s_combo[color].s) { 359 if (cbp->c_combo.s <= sp->s_combo[color].s) {
360 if (cbp->c_combo.s != sp->s_combo[color].s) { 360 if (cbp->c_combo.s != sp->s_combo[color].s) {
361 sp->s_combo[color].s = cbp->c_combo.s; 361 sp->s_combo[color].s = cbp->c_combo.s;
362 sp->s_level[color] = cbp->c_nframes; 362 sp->s_level[color] = cbp->c_nframes;
363 } else if (cbp->c_nframes < sp->s_level[color]) 363 } else if (cbp->c_nframes < sp->s_level[color])
364 sp->s_level[color] = cbp->c_nframes; 364 sp->s_level[color] = cbp->c_nframes;
365 } 365 }
366 nep = ep->e_next; 366 nep = ep->e_next;
367 free(ep); 367 free(ep);
368 elistcnt--; 368 elistcnt--;
@@ -552,27 +552,27 @@ addframes(int level) @@ -552,27 +552,27 @@ addframes(int level)
552{ 552{
553 struct combostr *cbp, *ecbp; 553 struct combostr *cbp, *ecbp;
554 struct spotstr *sp, *fsp; 554 struct spotstr *sp, *fsp;
555 struct elist *ep, *nep; 555 struct elist *ep, *nep;
556 int i, r, d; 556 int i, r, d;
557 struct combostr **cbpp, *pcbp; 557 struct combostr **cbpp, *pcbp;
558 union comboval fcb, cb; 558 union comboval fcb, cb;
559 unsigned pos; 559 unsigned pos;
560 560
561 curlevel = level; 561 curlevel = level;
562 562
563 /* scan for combos at empty spots */ 563 /* scan for combos at empty spots */
564 i = curcolor; 564 i = curcolor;
565 for (pos = PT(T, 20); pos-- > PT(A, 1); ) { 565 for (pos = PT(BSZ, BSZ + 1); pos-- > PT(1, 1); ) {
566 sp = &board[pos]; 566 sp = &board[pos];
567 for (ep = sp->s_empty; ep; ep = nep) { 567 for (ep = sp->s_empty; ep; ep = nep) {
568 cbp = ep->e_combo; 568 cbp = ep->e_combo;
569 if (cbp->c_combo.s <= sp->s_combo[i].s) { 569 if (cbp->c_combo.s <= sp->s_combo[i].s) {
570 if (cbp->c_combo.s != sp->s_combo[i].s) { 570 if (cbp->c_combo.s != sp->s_combo[i].s) {
571 sp->s_combo[i].s = cbp->c_combo.s; 571 sp->s_combo[i].s = cbp->c_combo.s;
572 sp->s_level[i] = cbp->c_nframes; 572 sp->s_level[i] = cbp->c_nframes;
573 } else if (cbp->c_nframes < sp->s_level[i]) 573 } else if (cbp->c_nframes < sp->s_level[i])
574 sp->s_level[i] = cbp->c_nframes; 574 sp->s_level[i] = cbp->c_nframes;
575 } 575 }
576 nep = ep->e_next; 576 nep = ep->e_next;
577 free(ep); 577 free(ep);
578 elistcnt--; 578 elistcnt--;