#include <sys/cdefs.h>
__RCSID("$NetBSD: makemove.c,v 1.43 2022/06/19 10:23:48 rillig Exp $");
#include "gomoku.h"
const int dd[4] = {
1,
-(BSZ + 1) + 1,
-(BSZ + 1),
-(BSZ + 1) - 1
};
static const int weight[5] = { 0, 1, 7, 22, 100 };
static void update_overlap(spot_index);
static bool
is_tie(void)
{
for (int y = 1; y <= BSZ; y++)
for (int x = 1; x <= BSZ; x++)
if (board[PT(x, y)].s_wval != 0)
return false;
return true;
}
static void
sortframes_remove(struct combostr *cbp)
{
if (cbp->c_next == NULL)
return;
if (sortframes[BLACK] == cbp)
sortframes[BLACK] = cbp->c_next;
if (sortframes[WHITE] == cbp)
sortframes[WHITE] = cbp->c_next;
cbp->c_next->c_prev = cbp->c_prev;
cbp->c_prev->c_next = cbp->c_next;
}
static int
old_weight_value(const struct spotstr *sp, direction r)
{
union comboval cb;
int val = 0;
if ((cb = sp->s_fval[BLACK][r]).s <= 0x500)
val += weight[5 - cb.cv_force - cb.cv_win];
if ((cb = sp->s_fval[WHITE][r]).s <= 0x500)
val += weight[5 - cb.cv_force - cb.cv_win];
return val;
}
int
makemove(player_color us, spot_index mv)
{
if (mv == RESIGN)
return RESIGN;
struct spotstr *sp = &board[mv];
if (sp->s_occ != EMPTY)
return ILLEGAL;
sp->s_occ = us;
game.moves[game.nmoves++] = mv;
sp->s_wval = 0;
for (direction r = 4; r-- > 0; ) {
int d = dd[r];
struct spotstr *fsp = &board[mv];
for (int f = 5; --f >= 0; fsp -= d) {
if (fsp->s_occ == BORDER)
goto nextr;
if (is_blocked(fsp, r))
continue;
struct combostr *cbp = &frames[fsp->s_frame[r]];
sortframes_remove(cbp);
int val = old_weight_value(fsp, r);
bool space = fsp->s_occ == EMPTY;
int n = 0;
sp = fsp;
for (int off = 5; off-- > 0; sp += d) {
if (sp->s_occ == us)
n++;
else if (sp->s_occ == EMPTY)
sp->s_wval -= val;
else {
set_blocked(fsp, r);
fsp->s_fval[BLACK][r].s = 0x600;
fsp->s_fval[WHITE][r].s = 0x600;
while (off-- > 0) {
sp += d;
if (sp->s_occ == EMPTY)
sp->s_wval -= val;
}
goto nextf;
}
}
if (n == 5) {
game.win_spot = (spot_index)(fsp - board);
game.win_dir = r;
return WIN;
}
player_color them = us != BLACK ? BLACK : WHITE;
fsp->s_fval[them][r].s = 0x600;
union comboval *cp = &fsp->s_fval[us][r];
if (space && sp->s_occ == EMPTY) {
cp->cv_force = 4 - n;
cp->cv_win = 1;
} else {
cp->cv_force = 5 - n;
cp->cv_win = 0;
}
val = weight[n];
sp = fsp;
for (int off = 5; off-- > 0; sp += d)
if (sp->s_occ == EMPTY)
sp->s_wval += val;
struct combostr *cbp1 = sortframes[us];
if (cbp1 == NULL)
sortframes[us] = cbp->c_next = cbp->c_prev = cbp;
else {
union comboval *cp1 =
&board[cbp1->c_vertex].s_fval[us][cbp1->c_dir];
if (cp->s <= cp1->s) {
sortframes[us] = cbp;
} else {
do {
cbp1 = cbp1->c_next;
cp1 = &board[cbp1->c_vertex].s_fval[us][cbp1->c_dir];
if (cp->s <= cp1->s)
break;
} while (cbp1 != sortframes[us]);
}
cbp->c_next = cbp1;
cbp->c_prev = cbp1->c_prev;
cbp1->c_prev->c_next = cbp;
cbp1->c_prev = cbp;
}
nextf:
;
}
if (fsp->s_occ == EMPTY) {
union comboval *cp = &fsp->s_fval[BLACK][r];
if (cp->cv_win != 0) {
cp->cv_force++;
cp->cv_win = 0;
}
cp = &fsp->s_fval[WHITE][r];
if (cp->cv_win != 0) {
cp->cv_force++;
cp->cv_win = 0;
}
}
nextr:
;
}
update_overlap(mv);
if (is_tie())
return TIE;
return MOVEOK;
}
static void
update_overlap_same_direction(spot_index s1, spot_index s2,
frame_index a, int d, int off_minus_f,
direction r)
{
int n = 0;
spot_index s = s1;
spot_index es = 0;
for (int b = off_minus_f; b < 5; b++, s += d) {
if (board[s].s_occ == EMPTY) {
es = s;
n++;
}
}
frame_index b = board[s2].s_frame[r];
if (n == 0) {
if (board[s].s_occ == EMPTY) {
overlap[a * FAREA + b] &= 0xA;
overlap[b * FAREA + a] &= 0xC;
intersect[a * FAREA + b] = s;
intersect[b * FAREA + a] = s;
} else {
overlap[a * FAREA + b] = 0;
overlap[b * FAREA + a] = 0;
}
} else if (n == 1) {
if (board[s].s_occ == EMPTY) {
overlap[a * FAREA + b] &= 0xAF;
overlap[b * FAREA + a] &= 0xCF;
} else {
overlap[a * FAREA + b] &= 0xF;
overlap[b * FAREA + a] &= 0xF;
}
intersect[a * FAREA + b] = es;
intersect[b * FAREA + a] = es;
}
}
static void
update_overlap_different_direction(spot_index os, frame_index a, direction rb)
{
int db = dd[rb];
for (int off = 0; off < 6; off++) {
const struct spotstr *sp = &board[os - db * off];
if (sp->s_occ == BORDER)
break;
if (is_blocked(sp, rb))
continue;
frame_index b = sp->s_frame[rb];
overlap[a * FAREA + b] = 0;
overlap[b * FAREA + a] = 0;
}
}
static void
update_overlap(spot_index os)
{
for (direction r = 4; r-- > 0; ) {
int d = dd[r];
spot_index s1 = os;
for (int f = 0; f < 6; f++, s1 -= d) {
if (board[s1].s_occ == BORDER)
break;
if (is_blocked(&board[s1], r))
continue;
frame_index a = board[s1].s_frame[r];
spot_index s2 = s1 - d;
for (int off = f + 1; off < 6; off++, s2 -= d) {
if (board[s2].s_occ == BORDER)
break;
if (is_blocked(&board[s2], r))
continue;
update_overlap_same_direction(s1, s2, a, d, off - f, r);
}
for (direction rb = 0; rb < r; rb++)
update_overlap_different_direction(os, a, rb);
}
}
}