#include "gomoku.h"
int dd[4] = {
MRIGHT, MRIGHT+MDOWN, MDOWN, MDOWN+MLEFT
};
int weight[5] = { 0, 1, 7, 22, 100 };
int
makemove(int us, int mv)
{
struct spotstr *sp, *fsp;
union comboval *cp;
struct spotstr *osp;
struct combostr *cbp, *cbp1;
union comboval *cp1;
int i, f, r, d, n;
int space, val, bmask;
if (mv == RESIGN)
return(RESIGN);
sp = &board[mv];
if (sp->s_occ != EMPTY)
return(ILLEGAL);
sp->s_occ = us;
movelog[movenum - 1] = mv;
if (++movenum == BSZ * BSZ)
return(TIE);
sp->s_wval = 0;
osp = sp;
for (r = 4; --r >= 0; ) {
d = dd[r];
fsp = osp;
bmask = BFLAG << r;
for (f = 5; --f >= 0; fsp -= d) {
if (fsp->s_occ == BORDER)
goto nextr;
if (fsp->s_flg & bmask)
continue;
cbp = fsp->s_frame[r];
if (cbp->c_next) {
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;
}
cp = &fsp->s_fval[BLACK][r];
if (cp->s <= 0x500)
val = weight[5 - cp->c.a - cp->c.b];
else
val = 0;
cp = &fsp->s_fval[WHITE][r];
if (cp->s <= 0x500)
val += weight[5 - cp->c.a - cp->c.b];
sp = fsp;
space = sp->s_occ == EMPTY;
n = 0;
for (i = 5; --i >= 0; sp += d) {
if (sp->s_occ == us)
n++;
else if (sp->s_occ == EMPTY)
sp->s_wval -= val;
else {
fsp->s_flg |= bmask;
fsp->s_fval[BLACK][r].s = MAXCOMBO;
fsp->s_fval[WHITE][r].s = MAXCOMBO;
while (--i >= 0) {
sp += d;
if (sp->s_occ == EMPTY)
sp->s_wval -= val;
}
goto nextf;
}
}
if (n == 5)
return(WIN);
fsp->s_fval[!us][r].s = MAXCOMBO;
cp = &fsp->s_fval[us][r];
if (space && sp->s_occ == EMPTY) {
cp->c.a = 4 - n;
cp->c.b = 1;
} else {
cp->c.a = 5 - n;
cp->c.b = 0;
}
val = weight[n];
sp = fsp;
for (i = 5; --i >= 0; sp += d)
if (sp->s_occ == EMPTY)
sp->s_wval += val;
cbp1 = sortframes[us];
if (!cbp1)
sortframes[us] = cbp->c_next = cbp->c_prev = cbp;
else {
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) {
cp = &fsp->s_fval[BLACK][r];
if (cp->c.b) {
cp->c.a += 1;
cp->c.b = 0;
}
cp = &fsp->s_fval[WHITE][r];
if (cp->c.b) {
cp->c.a += 1;
cp->c.b = 0;
}
}
nextr:
;
}
update_overlap(osp);
return(MOVEOK);
}
void
update_overlap(struct spotstr *osp)
{
struct spotstr *sp, *sp1, *sp2;
int i, f, r, r1, d, d1, n;
int a, b, bmask, bmask1;
struct spotstr *esp = NULL;
u_char *str;
for (r = 4; --r >= 0; ) {
d = dd[r];
sp1 = osp;
bmask = BFLAG << r;
for (f = 0; f < 6; f++, sp1 -= d) {
if (sp1->s_occ == BORDER)
break;
if (sp1->s_flg & bmask)
continue;
str = &overlap[(a = sp1->s_frame[r] - frames) * FAREA];
sp2 = sp1 - d;
for (i = f + 1; i < 6; i++, sp2 -= d) {
if (sp2->s_occ == BORDER)
break;
if (sp2->s_flg & bmask)
continue;
n = 0;
sp = sp1;
for (b = i - f; b < 5; b++, sp += d) {
if (sp->s_occ == EMPTY) {
esp = sp;
n++;
}
}
b = sp2->s_frame[r] - frames;
if (n == 0) {
if (sp->s_occ == EMPTY) {
str[b] &= 0xA;
overlap[b * FAREA + a] &= 0xC;
intersect[a * FAREA + b] = n = sp - board;
intersect[b * FAREA + a] = n;
} else {
str[b] = 0;
overlap[b * FAREA + a] = 0;
}
} else if (n == 1) {
if (sp->s_occ == EMPTY) {
str[b] &= 0xAF;
overlap[b * FAREA + a] &= 0xCF;
} else {
str[b] &= 0xF;
overlap[b * FAREA + a] &= 0xF;
}
intersect[a * FAREA + b] = n = esp - board;
intersect[b * FAREA + a] = n;
}
}
for (r1 = r; --r1 >= 0; ) {
d1 = dd[r1];
bmask1 = BFLAG << r1;
sp = osp;
for (i = 6; --i >= 0; sp -= d1) {
if (sp->s_occ == BORDER)
break;
if (sp->s_flg & bmask1)
continue;
b = sp->s_frame[r1] - frames;
str[b] = 0;
overlap[b * FAREA + a] = 0;
}
}
}
}
}