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 'for' loop variables



details:   https://anonhg.NetBSD.org/src/rev/7d7c3628858a
branches:  trunk
changeset: 366215:7d7c3628858a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed May 18 22:30:19 2022 +0000

description:
gomoku: reduce scope of 'for' loop variables

No binary change.

diffstat:

 games/gomoku/bdinit.c   |  32 +++++++++++++++-----------------
 games/gomoku/bdisp.c    |  23 +++++++++++------------
 games/gomoku/makemove.c |  26 +++++++++++++-------------
 games/gomoku/pickmove.c |  44 ++++++++++++++++++++------------------------
 games/gomoku/stoc.c     |  12 +++++-------
 5 files changed, 64 insertions(+), 73 deletions(-)

diffs (truncated from 521 to 300 lines):

diff -r 57d76856a0eb -r 7d7c3628858a games/gomoku/bdinit.c
--- a/games/gomoku/bdinit.c     Wed May 18 21:45:40 2022 +0000
+++ b/games/gomoku/bdinit.c     Wed May 18 22:30:19 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdinit.c,v 1.14 2022/05/18 21:45:40 rillig Exp $       */
+/*     $NetBSD: bdinit.c,v 1.15 2022/05/18 22:30:19 rillig Exp $       */
 
 /*
  * Copyright (c) 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "from: @(#)bdinit.c     8.2 (Berkeley) 5/3/95";
 #else
-__RCSID("$NetBSD: bdinit.c,v 1.14 2022/05/18 21:45:40 rillig Exp $");
+__RCSID("$NetBSD: bdinit.c,v 1.15 2022/05/18 22:30:19 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -49,7 +49,6 @@
 void
 bdinit(struct spotstr *bp)
 {
-       int i, j, r;
        struct spotstr *sp;
        struct combostr *cbp;
 
@@ -57,7 +56,7 @@
 
        /* mark the borders as such */
        sp = bp;
-       for (i = 1 + BSZ + 1; --i >= 0; sp++) {
+       for (int i = 1 + BSZ + 1; --i >= 0; sp++) {
                sp->s_occ = BORDER;                     /* top border */
                sp->s_flags = BFLAGALL;
        }
@@ -65,8 +64,8 @@
        /* fill entire board with EMPTY spots */
        memset(frames, 0, sizeof(frames));
        cbp = frames;
-       for (j = 0; ++j < BSZ + 1; sp++) {              /* for each row */
-               for (i = 0; ++i < BSZ + 1; sp++) {      /* for each column */
+       for (int j = 0; ++j < BSZ + 1; sp++) {          /* for each row */
+               for (int i = 0; ++i < BSZ + 1; sp++) {  /* for each column */
                        sp->s_occ = EMPTY;
                        sp->s_flags = 0;
                        sp->s_wval = 0;
@@ -129,7 +128,7 @@
                        /*
                         * Allocate a frame structure for non blocked frames.
                         */
-                       for (r = 4; --r >= 0; ) {
+                       for (int r = 4; --r >= 0; ) {
                                if ((sp->s_flags & (BFLAG << r)) != 0)
                                        continue;
                                cbp->c_combo.s = sp->s_fval[BLACK][r].s;
@@ -145,7 +144,7 @@
        }
 
        /* mark the borders as such */
-       for (i = BSZ + 1; --i >= 0; sp++) {
+       for (int i = BSZ + 1; --i >= 0; sp++) {
                sp->s_occ = BORDER;                     /* bottom border */
                sp->s_flags = BFLAGALL;
        }
@@ -177,8 +176,7 @@
 {
        struct spotstr *sp1, *sp2;
        struct combostr *cbp;
-       unsigned frameix;
-       int i, f, r, n, d1, d2;
+       int n, d1, d2;
        int mask, bmask, vertex, s;
        u_char *op;
        short *ip;
@@ -187,28 +185,28 @@
        memset(intersect, 0, sizeof(intersect));
        op = &overlap[FAREA * FAREA];
        ip = &intersect[FAREA * FAREA];
-       for (frameix = FAREA; frameix-- > 0; ) {        /* each frame */
-           cbp = &frames[frameix];
+       for (unsigned fi = FAREA; fi-- > 0; ) { /* each frame */
+           cbp = &frames[fi];
            op -= FAREA;
            ip -= FAREA;
            sp1 = &board[vertex = cbp->c_vertex];
-           d1 = dd[r = cbp->c_dir];
+           d1 = dd[cbp->c_dir];
            /*
             * s = 5 if closed, 6 if open.
             * At this point black & white are the same.
             */
-           s = 5 + sp1->s_fval[BLACK][r].c.b;
+           s = 5 + sp1->s_fval[BLACK][cbp->c_dir].c.b;
            /* for each spot in frame A */
-           for (i = 0; i < s; i++, sp1 += d1, vertex += d1) {
+           for (int i = 0; i < s; i++, sp1 += d1, vertex += d1) {
                /* the sixth spot in frame A only overlaps if it is open */
                mask = (i == 5) ? 0xC : 0xF;
                /* for each direction */
-               for (r = 4; --r >= 0; ) {
+               for (int r = 4; --r >= 0; ) {
                    bmask = BFLAG << r;
                    sp2 = sp1;
                    d2 = dd[r];
                    /* for each frame that intersects at spot sp1 */
-                   for (f = 0; f < 6; f++, sp2 -= d2) {
+                   for (int f = 0; f < 6; f++, sp2 -= d2) {
                        if (sp2->s_occ == BORDER)
                            break;
                        if ((sp2->s_flags & bmask) != 0)
diff -r 57d76856a0eb -r 7d7c3628858a games/gomoku/bdisp.c
--- a/games/gomoku/bdisp.c      Wed May 18 21:45:40 2022 +0000
+++ b/games/gomoku/bdisp.c      Wed May 18 22:30:19 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdisp.c,v 1.27 2022/05/16 22:03:16 rillig Exp $        */
+/*     $NetBSD: bdisp.c,v 1.28 2022/05/18 22:30:19 rillig Exp $        */
 
 /*
  * Copyright (c) 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)bdisp.c    8.2 (Berkeley) 5/3/95";
 #else
-__RCSID("$NetBSD: bdisp.c,v 1.27 2022/05/16 22:03:16 rillig Exp $");
+__RCSID("$NetBSD: bdisp.c,v 1.28 2022/05/18 22:30:19 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -98,22 +98,21 @@
 void
 bdisp_init(void)
 {
-       int i, j;
 
        /* top border */
-       for (i = 1; i < BSZ + 1; i++) {
+       for (int i = 1; i < BSZ + 1; i++) {
                move(0, 2 * i + 1);
                addch(letters[i]);
        }
        /* left and right edges */
-       for (j = BSZ + 1; --j > 0; ) {
+       for (int j = BSZ + 1; --j > 0; ) {
                move(20 - j, 0);
                printw("%2d ", j);
                move(20 - j, 2 * (BSZ + 1) + 1);
                printw("%d ", j);
        }
        /* bottom border */
-       for (i = 1; i < BSZ + 1; i++) {
+       for (int i = 1; i < BSZ + 1; i++) {
                move(20, 2 * i + 1);
                addch(letters[i]);
        }
@@ -162,11 +161,11 @@
 void
 bdisp(void)
 {
-       int i, j, c;
+       int c;
        struct spotstr *sp;
 
-       for (j = BSZ + 1; --j > 0; ) {
-               for (i = 1; i < BSZ + 1; i++) {
+       for (int j = BSZ + 1; --j > 0; ) {
+               for (int i = 1; i < BSZ + 1; i++) {
                        move(BSZ + 1 - j, 2 * i + 1);
                        sp = &board[i + j * (BSZ + 1)];
                        if (debug > 1 && sp->s_occ == EMPTY) {
@@ -196,16 +195,16 @@
 void
 bdump(FILE *fp)
 {
-       int i, j, c;
+       int c;
        struct spotstr *sp;
 
        /* top border */
        fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
 
-       for (j = BSZ + 1; --j > 0; ) {
+       for (int j = BSZ + 1; --j > 0; ) {
                /* left edge */
                fprintf(fp, "%2d ", j);
-               for (i = 1; i < BSZ + 1; i++) {
+               for (int i = 1; i < BSZ + 1; i++) {
                        sp = &board[i + j * (BSZ + 1)];
                        if (debug > 1 && sp->s_occ == EMPTY) {
                                if ((sp->s_flags & IFLAGALL) != 0)
diff -r 57d76856a0eb -r 7d7c3628858a games/gomoku/makemove.c
--- a/games/gomoku/makemove.c   Wed May 18 21:45:40 2022 +0000
+++ b/games/gomoku/makemove.c   Wed May 18 22:30:19 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makemove.c,v 1.16 2022/05/16 21:48:45 rillig Exp $     */
+/*     $NetBSD: makemove.c,v 1.17 2022/05/18 22:30:19 rillig Exp $     */
 
 /*
  * Copyright (c) 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)makemove.c 8.2 (Berkeley) 5/3/95";
 #else
-__RCSID("$NetBSD: makemove.c,v 1.16 2022/05/16 21:48:45 rillig Exp $");
+__RCSID("$NetBSD: makemove.c,v 1.17 2022/05/18 22:30:19 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -68,7 +68,7 @@
        struct spotstr *osp;
        struct combostr *cbp, *cbp1;
        union comboval *cp1;
-       int i, f, r, d, n;
+       int d, n;
        int val, bmask;
        bool space;
 
@@ -90,11 +90,11 @@
        /* compute new frame values */
        sp->s_wval = 0;
        osp = sp;
-       for (r = 4; --r >= 0; ) {                       /* for each direction */
+       for (int r = 4; --r >= 0; ) {           /* for each direction */
            d = dd[r];
            fsp = osp;
            bmask = BFLAG << r;
-           for (f = 5; --f >= 0; fsp -= d) {           /* for each frame */
+           for (int f = 5; --f >= 0; fsp -= d) {       /* for each frame */
                if (fsp->s_occ == BORDER)
                    goto nextr;
                if ((fsp->s_flags & bmask) != 0)
@@ -125,7 +125,7 @@
                sp = fsp;
                space = sp->s_occ == EMPTY;
                n = 0;
-               for (i = 5; --i >= 0; sp += d) {        /* for each spot */
+               for (int i = 5; --i >= 0; sp += d) {    /* for each spot */
                    if (sp->s_occ == us)
                        n++;
                    else if (sp->s_occ == EMPTY)
@@ -161,7 +161,7 @@
                }
                val = weight[n];
                sp = fsp;
-               for (i = 5; --i >= 0; sp += d)          /* for each spot */
+               for (int i = 5; --i >= 0; sp += d)      /* for each spot */
                    if (sp->s_occ == EMPTY)
                        sp->s_wval += val;
 
@@ -222,17 +222,17 @@
 update_overlap(struct spotstr *osp)
 {
        struct spotstr *sp, *sp1, *sp2;
-       int i, f, r, r1, d, d1, n;
+       int d, d1, n;
        int a, b, bmask, bmask1;
        struct spotstr *esp;
        u_char *str;
 
        esp = NULL;
-       for (r = 4; --r >= 0; ) {                       /* for each direction */
+       for (int r = 4; --r >= 0; ) {           /* for each direction */
            d = dd[r];
            sp1 = osp;
            bmask = BFLAG << r;
-           for (f = 0; f < 6; f++, sp1 -= d) {         /* for each frame */
+           for (int f = 0; f < 6; f++, sp1 -= d) {     /* for each frame */
                if (sp1->s_occ == BORDER)
                    break;
                if ((sp1->s_flags & bmask) != 0)
@@ -246,7 +246,7 @@
                 */
                str = &overlap[(a = (int)(sp1->s_frame[r] - frames)) * FAREA];
                sp2 = sp1 - d;
-               for (i = f + 1; i < 6; i++, sp2 -= d) {
+               for (int i = f + 1; i < 6; i++, sp2 -= d) {
                    if (sp2->s_occ == BORDER)
                        break;
                    if ((sp2->s_flags & bmask) != 0)
@@ -289,11 +289,11 @@
                }
 
                /* the other directions can only intersect at spot osp */
-               for (r1 = r; --r1 >= 0; ) {
+               for (int r1 = r; --r1 >= 0; ) {
                    d1 = dd[r1];
                    bmask1 = BFLAG << r1;
                    sp = osp;
-                   for (i = 6; --i >= 0; sp -= d1) {   /* for each spot */
+                   for (int i = 6; --i >= 0; sp -= d1) { /* for each spot */
                        if (sp->s_occ == BORDER)
                            break;
                        if ((sp->s_flags & bmask1) != 0)
diff -r 57d76856a0eb -r 7d7c3628858a games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c   Wed May 18 21:45:40 2022 +0000
+++ b/games/gomoku/pickmove.c   Wed May 18 22:30:19 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pickmove.c,v 1.33 2022/05/16 21:48:45 rillig Exp $     */
+/*     $NetBSD: pickmove.c,v 1.34 2022/05/18 22:30:19 rillig Exp $     */
 
 /*



Home | Main Index | Thread Index | Old Index