Source-Changes-HG archive

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

[src/trunk]: src/games/gomoku gomoku: de-obfuscate screen coordinate calculation



details:   https://anonhg.NetBSD.org/src/rev/e7483c000f45
branches:  trunk
changeset: 366235:e7483c000f45
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu May 19 18:58:59 2022 +0000

description:
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.

diffstat:

 games/gomoku/bdinit.c   |   8 ++++----
 games/gomoku/bdisp.c    |  32 +++++++++++++++++++-------------
 games/gomoku/gomoku.h   |  13 +++----------
 games/gomoku/pickmove.c |   6 +++---
 4 files changed, 29 insertions(+), 30 deletions(-)

diffs (209 lines):

diff -r f9758368e90c -r e7483c000f45 games/gomoku/bdinit.c
--- a/games/gomoku/bdinit.c     Thu May 19 17:24:14 2022 +0000
+++ b/games/gomoku/bdinit.c     Thu May 19 18:58:59 2022 +0000
@@ -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 @@
 
        /* 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 @@
        }
 
        /* 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;
        }
diff -r f9758368e90c -r e7483c000f45 games/gomoku/bdisp.c
--- a/games/gomoku/bdisp.c      Thu May 19 17:24:14 2022 +0000
+++ b/games/gomoku/bdisp.c      Thu May 19 18:58:59 2022 +0000
@@ -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 @@
 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 @@
 
        /* 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 @@
 {
        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 @@
 
        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 @@
 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) {
diff -r f9758368e90c -r e7483c000f45 games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Thu May 19 17:24:14 2022 +0000
+++ b/games/gomoku/gomoku.h     Thu May 19 18:58:59 2022 +0000
@@ -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 {
diff -r f9758368e90c -r e7483c000f45 games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c   Thu May 19 17:24:14 2022 +0000
+++ b/games/gomoku/pickmove.c   Thu May 19 18:58:59 2022 +0000
@@ -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 @@
 
                /* 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;
 
                /*



Home | Main Index | Thread Index | Old Index