gomoku: highlight the winning frame

This commit is contained in:
rillig 2022-05-28 08:32:55 +00:00
parent a347aeb809
commit 8fc3223a93
4 changed files with 28 additions and 10 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: bdinit.c,v 1.23 2022/05/28 08:19:18 rillig Exp $ */
/* $NetBSD: bdinit.c,v 1.24 2022/05/28 08:32:55 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.23 2022/05/28 08:19:18 rillig Exp $");
__RCSID("$NetBSD: bdinit.c,v 1.24 2022/05/28 08:32:55 rillig Exp $");
#include <string.h>
#include "gomoku.h"
@ -48,6 +48,7 @@ bdinit(struct spotstr *bp)
struct combostr *cbp;
game.nmoves = 0;
game.winning_spot = 0;
/* mark the borders as such */
sp = bp;

View File

@ -1,4 +1,4 @@
/* $NetBSD: bdisp.c,v 1.48 2022/05/28 08:19:18 rillig Exp $ */
/* $NetBSD: bdisp.c,v 1.49 2022/05/28 08:32:55 rillig Exp $ */
/*
* Copyright (c) 1994
@ -34,7 +34,7 @@
#include <sys/cdefs.h>
/* @(#)bdisp.c 8.2 (Berkeley) 5/3/95 */
__RCSID("$NetBSD: bdisp.c,v 1.48 2022/05/28 08:19:18 rillig Exp $");
__RCSID("$NetBSD: bdisp.c,v 1.49 2022/05/28 08:32:55 rillig Exp $");
#include <curses.h>
#include <string.h>
@ -148,6 +148,19 @@ bdwho(void)
bw, plyr[BLACK], ww, plyr[WHITE]);
}
static bool
should_highlight(int s)
{
if (game.nmoves > 0 && game.moves[game.nmoves - 1] == s)
return true;
if (game.winning_spot != 0)
for (int i = 0; i < 5; i++)
if (s == game.winning_spot + i * dd[game.winning_dir])
return true;
return false;
}
/*
* Update the board display after a move.
*/
@ -171,8 +184,7 @@ bdisp(void)
c = pcolor[sp->s_occ];
move(scr_y(j), scr_x(i));
if (game.nmoves > 0 &&
game.moves[game.nmoves - 1] == PT(i, j)) {
if (should_highlight(PT(i, j))) {
attron(A_BOLD);
addch(c);
attroff(A_BOLD);

View File

@ -1,4 +1,4 @@
/* $NetBSD: gomoku.h,v 1.43 2022/05/28 08:19:18 rillig Exp $ */
/* $NetBSD: gomoku.h,v 1.44 2022/05/28 08:32:55 rillig Exp $ */
/*
* Copyright (c) 1994
@ -218,6 +218,8 @@ struct spotstr {
struct game {
int moves[BSZ * BSZ]; /* log of all played moves */
unsigned int nmoves; /* number of played moves */
int winning_spot;
int winning_dir;
};
extern const char letters[];

View File

@ -1,4 +1,4 @@
/* $NetBSD: makemove.c,v 1.32 2022/05/28 08:19:18 rillig Exp $ */
/* $NetBSD: makemove.c,v 1.33 2022/05/28 08:32:55 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.32 2022/05/28 08:19:18 rillig Exp $");
__RCSID("$NetBSD: makemove.c,v 1.33 2022/05/28 08:32:55 rillig Exp $");
#include "gomoku.h"
@ -154,8 +154,11 @@ makemove(int us, int mv)
}
/* check for game over */
if (n == 5)
if (n == 5) {
game.winning_spot = (int)(fsp - board);
game.winning_dir = r;
return WIN;
}
/* compute new value & combo number for this frame & color */
fsp->s_fval[us != BLACK ? BLACK : WHITE][r].s = 0x600;