Module Name:    src
Committed By:   rillig
Date:           Thu May 19 18:58:59 UTC 2022

Modified Files:
        src/games/gomoku: bdinit.c bdisp.c gomoku.h pickmove.c

Log Message:
gomoku: de-obfuscate screen coordinate calculation

Modern compilers optimize linear integer arithmetic, so there is no
reason to use strange or misleading formulas.

Replace several magic numbers with proper formulas.

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/games/gomoku/bdinit.c
cvs rdiff -u -r1.30 -r1.31 src/games/gomoku/bdisp.c
cvs rdiff -u -r1.28 -r1.29 src/games/gomoku/gomoku.h
cvs rdiff -u -r1.34 -r1.35 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.16 src/games/gomoku/bdinit.c:1.17
--- src/games/gomoku/bdinit.c:1.16	Wed May 18 22:35:13 2022
+++ src/games/gomoku/bdinit.c	Thu May 19 18:58:59 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: bdinit.c,v 1.16 2022/05/18 22:35:13 rillig Exp $	*/
+/*	$NetBSD: bdinit.c,v 1.17 2022/05/19 18:58:59 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.16 2022/05/18 22:35:13 rillig Exp $");
+__RCSID("$NetBSD: bdinit.c,v 1.17 2022/05/19 18:58:59 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -56,7 +56,7 @@ bdinit(struct spotstr *bp)
 
 	/* mark the borders as such */
 	sp = bp;
-	for (int i = 1 + BSZ + 1; --i >= 0; sp++) {
+	for (int i = 0; i < 1 + BSZ + 1; i++, sp++) {
 		sp->s_occ = BORDER;			/* top border */
 		sp->s_flags = BFLAGALL;
 	}
@@ -144,7 +144,7 @@ bdinit(struct spotstr *bp)
 	}
 
 	/* mark the borders as such */
-	for (int i = BSZ + 1; --i >= 0; sp++) {
+	for (int i = 0; i < BSZ + 1; i++, sp++) {
 		sp->s_occ = BORDER;			/* bottom border */
 		sp->s_flags = BFLAGALL;
 	}

Index: src/games/gomoku/bdisp.c
diff -u src/games/gomoku/bdisp.c:1.30 src/games/gomoku/bdisp.c:1.31
--- src/games/gomoku/bdisp.c:1.30	Thu May 19 17:02:51 2022
+++ src/games/gomoku/bdisp.c	Thu May 19 18:58:59 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: bdisp.c,v 1.30 2022/05/19 17:02:51 rillig Exp $	*/
+/*	$NetBSD: bdisp.c,v 1.31 2022/05/19 18:58:59 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.30 2022/05/19 17:02:51 rillig Exp $");
+__RCSID("$NetBSD: bdisp.c,v 1.31 2022/05/19 18:58:59 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -53,6 +53,9 @@ __RCSID("$NetBSD: bdisp.c,v 1.30 2022/05
 static	int	lastline;
 static	char	pcolor[] = "*O.?";
 
+#define	scr_y(by)	(1 + (BSZ - 1) - ((by) - 1))
+#define	scr_x(bx)	(3 + 2 * ((bx) - 1))
+
 /*
  * Initialize screen display.
  */
@@ -101,23 +104,23 @@ bdisp_init(void)
 
 	/* top border */
 	for (int i = 1; i < BSZ + 1; i++) {
-		move(0, 2 * i + 1);
+		move(scr_y(BSZ + 1), scr_x(i));
 		addch(letters[i]);
 	}
 	/* left and right edges */
 	for (int j = BSZ + 1; --j > 0; ) {
-		move(20 - j, 0);
+		move(scr_y(j), 0);
 		printw("%2d ", j);
-		move(20 - j, 2 * (BSZ + 1) + 1);
+		move(scr_y(j), scr_x(BSZ) + 2);
 		printw("%d ", j);
 	}
 	/* bottom border */
 	for (int i = 1; i < BSZ + 1; i++) {
-		move(20, 2 * i + 1);
+		move(scr_y(0), scr_x(i));
 		addch(letters[i]);
 	}
 	bdwho(false);
-	move(0, 47);
+	move(0, TRANSCRIPT_COL + 1);
 	addstr("#  black  white");
 	lastline = 0;
 	bdisp();
@@ -131,16 +134,17 @@ bdwho(bool update)
 {
 	int i, j;
 
-	move(21, 0);
+	move(BSZ + 2, 0);
 	printw("                                              ");
 	i = (int)strlen(plyr[BLACK]);
 	j = (int)strlen(plyr[WHITE]);
 	if (i + j <= 20) {
-		move(21, 10 - (i + j) / 2);
+		/* TODO: properly center the text when i + j is even. */
+		move(BSZ + 2, 10 - (i + j) / 2);
 		printw("BLACK/%s (*) vs. WHITE/%s (O)",
 		    plyr[BLACK], plyr[WHITE]);
 	} else {
-		move(21, 0);
+		move(BSZ + 2, 0);
 		if (i <= 10) {
 			j = 20 - i;
 		} else if (j <= 10) {
@@ -166,7 +170,7 @@ bdisp(void)
 
 	for (int j = BSZ + 1; --j > 0; ) {
 		for (int i = 1; i < BSZ + 1; i++) {
-			move(BSZ + 1 - j, 2 * i + 1);
+			move(scr_y(j), scr_x(i));
 			sp = &board[i + j * (BSZ + 1)];
 			if (debug > 1 && sp->s_occ == EMPTY) {
 				if ((sp->s_flags & IFLAGALL) != 0)
@@ -331,18 +335,20 @@ get_line(char *buf, int size)
 int
 get_coord(void)
 {
+	/* XXX: These coordinates are 0-based, all others are 1-based. */
 	static int curx = BSZ / 2;
 	static int cury = BSZ / 2;
 	int ny, nx, ch;
 
-	BGOTO(cury, curx);
+	move(scr_y(cury + 1), scr_x(curx + 1));
 	refresh();
 	nx = curx;
 	ny = cury;
 	for (;;) {
+		/* TODO: replace with 'letters[curx + 1]' */
 		mvprintw(BSZ + 3, (BSZ - 6) / 2, "(%c %d) ",
 		    'A' + ((curx > 7) ? (curx + 1) : curx), cury + 1);
-		BGOTO(cury, curx);
+		move(scr_y(cury + 1), scr_x(curx + 1));
 
 		ch = getch();
 		switch (ch) {

Index: src/games/gomoku/gomoku.h
diff -u src/games/gomoku/gomoku.h:1.28 src/games/gomoku/gomoku.h:1.29
--- src/games/gomoku/gomoku.h:1.28	Mon May 16 21:48:45 2022
+++ src/games/gomoku/gomoku.h	Thu May 19 18:58:59 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: gomoku.h,v 1.28 2022/05/16 21:48:45 rillig Exp $	*/
+/*	$NetBSD: gomoku.h,v 1.29 2022/05/19 18:58:59 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -43,15 +43,10 @@
 #define BSZ	19
 #define BAREA	((1 + BSZ + 1) * (BSZ + 1) + 1)
 
-#define TRANSCRIPT_COL	(2 * (BSZ + 4))
-
-/* interactive curses stuff */
-#define BGOTO(y, x)	move(BSZ - (y), 2 * (x) + 3)
+#define TRANSCRIPT_COL	(3 + (2 * BSZ - 1) + 3 + 3)
 
 /* frame dimensions (based on 5 in a row) */
-#define FSZ1	BSZ
-#define FSZ2	(BSZ-4)
-#define FAREA	(FSZ1*FSZ2 + FSZ2*FSZ2 + FSZ1*FSZ2 + FSZ2*FSZ2)
+#define FAREA	(2 * BSZ * (BSZ - 4) + 2 * (BSZ - 4) * (BSZ - 4))
 
 #define MUP	(BSZ + 1)
 #define MDOWN	(-(BSZ + 1))
@@ -120,8 +115,6 @@
  *	complete which takes fewer or the same number of moves to win).
  */
 
-#define MAXA		6
-#define MAXB		2
 #define MAXCOMBO	0x600
 
 union comboval {

Index: src/games/gomoku/pickmove.c
diff -u src/games/gomoku/pickmove.c:1.34 src/games/gomoku/pickmove.c:1.35
--- src/games/gomoku/pickmove.c:1.34	Wed May 18 22:30:19 2022
+++ src/games/gomoku/pickmove.c	Thu May 19 18:58:59 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: pickmove.c,v 1.34 2022/05/18 22:30:19 rillig Exp $	*/
+/*	$NetBSD: pickmove.c,v 1.35 2022/05/19 18:58:59 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)pickmove.c	8.2 (Berkeley) 5/3/95";
 #else
-__RCSID("$NetBSD: pickmove.c,v 1.34 2022/05/18 22:30:19 rillig Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.35 2022/05/19 18:58:59 rillig Exp $");
 #endif
 #endif /* not lint */
 
@@ -450,7 +450,7 @@ makecombo2(struct combostr *ocbp, struct
 
 		/* don't include frames of the wrong color */
 		fcb.s = fsp->s_fval[curcolor][r].s;
-		if (fcb.c.a >= MAXA)
+		if (fcb.c.a >= 6)
 		    continue;
 
 		/*

Reply via email to