summaryrefslogtreecommitdiff
path: root/games
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2022-06-19 10:23:48 +0000
committerrillig <rillig@NetBSD.org>2022-06-19 10:23:48 +0000
commitc883e56889f40ddbf12b566e50b6bebc290bbd58 (patch)
treee7b08ee465a44909f42dc12879d242b6ee4e9663 /games
parent449b3ff90d94beedad902ac7a5070f202929787a (diff)
gomoku: reduce usage of magic numbers in the code
No binary change.
Diffstat (limited to 'games')
-rw-r--r--games/gomoku/bdinit.c91
-rw-r--r--games/gomoku/gomoku.h18
-rw-r--r--games/gomoku/makemove.c16
3 files changed, 69 insertions, 56 deletions
diff --git a/games/gomoku/bdinit.c b/games/gomoku/bdinit.c
index 3d4d7d3a43b..f1e1346e9e7 100644
--- a/games/gomoku/bdinit.c
+++ b/games/gomoku/bdinit.c
@@ -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, int col, int row)
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, frame_index *fip)
{
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 offa, spot_index s, int mask)
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];
diff --git a/games/gomoku/gomoku.h b/games/gomoku/gomoku.h
index 2b71202b337..e7ab5a24528 100644
--- a/games/gomoku/gomoku.h
+++ b/games/gomoku/gomoku.h
@@ -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 */
diff --git a/games/gomoku/makemove.c b/games/gomoku/makemove.c
index d59728ecadc..08a933f406c 100644
--- a/games/gomoku/makemove.c
+++ b/games/gomoku/makemove.c
@@ -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_index os, frame_index a, direction rb)
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);