Source-Changes-HG archive

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

[src/trunk]: src/games/gomoku gomoku: use consistent variable names



details:   https://anonhg.NetBSD.org/src/rev/3a9cae5fb563
branches:  trunk
changeset: 366487:3a9cae5fb563
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 29 00:12:11 2022 +0000

description:
gomoku: use consistent variable names

Previously, the name 's' was used for the index of a spot on the board,
as well as for the value of a combo, and for a few other purposes. Use
different names and mark the spot indexes using a custom type.

No binary change.

diffstat:

 games/gomoku/bdinit.c   |   8 +++---
 games/gomoku/bdisp.c    |   6 ++--
 games/gomoku/gomoku.h   |  11 ++++---
 games/gomoku/main.c     |   8 +++---
 games/gomoku/pickmove.c |  62 ++++++++++++++++++++++++------------------------
 games/gomoku/stoc.c     |   8 +++---
 6 files changed, 52 insertions(+), 51 deletions(-)

diffs (truncated from 403 to 300 lines):

diff -r 1c703e85f9bc -r 3a9cae5fb563 games/gomoku/bdinit.c
--- a/games/gomoku/bdinit.c     Sat May 28 23:05:45 2022 +0000
+++ b/games/gomoku/bdinit.c     Sun May 29 00:12:11 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdinit.c,v 1.27 2022/05/28 19:47:24 rillig Exp $       */
+/*     $NetBSD: bdinit.c,v 1.28 2022/05/29 00:12:11 rillig Exp $       */
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*     from: @(#)bdinit.c      8.2 (Berkeley) 5/3/95   */
-__RCSID("$NetBSD: bdinit.c,v 1.27 2022/05/28 19:47:24 rillig Exp $");
+__RCSID("$NetBSD: bdinit.c,v 1.28 2022/05/29 00:12:11 rillig Exp $");
 
 #include <string.h>
 #include "gomoku.h"
@@ -233,7 +233,7 @@
  * each frame B that overlaps frame A in that spot.
  */
 static void
-init_overlap_frame(int fia, int ra, int sia, int s, int mask)
+init_overlap_frame(int fia, int ra, int sia, spot_index s, int mask)
 {
 
        for (int rb = 4; --rb >= 0;) {
@@ -264,7 +264,7 @@
 
        for (int fia = FAREA; fia-- > 0;) {
                const struct combostr *fa = &frames[fia];
-               int s = fa->c_vertex;
+               spot_index s = fa->c_vertex;
                u_char ra = fa->c_dir;
                int da = dd[ra];
 
diff -r 1c703e85f9bc -r 3a9cae5fb563 games/gomoku/bdisp.c
--- a/games/gomoku/bdisp.c      Sat May 28 23:05:45 2022 +0000
+++ b/games/gomoku/bdisp.c      Sun May 29 00:12:11 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdisp.c,v 1.50 2022/05/28 20:54:31 rillig Exp $        */
+/*     $NetBSD: bdisp.c,v 1.51 2022/05/29 00:12:11 rillig Exp $        */
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*     @(#)bdisp.c     8.2 (Berkeley) 5/3/95   */
-__RCSID("$NetBSD: bdisp.c,v 1.50 2022/05/28 20:54:31 rillig Exp $");
+__RCSID("$NetBSD: bdisp.c,v 1.51 2022/05/29 00:12:11 rillig Exp $");
 
 #include <curses.h>
 #include <string.h>
@@ -149,7 +149,7 @@
 }
 
 static bool
-should_highlight(int s)
+should_highlight(spot_index s)
 {
 
        if (game.nmoves > 0 && game.moves[game.nmoves - 1] == s)
diff -r 1c703e85f9bc -r 3a9cae5fb563 games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Sat May 28 23:05:45 2022 +0000
+++ b/games/gomoku/gomoku.h     Sun May 29 00:12:11 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gomoku.h,v 1.46 2022/05/28 23:05:45 rillig Exp $       */
+/*     $NetBSD: gomoku.h,v 1.47 2022/05/29 00:12:11 rillig Exp $       */
 
 /*
  * Copyright (c) 1994
@@ -60,6 +60,7 @@
 #define BORDER 3
 
 /* return values for makemove, readinput */
+typedef int spot_index;
 #define MOVEOK 0
 #define RESIGN 1
 #define ILLEGAL        2
@@ -217,9 +218,9 @@
 #define BFLAGALL       0x0F0000        /* all frames dead */
 
 struct game {
-       int moves[BSZ * BSZ];           /* log of all played moves */
+       spot_index moves[BSZ * BSZ];    /* log of all played moves */
        unsigned int nmoves;            /* number of played moves */
-       int winning_spot;
+       spot_index winning_spot;
        int winning_dir;
 };
 
@@ -253,8 +254,8 @@
 void   panic(const char *, ...) __printflike(1, 2) __dead;
 void   debuglog(const char *, ...) __printflike(1, 2);
 void   whatsup(int);
-const char   *stoc(int);
-int    ctos(const char *);
+const char *stoc(spot_index);
+spot_index ctos(const char *);
 int    makemove(int, int);
 void   clearcombo(struct combostr *, int);
 void   markcombo(struct combostr *);
diff -r 1c703e85f9bc -r 3a9cae5fb563 games/gomoku/main.c
--- a/games/gomoku/main.c       Sat May 28 23:05:45 2022 +0000
+++ b/games/gomoku/main.c       Sun May 29 00:12:11 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.66 2022/05/28 23:05:45 rillig Exp $ */
+/*     $NetBSD: main.c,v 1.67 2022/05/29 00:12:11 rillig Exp $ */
 
 /*
  * Copyright (c) 1994
@@ -36,7 +36,7 @@
 __COPYRIGHT("@(#) Copyright (c) 1994\
  The Regents of the University of California.  All rights reserved.");
 /*     @(#)main.c      8.4 (Berkeley) 5/4/95   */
-__RCSID("$NetBSD: main.c,v 1.66 2022/05/28 23:05:45 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.67 2022/05/29 00:12:11 rillig Exp $");
 
 #include <sys/stat.h>
 #include <curses.h>
@@ -222,13 +222,13 @@
        /* NOTREACHED */
 }
 
-static int
+static spot_index
 read_move(void)
 {
 again:
        if (interactive) {
                ask("Select move, (S)ave or (Q)uit.");
-               int s = get_coord();
+               spot_index s = get_coord();
                if (s == SAVE) {
                        save_game();
                        goto again;
diff -r 1c703e85f9bc -r 3a9cae5fb563 games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c   Sat May 28 23:05:45 2022 +0000
+++ b/games/gomoku/pickmove.c   Sun May 29 00:12:11 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pickmove.c,v 1.48 2022/05/28 21:48:21 rillig Exp $     */
+/*     $NetBSD: pickmove.c,v 1.49 2022/05/29 00:12:11 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.48 2022/05/28 21:48:21 rillig Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.49 2022/05/29 00:12:11 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -198,8 +198,8 @@
                return sp->s_nforce[us] > sp1->s_nforce[us];
 
        int them = us != BLACK ? BLACK : WHITE;
-       int s = (int)(sp - board);
-       int s1 = (int)(sp1 - board);
+       spot_index s = (spot_index)(sp - board);
+       spot_index s1 = (spot_index)(sp1 - board);
        if (BIT_TEST(forcemap, s) != BIT_TEST(forcemap, s1))
                return BIT_TEST(forcemap, s);
 
@@ -411,10 +411,10 @@
 
 /*
  * Compute all level 2 combos of frames intersecting spot 'osp'
- * within the frame 'ocbp' and combo value 's'.
+ * within the frame 'ocbp' and combo value 'cv'.
  */
 static void
-makecombo2(struct combostr *ocbp, struct spotstr *osp, int off, int s)
+makecombo2(struct combostr *ocbp, struct spotstr *osp, int off, int cv)
 {
        struct combostr *ncbp;
        int c;
@@ -424,7 +424,7 @@
        char tmp[128];
 
        /* try to combine a new frame with those found so far */
-       ocb.s = s;
+       ocb.s = cv;
        baseB = ocb.cv_force + ocb.cv_win - 1;
        fcnt = ocb.cv_force - 2;
        emask = fcnt != 0 ? ((ocb.cv_win != 0 ? 0x1E : 0x1F) & ~(1 << off)) : 0;
@@ -641,10 +641,10 @@
 
 /*
  * Compute all level N combos of frames intersecting spot 'osp'
- * within the frame 'ocbp' and combo value 's'.
+ * within the frame 'ocbp' and combo value 'cv'.
  */
 static void
-makecombo(struct combostr *ocbp, struct spotstr *osp, int off, int s)
+makecombo(struct combostr *ocbp, struct spotstr *osp, int off, int cv)
 {
        struct combostr *cbp;
        struct spotstr *sp;
@@ -663,14 +663,14 @@
         */
        memset(vertices, 0, sizeof(vertices));
 
-       ocb.s = s;
+       ocb.s = cv;
        baseB = ocb.cv_force + ocb.cv_win - 1;
        fcnt = ocb.cv_force - 2;
        emask = fcnt != 0 ? ((ocb.cv_win != 0 ? 0x1E : 0x1F) & ~(1 << off)) : 0;
        for (struct elist *ep = osp->s_empty; ep != NULL; ep = ep->e_next) {
            /* check for various kinds of overlap */
            cbp = ep->e_combo;
-           verts = checkframes(cbp, ocbp, osp, s, vertices);
+           verts = checkframes(cbp, ocbp, osp, cv, vertices);
            if (verts < 0)
                continue;
 
@@ -802,7 +802,7 @@
        struct combostr *cbp, **cbpp;
        struct elist *ep, *nep;
        struct spotstr *sp;
-       int s, d, m, emask, i;
+       int d, emask, i;
        int nframes;
        char tmp[128];
 
@@ -849,7 +849,7 @@
        ep->e_emask = cbp->c_emask[0];
 
        /* now update the emask info */
-       s = 0;
+       int n = 0;
        for (i = 2, ep += 2; i < nframes; i++, ep++) {
                cbp = ep->e_combo;
                nep = &einfo[ep->e_frameindex];
@@ -857,7 +857,7 @@
                nep->e_emask = cbp->c_emask[0];
 
                if ((cbp->c_flags & C_LOOP) != 0) {
-                       s++;
+                       n++;
                        /*
                         * Account for the fact that this frame connects
                         * to a previous one (thus forming a loop).
@@ -874,7 +874,7 @@
         * We only need to update the emask values of "complete" loops
         * to include the intersection spots.
         */
-       if (s != 0 && ocbp->c_combo.cv_force == 2) {
+       if (n != 0 && ocbp->c_combo.cv_force == 2) {
                /* process loops from the top down */
                ep = &einfo[nframes];
                do {
@@ -909,7 +909,7 @@
                cbp = *cbpp;
                sp = &board[cbp->c_vertex];
                d = dd[cbp->c_dir];
-               for (s = 0, m = 1; s < 5; s++, sp += d, m <<= 1) {
+               for (int off = 0, m = 1; off < 5; off++, sp += d, m <<= 1) {
                        if (sp->s_occ != EMPTY || (emask & m) == 0)
                                continue;
 
@@ -918,7 +918,7 @@
                        if (nep == NULL)
                                panic("Out of memory!");
                        nep->e_combo = ocbp;
-                       nep->e_off = s;
+                       nep->e_off = off;
                        nep->e_frameindex = i;
                        if (ep->e_framecnt > 1) {
                                nep->e_framecnt = ep->e_framecnt - 1;
@@ -983,7 +983,7 @@
                        }
                } else {
                        /* update the board values for each spot in frame */
-                       int s = tcbp->c_vertex;
+                       spot_index s = tcbp->c_vertex;
                        struct spotstr *sp = &board[s];
                        int d = dd[tcbp->c_dir];
                        int i = (flags & C_OPEN_1) != 0 ? 6 : 5;
@@ -1008,7 +1008,7 @@
 
        if (color != nextcolor) {
                /* update the board values for each spot in frame */
-               int s = cbp->c_vertex;
+               spot_index s = cbp->c_vertex;
                struct spotstr *sp = &board[s];
                int d = dd[cbp->c_dir];
                int i = (flags & C_OPEN_0) != 0 ? 6 : 5;
@@ -1068,11 +1068,11 @@
  * would form some kind of valid loop. Also return the intersection spots
  * in 'vertices[]' beside the known intersection at spot 'osp'.
  * Return -1 if 'fcbp' should not be combined with 'cbp'.
- * 's' is the combo value for frame 'fcpb'.
+ * 'cv' is the combo value for frame 'fcbp'.
  */
 static int
 checkframes(struct combostr *cbp, struct combostr *fcbp, struct spotstr *osp,
-           int s, struct overlap_info *vertices)



Home | Main Index | Thread Index | Old Index