Module Name:    src
Committed By:   rillig
Date:           Sat May 28 06:25:35 UTC 2022

Modified Files:
        src/games/gomoku: gomoku.h main.c makemove.c

Log Message:
gomoku: extract update_overlap_different_direction

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/games/gomoku/gomoku.h
cvs rdiff -u -r1.59 -r1.60 src/games/gomoku/main.c
cvs rdiff -u -r1.28 -r1.29 src/games/gomoku/makemove.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/gomoku.h
diff -u src/games/gomoku/gomoku.h:1.41 src/games/gomoku/gomoku.h:1.42
--- src/games/gomoku/gomoku.h:1.41	Fri May 27 23:10:54 2022
+++ src/games/gomoku/gomoku.h	Sat May 28 06:25:35 2022
@@ -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	const int     dd[4];
 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;

Index: src/games/gomoku/main.c
diff -u src/games/gomoku/main.c:1.59 src/games/gomoku/main.c:1.60
--- src/games/gomoku/main.c:1.59	Fri May 27 19:59:56 2022
+++ src/games/gomoku/main.c	Sat May 28 06:25:35 2022
@@ -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 @@ const char	pdir[4]		= "-\\|/";
 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 */

Index: src/games/gomoku/makemove.c
diff -u src/games/gomoku/makemove.c:1.28 src/games/gomoku/makemove.c:1.29
--- src/games/gomoku/makemove.c:1.28	Sat May 28 05:44:41 2022
+++ src/games/gomoku/makemove.c	Sat May 28 06:25:35 2022
@@ -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 @@ update_overlap_same_direction(const stru
 }
 
 /*
+ * 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 @@ update_overlap(struct spotstr *osp)
 	    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 @@ update_overlap(struct spotstr *osp)
 		 * since the two frames can overlap at more than one point.
 		 */
 		int a = (int)(sp1->s_frame[r] - frames);
-		struct spotstr *sp2 = sp1 - d;
 
+		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 @@ update_overlap(struct spotstr *osp)
 		}
 
 		/* 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);
 	    }
 	}
 }

Reply via email to