Source-Changes-HG archive

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

[src/trunk]: src/games/gomoku gomoku: replace 1-based movenum with 0-based nm...



details:   https://anonhg.NetBSD.org/src/rev/5b768c73dbbd
branches:  trunk
changeset: 366449:5b768c73dbbd
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri May 27 19:59:56 2022 +0000

description:
gomoku: replace 1-based movenum with 0-based nmoves

No functional change, not even the TIE that is wrongly announced when
the very last spot on the board is yet to be filled by Black.  Even
without this off-by-one error, it could be that filling the very last
spot completes a frame, so that code has been wrong all the time.

In practical terms, this situation only arises when the human player is
unconcentrated or the computer player has a bad strategy.  The latter
may well be, as the computer moves in the (boring) endgame are not
directed towards winning -- they fill irrelevant spots before relevant
ones.

diffstat:

 games/gomoku/bdinit.c   |   6 +++---
 games/gomoku/bdisp.c    |   6 +++---
 games/gomoku/gomoku.h   |   6 +++---
 games/gomoku/main.c     |  38 +++++++++++++++++++-------------------
 games/gomoku/makemove.c |   8 ++++----
 games/gomoku/pickmove.c |  26 +++++++++++++-------------
 6 files changed, 45 insertions(+), 45 deletions(-)

diffs (truncated from 303 to 300 lines):

diff -r 3e7400d782c1 -r 5b768c73dbbd games/gomoku/bdinit.c
--- a/games/gomoku/bdinit.c     Fri May 27 19:30:56 2022 +0000
+++ b/games/gomoku/bdinit.c     Fri May 27 19:59:56 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdinit.c,v 1.21 2022/05/21 16:39:14 rillig Exp $       */
+/*     $NetBSD: bdinit.c,v 1.22 2022/05/27 19:59:56 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.21 2022/05/21 16:39:14 rillig Exp $");
+__RCSID("$NetBSD: bdinit.c,v 1.22 2022/05/27 19:59:56 rillig Exp $");
 
 #include <string.h>
 #include "gomoku.h"
@@ -47,7 +47,7 @@
        struct spotstr *sp;
        struct combostr *cbp;
 
-       movenum = 1;
+       nmoves = 0;
 
        /* mark the borders as such */
        sp = bp;
diff -r 3e7400d782c1 -r 5b768c73dbbd games/gomoku/bdisp.c
--- a/games/gomoku/bdisp.c      Fri May 27 19:30:56 2022 +0000
+++ b/games/gomoku/bdisp.c      Fri May 27 19:59:56 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdisp.c,v 1.46 2022/05/22 13:38:08 rillig Exp $        */
+/*     $NetBSD: bdisp.c,v 1.47 2022/05/27 19:59:56 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.46 2022/05/22 13:38:08 rillig Exp $");
+__RCSID("$NetBSD: bdisp.c,v 1.47 2022/05/27 19:59:56 rillig Exp $");
 
 #include <curses.h>
 #include <string.h>
@@ -171,7 +171,7 @@
                                c = pcolor[sp->s_occ];
 
                        move(scr_y(j), scr_x(i));
-                       if (movenum > 1 && movelog[movenum - 2] == PT(i, j)) {
+                       if (nmoves > 0 && movelog[nmoves - 1] == PT(i, j)) {
                                attron(A_BOLD);
                                addch(c);
                                attroff(A_BOLD);
diff -r 3e7400d782c1 -r 5b768c73dbbd games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Fri May 27 19:30:56 2022 +0000
+++ b/games/gomoku/gomoku.h     Fri May 27 19:59:56 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gomoku.h,v 1.39 2022/05/22 10:45:02 rillig Exp $       */
+/*     $NetBSD: gomoku.h,v 1.40 2022/05/27 19:59:56 rillig Exp $       */
 
 /*
  * Copyright (c) 1994
@@ -229,8 +229,8 @@
 extern struct  combostr *sortframes[2];        /* sorted, non-empty frames */
 extern u_char  overlap[FAREA * FAREA];         /* frame [a][b] overlap */
 extern short   intersect[FAREA * FAREA];       /* frame [a][b] intersection */
-extern int     movelog[BSZ * BSZ];             /* history of moves */
-extern int     movenum;
+extern int     movelog[BSZ * BSZ];
+extern unsigned int nmoves;
 extern int     debug;
 
 extern bool interactive;
diff -r 3e7400d782c1 -r 5b768c73dbbd games/gomoku/main.c
--- a/games/gomoku/main.c       Fri May 27 19:30:56 2022 +0000
+++ b/games/gomoku/main.c       Fri May 27 19:59:56 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.58 2022/05/22 09:17:15 rillig Exp $ */
+/*     $NetBSD: main.c,v 1.59 2022/05/27 19:59:56 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.58 2022/05/22 09:17:15 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.59 2022/05/27 19:59:56 rillig Exp $");
 
 #include <sys/stat.h>
 #include <curses.h>
@@ -78,8 +78,8 @@
 struct combostr *sortframes[2];        /* sorted list of non-empty frames */
 u_char overlap[FAREA * FAREA];         /* true if frame [a][b] overlap */
 short  intersect[FAREA * FAREA];       /* frame [a][b] intersection */
-int    movelog[BSZ * BSZ];             /* log of all the moves */
-int    movenum;                        /* current move number */
+int    movelog[BSZ * BSZ];             /* log of all played moves */
+unsigned int nmoves;                   /* number of played moves */
 const char *plyr[2] = { "???", "???" };        /* who's who */
 
 static int readinput(FILE *);
@@ -115,7 +115,7 @@
                misclog("cannot create save file");
                return;
        }
-       for (int i = 0; i < movenum - 1; i++)
+       for (unsigned int i = 0; i < nmoves; i++)
                fprintf(fp, "%s\n", stoc(movelog[i]));
        fclose(fp);
 }
@@ -311,8 +311,8 @@
        }
 
        if (interactive && curmove != ILLEGAL) {
-               misclog("%3d%*s%-6s",
-                   movenum, color == BLACK ? 2 : 9, "", stoc(curmove));
+               misclog("%3u%*s%-6s",
+                   nmoves + 1, color == BLACK ? 2 : 9, "", stoc(curmove));
        }
 
        if ((outcome = makemove(color, curmove)) != MOVEOK)
@@ -447,9 +447,9 @@
        case 'c':
                break;
        case 'b':               /* back up a move */
-               if (movenum > 1) {
-                       movenum--;
-                       board[movelog[movenum - 1]].s_occ = EMPTY;
+               if (nmoves > 0) {
+                       nmoves--;
+                       board[movelog[nmoves]].s_occ = EMPTY;
                        bdisp();
                }
                goto top;
@@ -459,23 +459,23 @@
                        stoc(pickmove(i)));
                goto top;
        case 'f':               /* go forward a move */
-               board[movelog[movenum - 1]].s_occ =
-                   (movenum & 1) != 0 ? BLACK : WHITE;
-               movenum++;
+               board[movelog[nmoves]].s_occ =
+                   nmoves % 2 == 0 ? BLACK : WHITE;
+               nmoves++;
                bdisp();
                goto top;
        case 'l':               /* print move history */
                if (input[1] == '\0') {
-                       for (i = 0; i < movenum - 1; i++)
-                               debuglog("%s", stoc(movelog[i]));
+                       for (unsigned int m = 0; m < nmoves; m++)
+                               debuglog("%s", stoc(movelog[m]));
                        goto top;
                }
                if ((fp = fopen(input + 1, "w")) == NULL)
                        goto top;
-               for (i = 0; i < movenum - 1; i++) {
-                       fprintf(fp, "%s", stoc(movelog[i]));
-                       if (++i < movenum - 1)
-                               fprintf(fp, " %s\n", stoc(movelog[i]));
+               for (unsigned int m = 0; m < nmoves; m++) {
+                       fprintf(fp, "%s", stoc(movelog[m]));
+                       if (++m < nmoves)
+                               fprintf(fp, " %s\n", stoc(movelog[m]));
                        else
                                fputc('\n', fp);
                }
diff -r 3e7400d782c1 -r 5b768c73dbbd games/gomoku/makemove.c
--- a/games/gomoku/makemove.c   Fri May 27 19:30:56 2022 +0000
+++ b/games/gomoku/makemove.c   Fri May 27 19:59:56 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makemove.c,v 1.20 2022/05/21 16:39:14 rillig Exp $     */
+/*     $NetBSD: makemove.c,v 1.21 2022/05/27 19:59:56 rillig Exp $     */
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*     @(#)makemove.c  8.2 (Berkeley) 5/3/95   */
-__RCSID("$NetBSD: makemove.c,v 1.20 2022/05/21 16:39:14 rillig Exp $");
+__RCSID("$NetBSD: makemove.c,v 1.21 2022/05/27 19:59:56 rillig Exp $");
 
 #include "gomoku.h"
 
@@ -78,8 +78,8 @@
 
        /* make move */
        sp->s_occ = us;
-       movelog[movenum - 1] = mv;
-       if (++movenum == BSZ * BSZ)
+       movelog[nmoves++] = mv;
+       if (nmoves + 1 == BSZ * BSZ)    /* FIXME: off-by-one */
                return TIE;
 
        /* compute new frame values */
diff -r 3e7400d782c1 -r 5b768c73dbbd games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c   Fri May 27 19:30:56 2022 +0000
+++ b/games/gomoku/pickmove.c   Fri May 27 19:59:56 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pickmove.c,v 1.44 2022/05/27 19:30:56 rillig Exp $     */
+/*     $NetBSD: pickmove.c,v 1.45 2022/05/27 19:59:56 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.44 2022/05/27 19:30:56 rillig Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.45 2022/05/27 19:59:56 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -72,7 +72,7 @@
 static bool better(const struct spotstr *, const struct spotstr *, int);
 static void scanframes(int);
 static void makecombo2(struct combostr *, struct spotstr *, int, int);
-static void addframes(int);
+static void addframes(unsigned int);
 static void makecombo(struct combostr *, struct spotstr *, int, int);
 static void appendcombo(struct combostr *, int);
 static void updatecombo(struct combostr *, int);
@@ -92,7 +92,7 @@
        int m;
 
        /* first move is easy */
-       if (movenum == 1)
+       if (nmoves == 0)
                return PT((BSZ + 1) / 2, (BSZ + 1) / 2);
 
        /* initialize all the board values */
@@ -218,7 +218,7 @@
 }
 
 static int curcolor;   /* implicit parameter to makecombo() */
-static int curlevel;   /* implicit parameter to makecombo() */
+static unsigned int curlevel;  /* implicit parameter to makecombo() */
 
 /*
  * Scan the sorted list of non-empty frames and
@@ -335,12 +335,12 @@
         * Limit the search depth early in the game.
         */
        /* LINTED 117: bitwise '>>' on signed value possibly nonportable */
-       for (int level = 2;
-            level <= ((movenum + 1) >> 1) && combolen > n; level++) {
+       for (unsigned int level = 2;
+            level <= 1 + nmoves / 2 && combolen > n; level++) {
                if (level >= 9)
                        break;  /* Do not think too long. */
                if (debug != 0) {
-                       debuglog("%cL%d %d %d %d", "BW"[color],
+                       debuglog("%cL%u %d %d %d", "BW"[color],
                            level, combolen - n, combocnt, elistcnt);
                        refresh();
                }
@@ -545,7 +545,7 @@
  * combinations of 'level' number of frames.
  */
 static void
-addframes(int level)
+addframes(unsigned int level)
 {
        struct combostr *cbp, *ecbp;
        struct spotstr *sp, *fsp;
@@ -1213,14 +1213,14 @@
 {
        struct combostr **spp, **cpp;
        struct combostr *cbp, *ecbp;
-       int n, inx;
+       int inx;
 
 #ifdef DEBUG
        if (debug > 3) {
                char buf[128];
                size_t pos;
 
-               debuglog("sortc: %s%c l%d", stoc(fcbp->c_vertex),
+               debuglog("sortc: %s%c l%u", stoc(fcbp->c_vertex),
                        pdir[fcbp->c_dir], curlevel);
                pos = 0;
                for (cpp = cbpp; cpp < cbpp + curlevel; cpp++) {
@@ -1233,7 +1233,7 @@
 #endif /* DEBUG */
 
        /* first build the new sorted list */
-       n = curlevel + 1;
+       unsigned int n = curlevel + 1;
        spp = scbpp + n;
        cpp = cbpp + curlevel;
        do {
@@ -1278,7 +1278,7 @@
                        char buf[128];
                        size_t pos;
 
-                       debuglog("sort1: n%d", n);
+                       debuglog("sort1: n%u", n);



Home | Main Index | Thread Index | Old Index