Module Name: src Committed By: rillig Date: Fri May 27 19:59:56 UTC 2022
Modified Files: src/games/gomoku: bdinit.c bdisp.c gomoku.h main.c makemove.c pickmove.c Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.21 -r1.22 src/games/gomoku/bdinit.c cvs rdiff -u -r1.46 -r1.47 src/games/gomoku/bdisp.c cvs rdiff -u -r1.39 -r1.40 src/games/gomoku/gomoku.h cvs rdiff -u -r1.58 -r1.59 src/games/gomoku/main.c cvs rdiff -u -r1.20 -r1.21 src/games/gomoku/makemove.c cvs rdiff -u -r1.44 -r1.45 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/bdinit.c diff -u src/games/gomoku/bdinit.c:1.21 src/games/gomoku/bdinit.c:1.22 --- src/games/gomoku/bdinit.c:1.21 Sat May 21 16:39:14 2022 +++ src/games/gomoku/bdinit.c Fri May 27 19:59:56 2022 @@ -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 @@ bdinit(struct spotstr *bp) struct spotstr *sp; struct combostr *cbp; - movenum = 1; + nmoves = 0; /* mark the borders as such */ sp = bp; Index: src/games/gomoku/bdisp.c diff -u src/games/gomoku/bdisp.c:1.46 src/games/gomoku/bdisp.c:1.47 --- src/games/gomoku/bdisp.c:1.46 Sun May 22 13:38:08 2022 +++ src/games/gomoku/bdisp.c Fri May 27 19:59:56 2022 @@ -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 @@ bdisp(void) 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); Index: src/games/gomoku/gomoku.h diff -u src/games/gomoku/gomoku.h:1.39 src/games/gomoku/gomoku.h:1.40 --- src/games/gomoku/gomoku.h:1.39 Sun May 22 10:45:02 2022 +++ src/games/gomoku/gomoku.h Fri May 27 19:59:56 2022 @@ -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 frames[FAREA]; / 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; Index: src/games/gomoku/main.c diff -u src/games/gomoku/main.c:1.58 src/games/gomoku/main.c:1.59 --- src/games/gomoku/main.c:1.58 Sun May 22 09:17:15 2022 +++ src/games/gomoku/main.c Fri May 27 19:59:56 2022 @@ -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 frames[FAREA]; /* stora 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 @@ save_game(void) 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 @@ again: } 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 @@ top: 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 @@ top: 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); } Index: src/games/gomoku/makemove.c diff -u src/games/gomoku/makemove.c:1.20 src/games/gomoku/makemove.c:1.21 --- src/games/gomoku/makemove.c:1.20 Sat May 21 16:39:14 2022 +++ src/games/gomoku/makemove.c Fri May 27 19:59:56 2022 @@ -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 @@ makemove(int us, int mv) /* 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 */ Index: src/games/gomoku/pickmove.c diff -u src/games/gomoku/pickmove.c:1.44 src/games/gomoku/pickmove.c:1.45 --- src/games/gomoku/pickmove.c:1.44 Fri May 27 19:30:56 2022 +++ src/games/gomoku/pickmove.c Fri May 27 19:59:56 2022 @@ -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 int nforce; /* count of opponen 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 @@ pickmove(int us) 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 @@ better(const struct spotstr *sp, const s } 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 @@ scanframes(int color) * 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 @@ makecombo2(struct combostr *ocbp, struct * 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 @@ sortcombo(struct combostr **scbpp, struc { 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 @@ sortcombo(struct combostr **scbpp, struc #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 @@ inserted: char buf[128]; size_t pos; - debuglog("sort1: n%d", n); + debuglog("sort1: n%u", n); pos = 0; for (cpp = scbpp; cpp < scbpp + n; cpp++) { snprintf(buf + pos, sizeof(buf) - pos, " %s%c",