Module Name: src
Committed By: rillig
Date: Fri May 27 23:10:54 UTC 2022
Modified Files:
src/games/gomoku: gomoku.h makemove.c pickmove.c
Log Message:
gomoku: reduce scope of local variables
No binary change.
To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/games/gomoku/gomoku.h
cvs rdiff -u -r1.23 -r1.24 src/games/gomoku/makemove.c
cvs rdiff -u -r1.45 -r1.46 src/games/gomoku/pickmove.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/games/gomoku/gomoku.h
diff -u src/games/gomoku/gomoku.h:1.40 src/games/gomoku/gomoku.h:1.41
--- src/games/gomoku/gomoku.h:1.40 Fri May 27 19:59:56 2022
+++ src/games/gomoku/gomoku.h Fri May 27 23:10:54 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: gomoku.h,v 1.40 2022/05/27 19:59:56 rillig Exp $ */
+/* $NetBSD: gomoku.h,v 1.41 2022/05/27 23:10:54 rillig Exp $ */
/*
* Copyright (c) 1994
@@ -53,11 +53,6 @@
*/
#define FAREA (2 * BSZ * (BSZ - 4) + 2 * (BSZ - 4) * (BSZ - 4))
-#define MUP (BSZ + 1)
-#define MDOWN (-(BSZ + 1))
-#define MLEFT (-1)
-#define MRIGHT (1)
-
/* values for s_occ */
#define BLACK 0
#define WHITE 1
Index: src/games/gomoku/makemove.c
diff -u src/games/gomoku/makemove.c:1.23 src/games/gomoku/makemove.c:1.24
--- src/games/gomoku/makemove.c:1.23 Fri May 27 20:48:42 2022
+++ src/games/gomoku/makemove.c Fri May 27 23:10:54 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: makemove.c,v 1.23 2022/05/27 20:48:42 rillig Exp $ */
+/* $NetBSD: makemove.c,v 1.24 2022/05/27 23:10:54 rillig Exp $ */
/*
* Copyright (c) 1994
@@ -34,13 +34,16 @@
#include <sys/cdefs.h>
/* @(#)makemove.c 8.2 (Berkeley) 5/3/95 */
-__RCSID("$NetBSD: makemove.c,v 1.23 2022/05/27 20:48:42 rillig Exp $");
+__RCSID("$NetBSD: makemove.c,v 1.24 2022/05/27 23:10:54 rillig Exp $");
#include "gomoku.h"
/* direction deltas */
const int dd[4] = {
- MRIGHT, MRIGHT+MDOWN, MDOWN, MDOWN+MLEFT
+ 1, /* right */
+ -(BSZ + 1) + 1, /* down + right */
+ -(BSZ + 1), /* down */
+ -(BSZ + 1) - 1 /* down + left */
};
static const int weight[5] = { 0, 1, 7, 22, 100 };
@@ -58,21 +61,13 @@ static void update_overlap(struct spotst
int
makemove(int us, int mv)
{
- struct spotstr *sp, *fsp;
- union comboval *cp;
- struct spotstr *osp;
- struct combostr *cbp, *cbp1;
- union comboval *cp1;
- int d, n;
- int val, bmask;
- bool space;
/* check for end of game */
if (mv == RESIGN)
return RESIGN;
/* check for illegal move */
- sp = &board[mv];
+ struct spotstr *sp = &board[mv];
if (sp->s_occ != EMPTY)
return ILLEGAL;
@@ -82,11 +77,12 @@ makemove(int us, int mv)
/* compute new frame values */
sp->s_wval = 0;
- osp = sp;
+ struct spotstr *osp = sp;
for (int r = 4; --r >= 0; ) { /* for each direction */
- d = dd[r];
- fsp = osp;
- bmask = BFLAG << r;
+ int d = dd[r];
+ struct spotstr *fsp = osp;
+ int bmask = BFLAG << r;
+
for (int f = 5; --f >= 0; fsp -= d) { /* for each frame */
if (fsp->s_occ == BORDER)
goto nextr;
@@ -94,7 +90,7 @@ makemove(int us, int mv)
continue;
/* remove this frame from the sorted list of frames */
- cbp = fsp->s_frame[r];
+ struct combostr *cbp = fsp->s_frame[r];
if (cbp->c_next != NULL) {
if (sortframes[BLACK] == cbp)
sortframes[BLACK] = cbp->c_next;
@@ -105,7 +101,9 @@ makemove(int us, int mv)
}
/* compute old weight value for this frame */
- cp = &fsp->s_fval[BLACK][r];
+ union comboval *cp = &fsp->s_fval[BLACK][r];
+
+ int val;
if (cp->s <= 0x500)
val = weight[5 - cp->cv_force - cp->cv_win];
else
@@ -115,9 +113,9 @@ makemove(int us, int mv)
val += weight[5 - cp->cv_force - cp->cv_win];
/* compute new combo value for this frame */
+ bool space = fsp->s_occ == EMPTY;
+ int n = 0;
sp = fsp;
- space = sp->s_occ == EMPTY;
- n = 0;
for (int i = 5; --i >= 0; sp += d) { /* for each spot */
if (sp->s_occ == us)
n++;
@@ -139,7 +137,7 @@ makemove(int us, int mv)
/* check for game over */
if (n == 5)
- return(WIN);
+ return WIN;
/* compute new value & combo number for this frame & color */
fsp->s_fval[us != BLACK ? BLACK : WHITE][r].s = 0x600;
@@ -159,11 +157,12 @@ makemove(int us, int mv)
sp->s_wval += val;
/* add this frame to the sorted list of frames by combo value */
- cbp1 = sortframes[us];
+ struct combostr *cbp1 = sortframes[us];
if (cbp1 == NULL)
sortframes[us] = cbp->c_next = cbp->c_prev = cbp;
else {
- cp1 = &board[cbp1->c_vertex].s_fval[us][cbp1->c_dir];
+ union comboval *cp1 =
+ &board[cbp1->c_vertex].s_fval[us][cbp1->c_dir];
if (cp->s <= cp1->s) {
/* insert at the head of the list */
sortframes[us] = cbp;
@@ -187,7 +186,7 @@ makemove(int us, int mv)
/* both ends open? */
if (fsp->s_occ == EMPTY) {
- cp = &fsp->s_fval[BLACK][r];
+ union comboval *cp = &fsp->s_fval[BLACK][r];
if (cp->cv_win != 0) {
cp->cv_force++;
cp->cv_win = 0;
@@ -221,17 +220,13 @@ makemove(int us, int mv)
static void
update_overlap(struct spotstr *osp)
{
- struct spotstr *sp, *sp1, *sp2;
- int d, d1, n;
- int a, b, bmask, bmask1;
- struct spotstr *esp;
- u_char *str;
- esp = NULL;
+ struct spotstr *esp = NULL;
for (int r = 4; --r >= 0; ) { /* for each direction */
- d = dd[r];
- sp1 = osp;
- bmask = BFLAG << r;
+ int d = dd[r];
+ struct spotstr *sp1 = osp;
+ int bmask = BFLAG << r;
+
for (int f = 0; f < 6; f++, sp1 -= d) { /* for each frame */
if (sp1->s_occ == BORDER)
break;
@@ -244,26 +239,29 @@ update_overlap(struct spotstr *osp)
* do the rows 0 <= r1 <= r. The r1 == r case is special
* since the two frames can overlap at more than one point.
*/
- str = &overlap[(a = (int)(sp1->s_frame[r] - frames)) * FAREA];
- sp2 = sp1 - d;
+ int a = (int)(sp1->s_frame[r] - frames);
+ u_char *str = &overlap[a * FAREA];
+ struct spotstr *sp2 = sp1 - d;
+
for (int i = f + 1; i < 6; i++, sp2 -= d) {
if (sp2->s_occ == BORDER)
break;
if ((sp2->s_flags & bmask) != 0)
continue;
+
/*
* count the number of empty spots to see if there is
* still an overlap.
*/
- n = 0;
- sp = sp1;
- for (b = i - f; b < 5; b++, sp += d) {
+ int n = 0;
+ struct spotstr *sp = sp1;
+ for (int b = i - f; b < 5; b++, sp += d) {
if (sp->s_occ == EMPTY) {
esp = sp; /* save the intersection point */
n++;
}
}
- b = (int)(sp2->s_frame[r] - frames);
+ int b = (int)(sp2->s_frame[r] - frames);
if (n == 0) {
if (sp->s_occ == EMPTY) {
str[b] &= 0xA;
@@ -290,15 +288,15 @@ update_overlap(struct spotstr *osp)
/* the other directions can only intersect at spot osp */
for (int r1 = r; --r1 >= 0; ) {
- d1 = dd[r1];
- bmask1 = BFLAG << r1;
- sp = osp;
+ int d1 = dd[r1];
+ int bmask1 = BFLAG << r1;
+ struct spotstr *sp = osp;
for (int i = 6; --i >= 0; sp -= d1) { /* for each spot */
if (sp->s_occ == BORDER)
break;
if ((sp->s_flags & bmask1) != 0)
continue;
- b = (int)(sp->s_frame[r1] - frames);
+ int b = (int)(sp->s_frame[r1] - frames);
str[b] = 0;
overlap[b * FAREA + a] = 0;
}
Index: src/games/gomoku/pickmove.c
diff -u src/games/gomoku/pickmove.c:1.45 src/games/gomoku/pickmove.c:1.46
--- src/games/gomoku/pickmove.c:1.45 Fri May 27 19:59:56 2022
+++ src/games/gomoku/pickmove.c Fri May 27 23:10:54 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: pickmove.c,v 1.45 2022/05/27 19:59:56 rillig Exp $ */
+/* $NetBSD: pickmove.c,v 1.46 2022/05/27 23:10:54 rillig Exp $ */
/*
* Copyright (c) 1994
@@ -34,7 +34,7 @@
#include <sys/cdefs.h>
/* @(#)pickmove.c 8.2 (Berkeley) 5/3/95 */
-__RCSID("$NetBSD: pickmove.c,v 1.45 2022/05/27 19:59:56 rillig Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.46 2022/05/27 23:10:54 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -87,9 +87,6 @@ static void printcombo(struct combostr *
int
pickmove(int us)
{
- struct spotstr *sp, *sp1, *sp2;
- union comboval *Ocp, *Tcp;
- int m;
/* first move is easy */
if (nmoves == 0)
@@ -97,7 +94,7 @@ pickmove(int us)
/* initialize all the board values */
for (unsigned pos = PT(BSZ, BSZ + 1); pos-- > PT(1, 1); ) {
- sp = &board[pos];
+ struct spotstr *sp = &board[pos];
sp->s_combo[BLACK].s = 0x601;
sp->s_combo[WHITE].s = 0x601;
sp->s_level[BLACK] = 255;
@@ -116,9 +113,10 @@ pickmove(int us)
/* find the spot with the highest value */
unsigned pos = PT(BSZ, BSZ);
- sp1 = sp2 = &board[pos];
+ struct spotstr *sp1 = &board[pos];
+ struct spotstr *sp2 = sp1;
for ( ; pos-- > PT(1, 1); ) {
- sp = &board[pos];
+ struct spotstr *sp = &board[pos];
if (sp->s_occ != EMPTY)
continue;
if (debug != 0 && (sp->s_combo[BLACK].cv_force == 1 ||
@@ -156,19 +154,22 @@ pickmove(int us)
* Check for more than one force that can't
* all be blocked with one move.
*/
- sp = (us == BLACK) ? sp2 : sp1;
- m = (int)(sp - board);
+ struct spotstr *sp = (us == BLACK) ? sp2 : sp1;
+ int m = (int)(sp - board);
if (sp->s_combo[us != BLACK ? BLACK : WHITE].cv_force == 1 &&
!BIT_TEST(forcemap, m))
debuglog("*** Can't be blocked");
}
+
+ union comboval *Ocp, *Tcp;
if (us == BLACK) {
Ocp = &sp1->s_combo[BLACK];
Tcp = &sp2->s_combo[WHITE];
} else {
Tcp = &sp1->s_combo[BLACK];
Ocp = &sp2->s_combo[WHITE];
- sp = sp1;
+
+ struct spotstr *sp = sp1;
sp1 = sp2;
sp2 = sp;
}
@@ -189,7 +190,6 @@ pickmove(int us)
static bool
better(const struct spotstr *sp, const struct spotstr *sp1, int us)
{
- int them, s, s1;
if (/* .... */ sp->s_combo[us].s != sp1->s_combo[us].s)
return sp->s_combo[us].s < sp1->s_combo[us].s;
@@ -198,9 +198,9 @@ better(const struct spotstr *sp, const s
if (/* .... */ sp->s_nforce[us] != sp1->s_nforce[us])
return sp->s_nforce[us] > sp1->s_nforce[us];
- them = us != BLACK ? BLACK : WHITE;
- s = (int)(sp - board);
- s1 = (int)(sp1 - board);
+ int them = us != BLACK ? BLACK : WHITE;
+ int s = (int)(sp - board);
+ int s1 = (int)(sp1 - board);
if (BIT_TEST(forcemap, s) != BIT_TEST(forcemap, s1))
return BIT_TEST(forcemap, s);
@@ -228,7 +228,7 @@ static unsigned int curlevel; /* implici
static void
scanframes(int color)
{
- struct combostr *cbp, *ecbp;
+ struct combostr *ecbp;
struct spotstr *sp;
union comboval *cp;
struct elist *nep;
@@ -238,7 +238,7 @@ scanframes(int color)
curcolor = color;
/* check for empty list of frames */
- cbp = sortframes[color];
+ struct combostr *cbp = sortframes[color];
if (cbp == NULL)
return;
@@ -417,10 +417,9 @@ scanframes(int color)
static void
makecombo2(struct combostr *ocbp, struct spotstr *osp, int off, int s)
{
- struct spotstr *fsp;
struct combostr *ncbp;
- int d, c;
- int baseB, fcnt, emask, bmask, n;
+ int c;
+ int baseB, fcnt, emask, n;
union comboval ocb, fcb;
struct combostr **scbpp, *fcbp;
char tmp[128];
@@ -434,15 +433,15 @@ makecombo2(struct combostr *ocbp, struct
/* don't include frames that overlap in the same direction */
if (r == ocbp->c_dir)
continue;
- d = dd[r];
+ int d = dd[r];
/*
* Frame A combined with B is the same value as B combined with A
* so skip frames that have already been processed (MFLAG).
* Also skip blocked frames (BFLAG) and frames that are <1,x>
* since combining another frame with it isn't valid.
*/
- bmask = (BFLAG | FFLAG | MFLAG) << r;
- fsp = osp;
+ int bmask = (BFLAG | FFLAG | MFLAG) << r;
+ struct spotstr *fsp = osp;
for (int f = 0; f < 5; f++, fsp -= d) { /* for each frame */
if (fsp->s_occ == BORDER)
break;
@@ -548,9 +547,9 @@ static void
addframes(unsigned int level)
{
struct combostr *cbp, *ecbp;
- struct spotstr *sp, *fsp;
+ struct spotstr *fsp;
struct elist *nep;
- int i, r, d;
+ int i, r;
struct combostr **cbpp, *pcbp;
union comboval fcb, cb;
@@ -559,7 +558,7 @@ addframes(unsigned int level)
/* scan for combos at empty spots */
i = curcolor;
for (unsigned pos = PT(BSZ, BSZ + 1); pos-- > PT(1, 1); ) {
- sp = &board[pos];
+ struct spotstr *sp = &board[pos];
for (struct elist *ep = sp->s_empty; ep != NULL; ep = nep) {
cbp = ep->e_combo;
if (cbp->c_combo.s <= sp->s_combo[i].s) {
@@ -611,8 +610,8 @@ addframes(unsigned int level)
* The next four spots are handled the same for both
* open and closed ended frames.
*/
- d = dd[r];
- sp = fsp + d;
+ int d = dd[r];
+ struct spotstr *sp = fsp + d;
for (i = 1; i < 5; i++, sp += d) {
if (sp->s_occ != EMPTY)
continue;
@@ -648,9 +647,8 @@ addframes(unsigned int level)
static void
makecombo(struct combostr *ocbp, struct spotstr *osp, int off, int s)
{
- struct combostr *cbp, *ncbp;
+ struct combostr *cbp;
struct spotstr *sp;
- int n, c;
struct combostr **scbpp;
int baseB, fcnt, emask, verts;
union comboval ocb;
@@ -707,17 +705,17 @@ makecombo(struct combostr *ocbp, struct
}
/* compute the first half of the combo value */
- c = cbp->c_combo.cv_force + ocb.cv_force - verts - 3;
+ int c = cbp->c_combo.cv_force + ocb.cv_force - verts - 3;
if (c > 4)
continue;
/* compute the second half of the combo value */
- n = ep->e_fval.cv_force + ep->e_fval.cv_win - 1;
+ int n = ep->e_fval.cv_force + ep->e_fval.cv_win - 1;
if (baseB < n)
n = baseB;
/* make a new combo! */
- ncbp = (struct combostr *)malloc(sizeof(struct combostr) +
+ struct combostr *ncbp = malloc(sizeof(struct combostr) +
(cbp->c_nframes + 1) * sizeof(struct combostr *));
if (ncbp == NULL)
panic("Out of memory!");
@@ -958,10 +956,8 @@ makeempty(struct combostr *ocbp)
static void
updatecombo(struct combostr *cbp, int color)
{
- struct spotstr *sp;
struct combostr *tcbp;
- int d;
- int nframes, flags, s;
+ int nframes, flags;
union comboval cb;
flags = 0;
@@ -977,7 +973,7 @@ updatecombo(struct combostr *cbp, int co
cb.cv_win = cbp->c_combo.cv_win;
if (color == nextcolor) {
/* update the board value for the vertex */
- sp = &board[cbp->c_vertex];
+ struct spotstr *sp = &board[cbp->c_vertex];
sp->s_nforce[color]++;
if (cb.s <= sp->s_combo[color].s) {
if (cb.s != sp->s_combo[color].s) {
@@ -988,8 +984,9 @@ updatecombo(struct combostr *cbp, int co
}
} else {
/* update the board values for each spot in frame */
- sp = &board[s = tcbp->c_vertex];
- d = dd[tcbp->c_dir];
+ int s = tcbp->c_vertex;
+ struct spotstr *sp = &board[s];
+ int d = dd[tcbp->c_dir];
int i = (flags & C_OPEN_1) != 0 ? 6 : 5;
for (; --i >= 0; sp += d, s += d) {
if (sp->s_occ != EMPTY)
@@ -1012,8 +1009,9 @@ updatecombo(struct combostr *cbp, int co
if (color != nextcolor) {
/* update the board values for each spot in frame */
- sp = &board[s = cbp->c_vertex];
- d = dd[cbp->c_dir];
+ int s = cbp->c_vertex;
+ struct spotstr *sp = &board[s];
+ int d = dd[cbp->c_dir];
int i = (flags & C_OPEN_0) != 0 ? 6 : 5;
for (; --i >= 0; sp += d, s += d) {
if (sp->s_occ != EMPTY)
@@ -1461,18 +1459,17 @@ markcombo(struct combostr *ocbp)
void
clearcombo(struct combostr *cbp, int open)
{
- struct spotstr *sp;
struct combostr *tcbp;
- int d, n, mask;
for (; (tcbp = cbp->c_link[1]) != NULL; cbp = cbp->c_link[0]) {
clearcombo(tcbp, cbp->c_flags & C_OPEN_1);
open = cbp->c_flags & C_OPEN_0;
}
- sp = &board[cbp->c_vertex];
- d = dd[n = cbp->c_dir];
- mask = ~((IFLAG | CFLAG) << n);
- n = open != 0 ? 6 : 5;
+
+ struct spotstr *sp = &board[cbp->c_vertex];
+ int d = dd[cbp->c_dir];
+ int mask = ~((IFLAG | CFLAG) << cbp->c_dir);
+ int n = open != 0 ? 6 : 5;
for (; --n >= 0; sp += d)
sp->s_flags &= mask;
}