Source-Changes-HG archive

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

[src/trunk]: src/games/gomoku gomoku: reduce usage of magic numbers in the code



details:   https://anonhg.NetBSD.org/src/rev/90702cf85475
branches:  trunk
changeset: 367249:90702cf85475
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jun 19 10:23:48 2022 +0000

description:
gomoku: reduce usage of magic numbers in the code

No binary change.

diffstat:

 games/gomoku/bdinit.c   |  91 +++++++++++++++++++++++-------------------------
 games/gomoku/gomoku.h   |  18 +++++++++-
 games/gomoku/makemove.c |  16 ++++----
 3 files changed, 69 insertions(+), 56 deletions(-)

diffs (240 lines):

diff -r 017be3d21c05 -r 90702cf85475 games/gomoku/bdinit.c
--- a/games/gomoku/bdinit.c     Sun Jun 19 04:47:12 2022 +0000
+++ b/games/gomoku/bdinit.c     Sun Jun 19 10:23:48 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdinit.c,v 1.35 2022/05/29 16:30:44 rillig Exp $       */
+/*     $NetBSD: bdinit.c,v 1.36 2022/06/19 10:23:48 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.35 2022/05/29 16:30:44 rillig Exp $");
+__RCSID("$NetBSD: bdinit.c,v 1.36 2022/06/19 10:23:48 rillig Exp $");
 
 #include <string.h>
 #include "gomoku.h"
@@ -47,59 +47,56 @@
 
        sp->s_flags = 0;
        if (row < 5) {
-               /* directions 1, 2, 3 are blocked */
-               sp->s_flags |= (BFLAG << 1) | (BFLAG << 2) |
-                   (BFLAG << 3);
-               sp->s_fval[BLACK][1].s = 0x600;
-               sp->s_fval[BLACK][2].s = 0x600;
-               sp->s_fval[BLACK][3].s = 0x600;
-               sp->s_fval[WHITE][1].s = 0x600;
-               sp->s_fval[WHITE][2].s = 0x600;
-               sp->s_fval[WHITE][3].s = 0x600;
+               set_blocked(sp, DIR_DR);
+               set_blocked(sp, DIR_D_);
+               set_blocked(sp, DIR_DL);
+               sp->s_fval[BLACK][DIR_DR].s = 0x600;
+               sp->s_fval[BLACK][DIR_D_].s = 0x600;
+               sp->s_fval[BLACK][DIR_DL].s = 0x600;
+               sp->s_fval[WHITE][DIR_DR].s = 0x600;
+               sp->s_fval[WHITE][DIR_D_].s = 0x600;
+               sp->s_fval[WHITE][DIR_DL].s = 0x600;
        } else if (row == 5) {
                /* five spaces, blocked on one side */
-               sp->s_fval[BLACK][1].s = 0x500;
-               sp->s_fval[BLACK][2].s = 0x500;
-               sp->s_fval[BLACK][3].s = 0x500;
-               sp->s_fval[WHITE][1].s = 0x500;
-               sp->s_fval[WHITE][2].s = 0x500;
-               sp->s_fval[WHITE][3].s = 0x500;
+               sp->s_fval[BLACK][DIR_DR].s = 0x500;
+               sp->s_fval[BLACK][DIR_D_].s = 0x500;
+               sp->s_fval[BLACK][DIR_DL].s = 0x500;
+               sp->s_fval[WHITE][DIR_DR].s = 0x500;
+               sp->s_fval[WHITE][DIR_D_].s = 0x500;
+               sp->s_fval[WHITE][DIR_DL].s = 0x500;
        } else {
                /* six spaces, not blocked */
-               sp->s_fval[BLACK][1].s = 0x401;
-               sp->s_fval[BLACK][2].s = 0x401;
-               sp->s_fval[BLACK][3].s = 0x401;
-               sp->s_fval[WHITE][1].s = 0x401;
-               sp->s_fval[WHITE][2].s = 0x401;
-               sp->s_fval[WHITE][3].s = 0x401;
+               sp->s_fval[BLACK][DIR_DR].s = 0x401;
+               sp->s_fval[BLACK][DIR_D_].s = 0x401;
+               sp->s_fval[BLACK][DIR_DL].s = 0x401;
+               sp->s_fval[WHITE][DIR_DR].s = 0x401;
+               sp->s_fval[WHITE][DIR_D_].s = 0x401;
+               sp->s_fval[WHITE][DIR_DL].s = 0x401;
        }
        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;
+               set_blocked(sp, DIR__R);
+               set_blocked(sp, DIR_DR);
+               sp->s_fval[BLACK][DIR__R].s = 0x600;
+               sp->s_fval[BLACK][DIR_DR].s = 0x600;
+               sp->s_fval[WHITE][DIR__R].s = 0x600;
+               sp->s_fval[WHITE][DIR_DR].s = 0x600;
        } 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 */
-               if ((sp->s_flags & (BFLAG << 1)) == 0) {
-                       sp->s_fval[BLACK][1].s = 0x500;
-                       sp->s_fval[WHITE][1].s = 0x500;
+               sp->s_fval[BLACK][DIR__R].s = 0x500;
+               sp->s_fval[WHITE][DIR__R].s = 0x500;
+               if (!is_blocked(sp, DIR_DR)) {
+                       sp->s_fval[BLACK][DIR_DR].s = 0x500;
+                       sp->s_fval[WHITE][DIR_DR].s = 0x500;
                }
        } else {
-               sp->s_fval[BLACK][0].s = 0x401;
-               sp->s_fval[WHITE][0].s = 0x401;
+               sp->s_fval[BLACK][DIR__R].s = 0x401;
+               sp->s_fval[WHITE][DIR__R].s = 0x401;
                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 (col == 5 &&
-                   (sp->s_flags & (BFLAG << 3)) == 0) {
-                       sp->s_fval[BLACK][3].s = 0x500;
-                       sp->s_fval[WHITE][3].s = 0x500;
+                       set_blocked(sp, DIR_DL);
+                       sp->s_fval[BLACK][DIR_DL].s = 0x600;
+                       sp->s_fval[WHITE][DIR_DL].s = 0x600;
+               } else if (col == 5 && !is_blocked(sp, DIR_DL)) {
+                       sp->s_fval[BLACK][DIR_DL].s = 0x500;
+                       sp->s_fval[WHITE][DIR_DL].s = 0x500;
                }
        }
 }
@@ -110,7 +107,7 @@
 {
 
        for (direction r = 4; r-- > 0; ) {
-               if ((sp->s_flags & (BFLAG << r)) != 0)
+               if (is_blocked(sp, r))
                        continue;
 
                frame_index fi = (*fip)++;
@@ -247,7 +244,7 @@
                        const struct spotstr *spb0 = &board[s - offb * db];
                        if (spb0->s_occ == BORDER)
                                break;
-                       if ((spb0->s_flags & BFLAG << rb) != 0)
+                       if (is_blocked(spb0, rb))
                                continue;
 
                        frame_index fib = spb0->s_frame[rb];
diff -r 017be3d21c05 -r 90702cf85475 games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Sun Jun 19 04:47:12 2022 +0000
+++ b/games/gomoku/gomoku.h     Sun Jun 19 10:23:48 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gomoku.h,v 1.55 2022/05/29 17:01:42 rillig Exp $       */
+/*     $NetBSD: gomoku.h,v 1.56 2022/06/19 10:23:48 rillig Exp $       */
 
 /*
  * Copyright (c) 1994
@@ -200,6 +200,10 @@
 
 /* 0 = right, 1 = down right, 2 = down, 3 = down left. */
 typedef unsigned char direction;
+#define DIR__R         0               /* right */
+#define DIR_DR         1               /* down right */
+#define DIR_D_         2               /* down */
+#define DIR_DL         3               /* down left */
 
 /*
  * One spot structure for each location on the board.
@@ -231,6 +235,18 @@
 #define BFLAG          0x010000        /* frame intersects border or dead */
 #define BFLAGALL       0x0F0000        /* all frames dead */
 
+static inline bool
+is_blocked(const struct spotstr *sp, direction r)
+{
+       return (sp->s_flags & (BFLAG << r)) != 0;
+}
+
+static inline void
+set_blocked(struct spotstr *sp, direction r)
+{
+       sp->s_flags |= BFLAG << r;
+}
+
 struct game {
        unsigned int    nmoves;         /* number of played moves */
        spot_index      moves[BSZ * BSZ]; /* log of all played moves */
diff -r 017be3d21c05 -r 90702cf85475 games/gomoku/makemove.c
--- a/games/gomoku/makemove.c   Sun Jun 19 04:47:12 2022 +0000
+++ b/games/gomoku/makemove.c   Sun Jun 19 10:23:48 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makemove.c,v 1.42 2022/05/29 17:01:42 rillig Exp $     */
+/*     $NetBSD: makemove.c,v 1.43 2022/06/19 10:23:48 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.42 2022/05/29 17:01:42 rillig Exp $");
+__RCSID("$NetBSD: makemove.c,v 1.43 2022/06/19 10:23:48 rillig Exp $");
 
 #include "gomoku.h"
 
@@ -121,7 +121,7 @@
            for (int f = 5; --f >= 0; fsp -= d) {       /* for each frame */
                if (fsp->s_occ == BORDER)
                    goto nextr;
-               if ((fsp->s_flags & BFLAG << r) != 0)
+               if (is_blocked(fsp, r))
                    continue;
 
                struct combostr *cbp = &frames[fsp->s_frame[r]];
@@ -139,8 +139,8 @@
                    else if (sp->s_occ == EMPTY)
                        sp->s_wval -= val;
                    else {
-                       /* this frame is now blocked, adjust values */
-                       fsp->s_flags |= BFLAG << r;
+                       set_blocked(fsp, r);
+                       /* adjust values */
                        fsp->s_fval[BLACK][r].s = 0x600;
                        fsp->s_fval[WHITE][r].s = 0x600;
                        while (off-- > 0) {
@@ -290,7 +290,7 @@
                const struct spotstr *sp = &board[os - db * off];
                if (sp->s_occ == BORDER)
                        break;
-               if ((sp->s_flags & BFLAG << rb) != 0)
+               if (is_blocked(sp, rb))
                        continue;
 
                frame_index b = sp->s_frame[rb];
@@ -314,7 +314,7 @@
            for (int f = 0; f < 6; f++, s1 -= d) {
                if (board[s1].s_occ == BORDER)
                    break;
-               if ((board[s1].s_flags & BFLAG << r) != 0)
+               if (is_blocked(&board[s1], r))
                    continue;
 
                /*
@@ -330,7 +330,7 @@
                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)
+                   if (is_blocked(&board[s2], r))
                        continue;
 
                    update_overlap_same_direction(s1, s2, a, d, off - f, r);



Home | Main Index | Thread Index | Old Index