Source-Changes-HG archive

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

[src/trunk]: src/games/gomoku gomoku: add data type for one of the 4 directio...



details:   https://anonhg.NetBSD.org/src/rev/1d04664800f6
branches:  trunk
changeset: 366512:1d04664800f6
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 29 13:49:10 2022 +0000

description:
gomoku: add data type for one of the 4 directions of a frame

No functional change.

diffstat:

 games/gomoku/bdinit.c   |   6 +++---
 games/gomoku/gomoku.h   |  21 ++++++++++++---------
 games/gomoku/makemove.c |  32 +++++++++++++++++---------------
 games/gomoku/pickmove.c |   9 +++++----
 4 files changed, 37 insertions(+), 31 deletions(-)

diffs (228 lines):

diff -r f15723ea5557 -r 1d04664800f6 games/gomoku/bdinit.c
--- a/games/gomoku/bdinit.c     Sun May 29 12:44:17 2022 +0000
+++ b/games/gomoku/bdinit.c     Sun May 29 13:49:10 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdinit.c,v 1.31 2022/05/29 11:36:12 rillig Exp $       */
+/*     $NetBSD: bdinit.c,v 1.32 2022/05/29 13:49:10 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.31 2022/05/29 11:36:12 rillig Exp $");
+__RCSID("$NetBSD: bdinit.c,v 1.32 2022/05/29 13:49:10 rillig Exp $");
 
 #include <string.h>
 #include "gomoku.h"
@@ -109,7 +109,7 @@
 init_spot_frame(struct spotstr *sp, frame_index *fip)
 {
 
-       for (int r = 4; --r >= 0; ) {
+       for (direction r = 4; r-- > 0; ) {
                if ((sp->s_flags & (BFLAG << r)) != 0)
                        continue;
 
diff -r f15723ea5557 -r 1d04664800f6 games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Sun May 29 12:44:17 2022 +0000
+++ b/games/gomoku/gomoku.h     Sun May 29 13:49:10 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gomoku.h,v 1.49 2022/05/29 10:37:21 rillig Exp $       */
+/*     $NetBSD: gomoku.h,v 1.50 2022/05/29 13:49:10 rillig Exp $       */
 
 /*
  * Copyright (c) 1994
@@ -190,18 +190,21 @@
 /* The index of a frame in the global 'frames'. */
 typedef unsigned short frame_index;
 
+/* 0 = right, 1 = down right, 2 = down, 3 = down left. */
+typedef unsigned char direction;
+
 /*
  * One spot structure for each location on the board.
- * A frame consists of the combination for the current spot plus the five spots
- * 0: right, 1: right & down, 2: down, 3: down & left.
+ * A frame consists of the combination for the current spot plus the next
+ * five spots in the direction.
  */
 struct spotstr {
        short           s_occ;          /* color of occupant */
        short           s_wval;         /* weighted value */
        int             s_flags;        /* flags for graph walks */
        frame_index     s_frame[4];     /* level 1 combo for [dir] */
-       union comboval  s_fval[2][4];   /* combo value for [color][frame] */
-       union comboval  s_combo[2];     /* minimum combo value for BLK & WHT */
+       union comboval  s_fval[2][4];   /* combo value for [color][dir] */
+       union comboval  s_combo[2];     /* minimum combo value for [color] */
        u_char          s_level[2];     /* number of frames in the min combo */
        u_char          s_nforce[2];    /* number of <1,x> combos */
        struct elist    *s_empty;       /* level n combo completion spots */
@@ -221,10 +224,10 @@
 #define BFLAGALL       0x0F0000        /* all frames dead */
 
 struct game {
-       spot_index moves[BSZ * BSZ];    /* log of all played moves */
-       unsigned int nmoves;            /* number of played moves */
-       spot_index winning_spot;
-       int winning_dir;
+       unsigned int    nmoves;         /* number of played moves */
+       spot_index      moves[BSZ * BSZ]; /* log of all played moves */
+       spot_index      winning_spot;
+       direction       winning_dir;
 };
 
 extern const char      letters[];
diff -r f15723ea5557 -r 1d04664800f6 games/gomoku/makemove.c
--- a/games/gomoku/makemove.c   Sun May 29 12:44:17 2022 +0000
+++ b/games/gomoku/makemove.c   Sun May 29 13:49:10 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makemove.c,v 1.37 2022/05/29 11:36:12 rillig Exp $     */
+/*     $NetBSD: makemove.c,v 1.38 2022/05/29 13:49:10 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.37 2022/05/29 11:36:12 rillig Exp $");
+__RCSID("$NetBSD: makemove.c,v 1.38 2022/05/29 13:49:10 rillig Exp $");
 
 #include "gomoku.h"
 
@@ -77,7 +77,7 @@
 }
 
 static int
-old_weight_value(const struct spotstr *sp, int r)
+old_weight_value(const struct spotstr *sp, direction r)
 {
        union comboval cb;
        int val = 0;
@@ -115,7 +115,7 @@
 
        /* compute new frame values */
        sp->s_wval = 0;
-       for (int r = 4; --r >= 0; ) {           /* for each direction */
+       for (direction r = 4; r-- > 0; ) {
            int d = dd[r];
            struct spotstr *fsp = &board[mv];
 
@@ -161,7 +161,8 @@
                }
 
                /* compute new value & combo number for this frame & color */
-               fsp->s_fval[us != BLACK ? BLACK : WHITE][r].s = 0x600;
+               int them = us != BLACK ? BLACK : WHITE;
+               fsp->s_fval[them][r].s = 0x600;
                union comboval *cp = &fsp->s_fval[us][r];
                /* both ends open? */
                if (space && sp->s_occ == EMPTY) {
@@ -233,7 +234,8 @@
 
 static void
 update_overlap_same_direction(spot_index s1, spot_index s2,
-                             frame_index a, int d, int i_minus_f, int r)
+                             frame_index a, int d, int i_minus_f,
+                             direction r)
 {
        /*
         * count the number of empty spots to see if there is
@@ -275,13 +277,13 @@
 }
 
 /*
- * The last move was at 'osp', which is part of frame 'a'. There are 6 frames
- * with direction 'rb' that cross frame 'a' in 'osp'. Since the spot 'osp'
+ * The last move was at 'os', which is part of frame 'a'. There are 6 frames
+ * with direction 'rb' that cross frame 'a' in 'os'. Since the spot 'os'
  * cannot be used as a double threat anymore, mark each of these crossing
  * frames as non-overlapping with frame 'a'.
  */
 static void
-update_overlap_different_direction(spot_index os, frame_index a, int rb)
+update_overlap_different_direction(spot_index os, frame_index a, direction rb)
 {
 
        int db = dd[rb];
@@ -299,17 +301,17 @@
 }
 
 /*
- * fix up the overlap array according to the changed 'osp'.
+ * fix up the overlap array according to the changed 'os'.
  */
 static void
 update_overlap(spot_index os)
 {
 
-       for (int r = 4; --r >= 0; ) {           /* for each direction */
+       for (direction r = 4; r-- > 0; ) {
            int d = dd[r];
            spot_index s1 = os;
 
-           /* for each frame 'a' that contains the spot 'osp' */
+           /* for each frame 'a' that contains the spot 'os' */
            for (int f = 0; f < 6; f++, s1 -= d) {
                if (board[s1].s_occ == BORDER)
                    break;
@@ -321,7 +323,7 @@
                 * to indicate whether they still overlap or not.
                 * Since F1 overlap F2 == F2 overlap F1, we only need to
                 * do the rows 0 <= r1 <= r. The r1 == r case is special
-                * since the two frames can overlap at more than one point.
+                * since the two frames can overlap in more than one spot.
                 */
                frame_index a = board[s1].s_frame[r];
 
@@ -335,8 +337,8 @@
                    update_overlap_same_direction(s1, s2, a, d, i - f, r);
                }
 
-               /* the other directions can only intersect at spot osp */
-               for (int rb = 0; rb < r; rb++)
+               /* the other directions can only intersect at spot 'os' */
+               for (direction rb = 0; rb < r; rb++)
                        update_overlap_different_direction(os, a, rb);
            }
        }
diff -r f15723ea5557 -r 1d04664800f6 games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c   Sun May 29 12:44:17 2022 +0000
+++ b/games/gomoku/pickmove.c   Sun May 29 13:49:10 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pickmove.c,v 1.56 2022/05/29 12:44:17 rillig Exp $     */
+/*     $NetBSD: pickmove.c,v 1.57 2022/05/29 13:49:10 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.56 2022/05/29 12:44:17 rillig Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.57 2022/05/29 13:49:10 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -227,7 +227,7 @@
 static unsigned int curlevel;  /* implicit parameter to makecombo() */
 
 static bool
-four_in_a_row(int color, spot_index s, int r)
+four_in_a_row(int color, spot_index s, direction r)
 {
 
        struct spotstr *sp = &board[s];
@@ -437,7 +437,8 @@
        baseB = ocb.cv_force + ocb.cv_win - 1;
        fcnt = ocb.cv_force - 2;
        emask = fcnt != 0 ? ((ocb.cv_win != 0 ? 0x1E : 0x1F) & ~(1 << off)) : 0;
-       for (int r = 4; --r >= 0; ) {           /* for each direction */
+
+       for (direction r = 4; r-- > 0; ) {
            /* don't include frames that overlap in the same direction */
            if (r == ocbp->c_dir)
                continue;



Home | Main Index | Thread Index | Old Index