Source-Changes-HG archive

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

[src/trunk]: src/games/gomoku gomoku: clean up color handling in 'pickmove'



details:   https://anonhg.NetBSD.org/src/rev/4690bcc21609
branches:  trunk
changeset: 366531:4690bcc21609
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 29 21:47:12 2022 +0000

description:
gomoku: clean up color handling in 'pickmove'

Instead of searching for the best black and white moves, search instead
for our and their best moves. This makes the code simpler and more
uniform, as the strategy is the same for Black and White.

No functional change.

diffstat:

 games/gomoku/pickmove.c |  49 ++++++++++++++++++++-----------------------------
 1 files changed, 20 insertions(+), 29 deletions(-)

diffs (103 lines):

diff -r 1f8e8407892d -r 4690bcc21609 games/gomoku/pickmove.c
--- a/games/gomoku/pickmove.c   Sun May 29 21:38:36 2022 +0000
+++ b/games/gomoku/pickmove.c   Sun May 29 21:47:12 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pickmove.c,v 1.66 2022/05/29 21:38:36 rillig Exp $     */
+/*     $NetBSD: pickmove.c,v 1.67 2022/05/29 21:47:12 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.66 2022/05/29 21:38:36 rillig Exp $");
+__RCSID("$NetBSD: pickmove.c,v 1.67 2022/05/29 21:47:12 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -106,6 +106,7 @@
        memset(forcemap, 0, sizeof(forcemap));
 
        /* compute new values */
+       player_color them = us != BLACK ? BLACK : WHITE;
        nextcolor = us;
        /*
         * TODO: Scanning for both frames misses that after loading the game
@@ -117,8 +118,8 @@
        scanframes(WHITE);
 
        /* find the spot with the highest value */
-       spot_index s1 = PT(BSZ, BSZ);
-       spot_index s2 = PT(BSZ, BSZ);
+       spot_index os = PT(BSZ, BSZ);           /* our spot */
+       spot_index ts = PT(BSZ, BSZ);           /* their spot */
        for (spot_index s = PT(BSZ, BSZ); s-- > PT(1, 1); ) {
                const struct spotstr *sp = &board[s];
                if (sp->s_occ != EMPTY)
@@ -136,13 +137,15 @@
                            sp->s_wval);
                }
 
-               if (better(s, s1, BLACK))       /* pick the best black move */
-                       s1 = s;
-               if (better(s, s2, WHITE))       /* pick the best white move */
-                       s2 = s;
+               if (better(s, os, us))          /* pick our best move */
+                       os = s;
+               if (better(s, ts, them))        /* pick their best move */
+                       ts = s;
        }
 
        if (debug > 0) {
+               spot_index s1 = us == BLACK ? os : ts;
+               spot_index s2 = us == BLACK ? ts : os;
                const struct spotstr *sp1 = &board[s1], *sp2 = &board[s2];
                debuglog("B %s %x/%d %d %x/%d %d %d",
                    stoc(s1),
@@ -161,36 +164,24 @@
                 * Check for more than one force that can't all be blocked
                 * with one move.
                 */
-               spot_index m = us == BLACK ? s2 : s1;
-               player_color them = us != BLACK ? BLACK : WHITE;
-               if (board[m].s_combo[them].cv_force == 1 &&
-                   !BIT_TEST(forcemap, m))
+               if (board[ts].s_combo[them].cv_force == 1 &&
+                   !BIT_TEST(forcemap, ts))
                        debuglog("*** Can't be blocked");
        }
 
-       union comboval *Ocp, *Tcp;
-       if (us == BLACK) {
-               Ocp = &board[s1].s_combo[BLACK];
-               Tcp = &board[s2].s_combo[WHITE];
-       } else {
-               Tcp = &board[s1].s_combo[BLACK];
-               Ocp = &board[s2].s_combo[WHITE];
-
-               spot_index tmp = s1;
-               s1 = s2;
-               s2 = tmp;
-       }
+       union comboval ocv = board[os].s_combo[us];
+       union comboval tcv = board[ts].s_combo[them];
 
        /*
         * Block their combo only if we have to (i.e., if they are one move
         * away from completing a force, and we don't have a force that
         * we can complete which takes fewer moves to win).
         */
-       if (Tcp->cv_force <= 1 &&
-           !(Ocp->cv_force <= 1 &&
-             Tcp->cv_force + Tcp->cv_win >= Ocp->cv_force + Ocp->cv_win))
-               return s2;
-       return s1;
+       if (tcv.cv_force <= 1 &&
+           !(ocv.cv_force <= 1 &&
+             tcv.cv_force + tcv.cv_win >= ocv.cv_force + ocv.cv_win))
+               return ts;
+       return os;
 }
 
 /*



Home | Main Index | Thread Index | Old Index