Module Name: src Committed By: rillig Date: Sun Jun 19 10:23:48 UTC 2022
Modified Files: src/games/gomoku: bdinit.c gomoku.h makemove.c Log Message: gomoku: reduce usage of magic numbers in the code No binary change. To generate a diff of this commit: cvs rdiff -u -r1.35 -r1.36 src/games/gomoku/bdinit.c cvs rdiff -u -r1.55 -r1.56 src/games/gomoku/gomoku.h cvs rdiff -u -r1.42 -r1.43 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/bdinit.c diff -u src/games/gomoku/bdinit.c:1.35 src/games/gomoku/bdinit.c:1.36 --- src/games/gomoku/bdinit.c:1.35 Sun May 29 16:30:44 2022 +++ src/games/gomoku/bdinit.c Sun Jun 19 10:23:48 2022 @@ -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 @@ init_spot_flags_and_fval(struct spotstr 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 @@ init_spot_frame(struct spotstr *sp, fram { 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 @@ init_overlap_frame(int fia, int ra, int 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]; Index: src/games/gomoku/gomoku.h diff -u src/games/gomoku/gomoku.h:1.55 src/games/gomoku/gomoku.h:1.56 --- src/games/gomoku/gomoku.h:1.55 Sun May 29 17:01:42 2022 +++ src/games/gomoku/gomoku.h Sun Jun 19 10:23:48 2022 @@ -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 @@ typedef unsigned short frame_index; /* 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 @@ struct spotstr { #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 */ Index: src/games/gomoku/makemove.c diff -u src/games/gomoku/makemove.c:1.42 src/games/gomoku/makemove.c:1.43 --- src/games/gomoku/makemove.c:1.42 Sun May 29 17:01:42 2022 +++ src/games/gomoku/makemove.c Sun Jun 19 10:23:48 2022 @@ -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 @@ makemove(player_color us, spot_index mv) 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 @@ makemove(player_color us, spot_index mv) 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 @@ update_overlap_different_direction(spot_ 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 @@ update_overlap(spot_index os) 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 @@ update_overlap(spot_index os) 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);