Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/gomoku gomoku: reduce scope of local variables



details:   https://anonhg.NetBSD.org/src/rev/ac0faa662dda
branches:  trunk
changeset: 366454:ac0faa662dda
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri May 27 23:10:54 2022 +0000

description:
gomoku: reduce scope of local variables

No binary change.

diffstat:

 games/gomoku/gomoku.h   |   7 +---
 games/gomoku/makemove.c |  84 ++++++++++++++++++++++-----------------------
 games/gomoku/pickmove.c |  91 +++++++++++++++++++++++-------------------------
 3 files changed, 86 insertions(+), 96 deletions(-)

diffs (truncated from 513 to 300 lines):

diff -r e482e334206e -r ac0faa662dda games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Fri May 27 21:43:26 2022 +0000
+++ b/games/gomoku/gomoku.h     Fri May 27 23:10:54 2022 +0000
@@ -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
diff -r e482e334206e -r ac0faa662dda games/gomoku/makemove.c
--- a/games/gomoku/makemove.c   Fri May 27 21:43:26 2022 +0000
+++ b/games/gomoku/makemove.c   Fri May 27 23:10:54 2022 +0000
@@ -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 @@
 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 @@
 
        /* 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 @@
                    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 @@
                }
 
                /* 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 @@
                    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 @@
 
                /* 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 @@
                        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 @@
 
            /* 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 @@
 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 @@
                 * 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 @@
 
                /* 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;
                    }
diff -r e482e334206e -r ac0faa662dda games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c   Fri May 27 21:43:26 2022 +0000
+++ b/games/gomoku/pickmove.c   Fri May 27 23:10:54 2022 +0000
@@ -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 @@
 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 @@
 
        /* 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 @@
 
        /* 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 @@
                 * 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];



Home | Main Index | Thread Index | Old Index