Source-Changes-HG archive

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

[src/trunk]: src/games/gomoku gomoku: extract update_overlap_different_direction



details:   https://anonhg.NetBSD.org/src/rev/0ff6727f6b2b
branches:  trunk
changeset: 366461:0ff6727f6b2b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat May 28 06:25:35 2022 +0000

description:
gomoku: extract update_overlap_different_direction

No functional change.

diffstat:

 games/gomoku/gomoku.h   |   4 ++--
 games/gomoku/main.c     |   7 ++++---
 games/gomoku/makemove.c |  47 ++++++++++++++++++++++++++++++-----------------
 3 files changed, 36 insertions(+), 22 deletions(-)

diffs (136 lines):

diff -r 64dd6228a8a9 -r 0ff6727f6b2b games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Sat May 28 05:44:41 2022 +0000
+++ b/games/gomoku/gomoku.h     Sat May 28 06:25:35 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gomoku.h,v 1.41 2022/05/27 23:10:54 rillig Exp $       */
+/*     $NetBSD: gomoku.h,v 1.42 2022/05/28 06:25:35 rillig Exp $       */
 
 /*
  * Copyright (c) 1994
@@ -222,7 +222,7 @@
 extern struct  spotstr board[BAREA];           /* info for board */
 extern struct  combostr frames[FAREA];         /* storage for single frames */
 extern struct  combostr *sortframes[2];        /* sorted, non-empty frames */
-extern u_char  overlap[FAREA * FAREA];         /* frame [a][b] overlap */
+extern u_char  overlap[FAREA * FAREA];
 extern short   intersect[FAREA * FAREA];       /* frame [a][b] intersection */
 extern int     movelog[BSZ * BSZ];
 extern unsigned int nmoves;
diff -r 64dd6228a8a9 -r 0ff6727f6b2b games/gomoku/main.c
--- a/games/gomoku/main.c       Sat May 28 05:44:41 2022 +0000
+++ b/games/gomoku/main.c       Sat May 28 06:25:35 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.59 2022/05/27 19:59:56 rillig Exp $ */
+/*     $NetBSD: main.c,v 1.60 2022/05/28 06:25:35 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.59 2022/05/27 19:59:56 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.60 2022/05/28 06:25:35 rillig Exp $");
 
 #include <sys/stat.h>
 #include <curses.h>
@@ -76,7 +76,8 @@
 struct spotstr board[BAREA];           /* info for board */
 struct combostr frames[FAREA];         /* storage for all frames */
 struct combostr *sortframes[2];        /* sorted list of non-empty frames */
-u_char overlap[FAREA * FAREA];         /* true if frame [a][b] overlap */
+u_char overlap[FAREA * FAREA];         /* non-zero if frame [a][b] overlap;
+                                        * see init_overlap */
 short  intersect[FAREA * FAREA];       /* frame [a][b] intersection */
 int    movelog[BSZ * BSZ];             /* log of all played moves */
 unsigned int nmoves;                   /* number of played moves */
diff -r 64dd6228a8a9 -r 0ff6727f6b2b games/gomoku/makemove.c
--- a/games/gomoku/makemove.c   Sat May 28 05:44:41 2022 +0000
+++ b/games/gomoku/makemove.c   Sat May 28 06:25:35 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makemove.c,v 1.28 2022/05/28 05:44:41 rillig Exp $     */
+/*     $NetBSD: makemove.c,v 1.29 2022/05/28 06:25:35 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.28 2022/05/28 05:44:41 rillig Exp $");
+__RCSID("$NetBSD: makemove.c,v 1.29 2022/05/28 06:25:35 rillig Exp $");
 
 #include "gomoku.h"
 
@@ -254,6 +254,30 @@
 }
 
 /*
+ * 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'
+ * 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(const struct spotstr *osp, int a, int rb)
+{
+
+       int db = dd[rb];
+       for (int i = 0; i < 6; i++) {
+               const struct spotstr *sp = osp - db * i;
+               if (sp->s_occ == BORDER)
+                       break;
+               if ((sp->s_flags & BFLAG << rb) != 0)
+                       continue;
+
+               int b = (int)(sp->s_frame[rb] - frames);
+               overlap[a * FAREA + b] = 0;
+               overlap[b * FAREA + a] = 0;
+       }
+}
+
+/*
  * fix up the overlap array according to the changed 'osp'.
  */
 static void
@@ -264,7 +288,7 @@
            int d = dd[r];
            struct spotstr *sp1 = osp;
 
-           /* for each frame that contains 'osp' */
+           /* for each frame 'a' that contains the spot 'osp' */
            for (int f = 0; f < 6; f++, sp1 -= d) {
                if (sp1->s_occ == BORDER)
                    break;
@@ -279,8 +303,8 @@
                 * since the two frames can overlap at more than one point.
                 */
                int a = (int)(sp1->s_frame[r] - frames);
+
                struct spotstr *sp2 = sp1 - d;
-
                for (int i = f + 1; i < 6; i++, sp2 -= d) {
                    if (sp2->s_occ == BORDER)
                        break;
@@ -291,19 +315,8 @@
                }
 
                /* the other directions can only intersect at spot osp */
-               for (int r1 = r; --r1 >= 0; ) {
-                   int d1 = dd[r1];
-                   struct spotstr *sp = osp;
-                   for (int i = 6; --i >= 0; sp -= d1) { /* for each spot */
-                       if (sp->s_occ == BORDER)
-                           break;
-                       if ((sp->s_flags & BFLAG << r1) != 0)
-                           continue;
-                       int b = (int)(sp->s_frame[r1] - frames);
-                       overlap[a * FAREA + b] = 0;
-                       overlap[b * FAREA + a] = 0;
-                   }
-               }
+               for (int rb = 0; rb < r; rb++)
+                       update_overlap_different_direction(osp, a, rb);
            }
        }
 }



Home | Main Index | Thread Index | Old Index