Module Name: src
Committed By: rillig
Date: Sun May 29 14:37:44 UTC 2022
Modified Files:
src/games/gomoku: bdinit.c bdisp.c gomoku.h main.c makemove.c
pickmove.c
Log Message:
gomoku: don't use 'i' as special-purpose variable name
No binary change.
To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/games/gomoku/bdinit.c
cvs rdiff -u -r1.51 -r1.52 src/games/gomoku/bdisp.c
cvs rdiff -u -r1.50 -r1.51 src/games/gomoku/gomoku.h
cvs rdiff -u -r1.69 -r1.70 src/games/gomoku/main.c
cvs rdiff -u -r1.39 -r1.40 src/games/gomoku/makemove.c
cvs rdiff -u -r1.58 -r1.59 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.33 src/games/gomoku/bdinit.c:1.34
--- src/games/gomoku/bdinit.c:1.33 Sun May 29 14:01:57 2022
+++ src/games/gomoku/bdinit.c Sun May 29 14:37:44 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: bdinit.c,v 1.33 2022/05/29 14:01:57 rillig Exp $ */
+/* $NetBSD: bdinit.c,v 1.34 2022/05/29 14:37:44 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.33 2022/05/29 14:01:57 rillig Exp $");
+__RCSID("$NetBSD: bdinit.c,v 1.34 2022/05/29 14:37:44 rillig Exp $");
#include <string.h>
#include "gomoku.h"
@@ -42,11 +42,11 @@ __RCSID("$NetBSD: bdinit.c,v 1.33 2022/0
static void init_overlap(void);
static void
-init_spot_flags_and_fval(struct spotstr *sp, int i, int j)
+init_spot_flags_and_fval(struct spotstr *sp, int col, int row)
{
sp->s_flags = 0;
- if (j < 5) {
+ if (row < 5) {
/* directions 1, 2, 3 are blocked */
sp->s_flags |= (BFLAG << 1) | (BFLAG << 2) |
(BFLAG << 3);
@@ -56,7 +56,7 @@ init_spot_flags_and_fval(struct spotstr
sp->s_fval[WHITE][1].s = 0x600;
sp->s_fval[WHITE][2].s = 0x600;
sp->s_fval[WHITE][3].s = 0x600;
- } else if (j == 5) {
+ } else if (row == 5) {
/* five spaces, blocked on one side */
sp->s_fval[BLACK][1].s = 0x500;
sp->s_fval[BLACK][2].s = 0x500;
@@ -73,14 +73,14 @@ init_spot_flags_and_fval(struct spotstr
sp->s_fval[WHITE][2].s = 0x401;
sp->s_fval[WHITE][3].s = 0x401;
}
- if (i > (BSZ - 4)) {
+ if (col > (BSZ - 4)) {
/* directions 0, 1 are blocked */
sp->s_flags |= BFLAG | (BFLAG << 1);
sp->s_fval[BLACK][0].s = 0x600;
sp->s_fval[BLACK][1].s = 0x600;
sp->s_fval[WHITE][0].s = 0x600;
sp->s_fval[WHITE][1].s = 0x600;
- } else if (i == (BSZ - 4)) {
+ } else if (col == (BSZ - 4)) {
sp->s_fval[BLACK][0].s = 0x500;
sp->s_fval[WHITE][0].s = 0x500;
/* if direction 1 is not blocked */
@@ -91,12 +91,12 @@ init_spot_flags_and_fval(struct spotstr
} else {
sp->s_fval[BLACK][0].s = 0x401;
sp->s_fval[WHITE][0].s = 0x401;
- if (i < 5) {
+ if (col < 5) {
/* direction 3 is blocked */
sp->s_flags |= (BFLAG << 3);
sp->s_fval[BLACK][3].s = 0x600;
sp->s_fval[WHITE][3].s = 0x600;
- } else if (i == 5 &&
+ } else if (col == 5 &&
(sp->s_flags & (BFLAG << 3)) == 0) {
sp->s_fval[BLACK][3].s = 0x500;
sp->s_fval[WHITE][3].s = 0x500;
@@ -129,7 +129,7 @@ init_board(void)
{
game.nmoves = 0;
- game.winning_spot = 0;
+ game.win_spot = 0;
struct spotstr *sp = board;
for (int i = 0; i < 1 + BSZ + 1; i++, sp++) {
Index: src/games/gomoku/bdisp.c
diff -u src/games/gomoku/bdisp.c:1.51 src/games/gomoku/bdisp.c:1.52
--- src/games/gomoku/bdisp.c:1.51 Sun May 29 00:12:11 2022
+++ src/games/gomoku/bdisp.c Sun May 29 14:37:44 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: bdisp.c,v 1.51 2022/05/29 00:12:11 rillig Exp $ */
+/* $NetBSD: bdisp.c,v 1.52 2022/05/29 14:37:44 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.51 2022/05/29 00:12:11 rillig Exp $");
+__RCSID("$NetBSD: bdisp.c,v 1.52 2022/05/29 14:37:44 rillig Exp $");
#include <curses.h>
#include <string.h>
@@ -98,15 +98,15 @@ bdisp_init(void)
{
/* top and bottom borders */
- for (int i = 1; i < BSZ + 1; i++) {
- mvaddch(scr_y(BSZ + 1), scr_x(i), letters[i]);
- mvaddch(scr_y(0), scr_x(i), letters[i]);
+ for (int col = 1; col <= BSZ; col++) {
+ mvaddch(scr_y(BSZ + 1), scr_x(col), letters[col]);
+ mvaddch(scr_y(0), scr_x(col), letters[col]);
}
/* left and right edges */
- for (int j = BSZ + 1; --j > 0; ) {
- mvprintw(scr_y(j), 0, "%2d", j);
- mvprintw(scr_y(j), scr_x(BSZ) + 2, "%d", j);
+ for (int row = BSZ; row >= 1; row--) {
+ mvprintw(scr_y(row), 0, "%2d", row);
+ mvprintw(scr_y(row), scr_x(BSZ) + 2, "%d", row);
}
bdwho();
@@ -154,9 +154,9 @@ should_highlight(spot_index s)
if (game.nmoves > 0 && game.moves[game.nmoves - 1] == s)
return true;
- if (game.winning_spot != 0)
- for (int i = 0; i < 5; i++)
- if (s == game.winning_spot + i * dd[game.winning_dir])
+ if (game.win_spot != 0)
+ for (int off = 0; off < 5; off++)
+ if (s == game.win_spot + off * dd[game.win_dir])
return true;
return false;
}
@@ -170,9 +170,9 @@ bdisp(void)
int c;
struct spotstr *sp;
- for (int j = BSZ + 1; --j > 0; ) {
- for (int i = 1; i < BSZ + 1; i++) {
- sp = &board[i + j * (BSZ + 1)];
+ for (int row = BSZ + 1; --row > 0; ) {
+ for (int col = 1; col <= BSZ; col++) {
+ sp = &board[PT(col, row)];
if (debug > 1 && sp->s_occ == EMPTY) {
if ((sp->s_flags & IFLAGALL) != 0)
c = '+';
@@ -183,8 +183,8 @@ bdisp(void)
} else
c = pcolor[sp->s_occ];
- move(scr_y(j), scr_x(i));
- if (should_highlight(PT(i, j))) {
+ move(scr_y(row), scr_x(col));
+ if (should_highlight(PT(col, row))) {
attron(A_BOLD);
addch(c);
attroff(A_BOLD);
@@ -208,11 +208,11 @@ bdump(FILE *fp)
/* 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 (int j = BSZ + 1; --j > 0; ) {
- /* left edge */
- fprintf(fp, "%2d ", j);
- for (int i = 1; i < BSZ + 1; i++) {
- sp = &board[i + j * (BSZ + 1)];
+ for (int row = BSZ + 1; --row > 0; ) {
+ fprintf(fp, "%2d ", row); /* left edge */
+
+ for (int col = 1; col <= BSZ; col++) {
+ sp = &board[PT(col, row)];
if (debug > 1 && sp->s_occ == EMPTY) {
if ((sp->s_flags & IFLAGALL) != 0)
c = '+';
@@ -225,8 +225,8 @@ bdump(FILE *fp)
putc(c, fp);
putc(' ', fp);
}
- /* right edge */
- fprintf(fp, "%d\n", j);
+
+ fprintf(fp, "%d\n", row); /* right edge */
}
/* bottom border */
Index: src/games/gomoku/gomoku.h
diff -u src/games/gomoku/gomoku.h:1.50 src/games/gomoku/gomoku.h:1.51
--- src/games/gomoku/gomoku.h:1.50 Sun May 29 13:49:10 2022
+++ src/games/gomoku/gomoku.h Sun May 29 14:37:44 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: gomoku.h,v 1.50 2022/05/29 13:49:10 rillig Exp $ */
+/* $NetBSD: gomoku.h,v 1.51 2022/05/29 14:37:44 rillig Exp $ */
/*
* Copyright (c) 1994
@@ -226,8 +226,8 @@ struct spotstr {
struct game {
unsigned int nmoves; /* number of played moves */
spot_index moves[BSZ * BSZ]; /* log of all played moves */
- spot_index winning_spot;
- direction winning_dir;
+ spot_index win_spot; /* the winning move, or 0 */
+ direction win_dir;
};
extern const char letters[];
Index: src/games/gomoku/main.c
diff -u src/games/gomoku/main.c:1.69 src/games/gomoku/main.c:1.70
--- src/games/gomoku/main.c:1.69 Sun May 29 10:37:21 2022
+++ src/games/gomoku/main.c Sun May 29 14:37:44 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: main.c,v 1.69 2022/05/29 10:37:21 rillig Exp $ */
+/* $NetBSD: main.c,v 1.70 2022/05/29 14:37:44 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.69 2022/05/29 10:37:21 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.70 2022/05/29 14:37:44 rillig Exp $");
#include <sys/stat.h>
#include <curses.h>
@@ -115,8 +115,8 @@ save_game(void)
misclog("cannot create save file");
return;
}
- for (unsigned int i = 0; i < game.nmoves; i++)
- fprintf(fp, "%s\n", stoc(game.moves[i]));
+ for (unsigned int m = 0; m < game.nmoves; m++)
+ fprintf(fp, "%s\n", stoc(game.moves[m]));
fclose(fp);
}
@@ -420,7 +420,8 @@ readinput(FILE *fp)
void
whatsup(int signum __unused)
{
- int i, n, s1, s2, d1, d2;
+ int n, s1, s2, d1, d2, color;
+ spot_index s;
struct spotstr *sp;
FILE *fp;
char *str;
@@ -455,9 +456,9 @@ top:
}
goto top;
case 's': /* suggest a move */
- i = input[1] == 'b' ? BLACK : WHITE;
- debuglog("suggest %c %s", i == BLACK ? 'B' : 'W',
- stoc(pickmove(i)));
+ color = input[1] == 'b' ? BLACK : WHITE;
+ debuglog("suggest %c %s", color == BLACK ? 'B' : 'W',
+ stoc(pickmove(color)));
goto top;
case 'f': /* go forward a move */
board[game.moves[game.nmoves]].s_occ =
@@ -510,16 +511,16 @@ top:
stoc(s2), pdir[d2], overlap[n]);
goto top;
case 'p':
- sp = &board[i = ctos(input + 1)];
- debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i),
+ sp = &board[s = ctos(input + 1)];
+ debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(s),
sp->s_combo[BLACK].s, sp->s_level[BLACK],
sp->s_nforce[BLACK],
sp->s_combo[WHITE].s, sp->s_level[WHITE],
sp->s_nforce[WHITE], sp->s_wval, sp->s_flags);
- debuglog("FB %s %x %x %x %x", stoc(i),
+ debuglog("FB %s %x %x %x %x", stoc(s),
sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
- debuglog("FW %s %x %x %x %x", stoc(i),
+ debuglog("FW %s %x %x %x %x", stoc(s),
sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s,
sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s);
goto top;
@@ -529,7 +530,7 @@ top:
n = *str++ - '0';
else
n = 0;
- sp = &board[i = ctos(str)];
+ sp = &board[ctos(str)];
for (ep = sp->s_empty; ep != NULL; ep = ep->e_next) {
cbp = ep->e_combo;
if (n != 0) {
Index: src/games/gomoku/makemove.c
diff -u src/games/gomoku/makemove.c:1.39 src/games/gomoku/makemove.c:1.40
--- src/games/gomoku/makemove.c:1.39 Sun May 29 14:01:57 2022
+++ src/games/gomoku/makemove.c Sun May 29 14:37:44 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: makemove.c,v 1.39 2022/05/29 14:01:57 rillig Exp $ */
+/* $NetBSD: makemove.c,v 1.40 2022/05/29 14:37:44 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.39 2022/05/29 14:01:57 rillig Exp $");
+__RCSID("$NetBSD: makemove.c,v 1.40 2022/05/29 14:37:44 rillig Exp $");
#include "gomoku.h"
@@ -155,8 +155,8 @@ makemove(int us, spot_index mv)
/* check for game over */
if (n == 5) {
- game.winning_spot = (spot_index)(fsp - board);
- game.winning_dir = r;
+ game.win_spot = (spot_index)(fsp - board);
+ game.win_dir = r;
return WIN;
}
@@ -234,7 +234,7 @@ makemove(int us, spot_index mv)
static void
update_overlap_same_direction(spot_index s1, spot_index s2,
- frame_index a, int d, int i_minus_f,
+ frame_index a, int d, int off_minus_f,
direction r)
{
/*
@@ -244,7 +244,7 @@ update_overlap_same_direction(spot_index
int n = 0;
spot_index s = s1;
spot_index es = 0;
- for (int b = i_minus_f; b < 5; b++, s += d) {
+ for (int b = off_minus_f; b < 5; b++, s += d) {
if (board[s].s_occ == EMPTY) {
es = s; /* save the intersection point */
n++;
@@ -287,8 +287,8 @@ update_overlap_different_direction(spot_
{
int db = dd[rb];
- for (int i = 0; i < 6; i++) {
- const struct spotstr *sp = &board[os - db * i];
+ for (int off = 0; off < 6; off++) {
+ const struct spotstr *sp = &board[os - db * off];
if (sp->s_occ == BORDER)
break;
if ((sp->s_flags & BFLAG << rb) != 0)
@@ -328,13 +328,13 @@ update_overlap(spot_index os)
frame_index a = board[s1].s_frame[r];
spot_index s2 = s1 - d;
- for (int i = f + 1; i < 6; i++, s2 -= d) {
+ for (int off = f + 1; off < 6; off++, s2 -= d) {
if (board[s2].s_occ == BORDER)
break;
if ((board[s2].s_flags & BFLAG << r) != 0)
continue;
- update_overlap_same_direction(s1, s2, a, d, i - f, r);
+ update_overlap_same_direction(s1, s2, a, d, off - f, r);
}
/* the other directions can only intersect at spot 'os' */
Index: src/games/gomoku/pickmove.c
diff -u src/games/gomoku/pickmove.c:1.58 src/games/gomoku/pickmove.c:1.59
--- src/games/gomoku/pickmove.c:1.58 Sun May 29 14:01:57 2022
+++ src/games/gomoku/pickmove.c Sun May 29 14:37:44 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: pickmove.c,v 1.58 2022/05/29 14:01:57 rillig Exp $ */
+/* $NetBSD: pickmove.c,v 1.59 2022/05/29 14:37:44 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.58 2022/05/29 14:01:57 rillig Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.59 2022/05/29 14:37:44 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -256,7 +256,7 @@ scanframes(int color)
struct spotstr *sp;
union comboval *cp;
struct elist *nep;
- int i, r, n;
+ int off, r, n;
union comboval cb;
curcolor = color;
@@ -301,12 +301,12 @@ scanframes(int color)
else if (color != nextcolor)
memset(tmpmap, 0, sizeof(tmpmap));
sp += delta;
- i = 1;
+ off = 1;
} else {
cb.s = cp->s;
- i = 0;
+ off = 0;
}
- for (; i < 5; i++, sp += delta) { /* for each spot */
+ for (; off < 5; off++, sp += delta) { /* for each spot */
if (sp->s_occ != EMPTY)
continue;
if (cp->s < sp->s_combo[color].s) {
@@ -325,13 +325,13 @@ scanframes(int color)
* Try combining other frames that intersect
* at this spot.
*/
- makecombo2(cbp, sp, i, cb.s);
+ makecombo2(cbp, sp, off, cb.s);
}
if (cp->s == 0x101 && color != nextcolor) {
if (nforce == 0)
memcpy(forcemap, tmpmap, sizeof(tmpmap));
else {
- for (i = 0; (unsigned int)i < MAPSZ; i++)
+ for (int i = 0; (unsigned int)i < MAPSZ; i++)
forcemap[i] &= tmpmap[i];
}
}
@@ -558,24 +558,24 @@ addframes(unsigned int level)
struct combostr *cbp, *ecbp;
struct spotstr *fsp;
struct elist *nep;
- int i, r;
+ int r;
struct combostr **cbpp, *pcbp;
union comboval fcb, cb;
curlevel = level;
/* scan for combos at empty spots */
- i = curcolor;
+ int c = curcolor;
for (unsigned pos = PT(BSZ, BSZ + 1); pos-- > PT(1, 1); ) {
struct spotstr *sp = &board[pos];
for (struct elist *ep = sp->s_empty; ep != NULL; ep = nep) {
cbp = ep->e_combo;
- if (cbp->c_combo.s <= sp->s_combo[i].s) {
- if (cbp->c_combo.s != sp->s_combo[i].s) {
- sp->s_combo[i].s = cbp->c_combo.s;
- sp->s_level[i] = cbp->c_nframes;
- } else if (cbp->c_nframes < sp->s_level[i])
- sp->s_level[i] = cbp->c_nframes;
+ if (cbp->c_combo.s <= sp->s_combo[c].s) {
+ if (cbp->c_combo.s != sp->s_combo[c].s) {
+ sp->s_combo[c].s = cbp->c_combo.s;
+ sp->s_level[c] = cbp->c_nframes;
+ } else if (cbp->c_nframes < sp->s_level[c])
+ sp->s_level[c] = cbp->c_nframes;
}
nep = ep->e_next;
free(ep);
@@ -621,10 +621,10 @@ addframes(unsigned int level)
*/
int d = dd[r];
struct spotstr *sp = fsp + d;
- for (i = 1; i < 5; i++, sp += d) {
+ for (int off = 1; off < 5; off++, sp += d) {
if (sp->s_occ != EMPTY)
continue;
- makecombo(cbp, sp, i, fcb.s);
+ makecombo(cbp, sp, off, fcb.s);
}
} while ((cbp = cbp->c_next) != ecbp);
@@ -996,8 +996,8 @@ updatecombo(struct combostr *cbp, int co
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;
- for (; --i >= 0; sp += d, s += d) {
+ int off = (flags & C_OPEN_1) != 0 ? 6 : 5;
+ for (; --off >= 0; sp += d, s += d) {
if (sp->s_occ != EMPTY)
continue;
sp->s_nforce[color]++;
@@ -1021,8 +1021,8 @@ updatecombo(struct combostr *cbp, int co
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;
- for (; --i >= 0; sp += d, s += d) {
+ int off = (flags & C_OPEN_0) != 0 ? 6 : 5;
+ for (; --off >= 0; sp += d, s += d) {
if (sp->s_occ != EMPTY)
continue;
sp->s_nforce[color]++;
@@ -1038,7 +1038,7 @@ updatecombo(struct combostr *cbp, int co
if (nforce == 0)
memcpy(forcemap, tmpmap, sizeof(tmpmap));
else {
- for (i = 0; (unsigned int)i < MAPSZ; i++)
+ for (int i = 0; (unsigned int)i < MAPSZ; i++)
forcemap[i] &= tmpmap[i];
}
nforce++;
@@ -1085,7 +1085,7 @@ checkframes(struct combostr *cbp, struct
int cv, struct overlap_info *vertices)
{
struct combostr *tcbp, *lcbp;
- int i, n, mask, flags, verts, myindex, fcnt;
+ int ovbit, n, mask, flags, verts, myindex, fcnt;
union comboval cb;
u_char *str;
@@ -1100,10 +1100,10 @@ checkframes(struct combostr *cbp, struct
str = &overlap[n];
spot_index *ip = &intersect[n];
/*
- * i == which overlap bit to test based on whether 'fcbp' is
+ * ovbit == which overlap bit to test based on whether 'fcbp' is
* an open or closed frame.
*/
- i = cb.cv_win != 0 ? 2 : 0;
+ ovbit = cb.cv_win != 0 ? 2 : 0;
for (; (tcbp = cbp->c_link[1]) != NULL;
lcbp = cbp, cbp = cbp->c_link[0]) {
if (tcbp == fcbp)
@@ -1113,7 +1113,7 @@ checkframes(struct combostr *cbp, struct
myindex--;
mask = str[tcbp - frames];
flags = cbp->c_flags;
- n = i + ((flags & C_OPEN_1) != 0 ? 1 : 0);
+ n = ovbit + ((flags & C_OPEN_1) != 0 ? 1 : 0);
if ((mask & (1 << n)) != 0) {
/*
* The two frames are not independent if they
@@ -1156,7 +1156,7 @@ checkframes(struct combostr *cbp, struct
verts++;
}
}
- n = i + ((flags & C_OPEN_0) != 0 ? 1 : 0);
+ n = ovbit + ((flags & C_OPEN_0) != 0 ? 1 : 0);
}
if (cbp == fcbp)
return -1; /* fcbp is already included */