Home | History | Annotate | Line # | Download | only in gomoku
bdisp.c revision 1.8
      1  1.8       agc /*	$NetBSD: bdisp.c,v 1.8 2003/08/07 09:37:16 agc Exp $	*/
      2  1.4       cgd 
      3  1.1       tls /*
      4  1.1       tls  * Copyright (c) 1994
      5  1.1       tls  *	The Regents of the University of California.  All rights reserved.
      6  1.1       tls  *
      7  1.1       tls  * This code is derived from software contributed to Berkeley by
      8  1.1       tls  * Ralph Campbell.
      9  1.1       tls  *
     10  1.1       tls  * Redistribution and use in source and binary forms, with or without
     11  1.1       tls  * modification, are permitted provided that the following conditions
     12  1.1       tls  * are met:
     13  1.1       tls  * 1. Redistributions of source code must retain the above copyright
     14  1.1       tls  *    notice, this list of conditions and the following disclaimer.
     15  1.1       tls  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       tls  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       tls  *    documentation and/or other materials provided with the distribution.
     18  1.8       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.1       tls  *    may be used to endorse or promote products derived from this software
     20  1.1       tls  *    without specific prior written permission.
     21  1.1       tls  *
     22  1.1       tls  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1       tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1       tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1       tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1       tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1       tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1       tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1       tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1       tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1       tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1       tls  * SUCH DAMAGE.
     33  1.1       tls  */
     34  1.1       tls 
     35  1.5     lukem #include <sys/cdefs.h>
     36  1.1       tls #ifndef lint
     37  1.2       tls #if 0
     38  1.1       tls static char sccsid[] = "@(#)bdisp.c	8.2 (Berkeley) 5/3/95";
     39  1.2       tls #else
     40  1.8       agc __RCSID("$NetBSD: bdisp.c,v 1.8 2003/08/07 09:37:16 agc Exp $");
     41  1.2       tls #endif
     42  1.1       tls #endif /* not lint */
     43  1.1       tls 
     44  1.5     lukem #include <curses.h>
     45  1.5     lukem #include <string.h>
     46  1.1       tls #include "gomoku.h"
     47  1.1       tls 
     48  1.1       tls #define	SCRNH		24		/* assume 24 lines for the moment */
     49  1.1       tls #define	SCRNW		80		/* assume 80 chars for the moment */
     50  1.1       tls 
     51  1.1       tls static	int	lastline;
     52  1.1       tls static	char	pcolor[] = "*O.?";
     53  1.1       tls 
     54  1.7  christos extern int interactive;
     55  1.7  christos extern char *plyr[];
     56  1.7  christos 
     57  1.1       tls /*
     58  1.1       tls  * Initialize screen display.
     59  1.1       tls  */
     60  1.5     lukem void
     61  1.1       tls cursinit()
     62  1.1       tls {
     63  1.1       tls 
     64  1.1       tls 	initscr();
     65  1.1       tls 	noecho();
     66  1.1       tls 	cbreak();
     67  1.1       tls 	leaveok(stdscr, TRUE);
     68  1.1       tls }
     69  1.1       tls 
     70  1.1       tls /*
     71  1.1       tls  * Restore screen display.
     72  1.1       tls  */
     73  1.5     lukem void
     74  1.1       tls cursfini()
     75  1.1       tls {
     76  1.1       tls 
     77  1.1       tls 	leaveok(stdscr, FALSE);
     78  1.1       tls 	move(23, 0);
     79  1.1       tls 	clrtoeol();
     80  1.1       tls 	refresh();
     81  1.1       tls 	endwin();
     82  1.1       tls }
     83  1.1       tls 
     84  1.1       tls /*
     85  1.1       tls  * Initialize board display.
     86  1.1       tls  */
     87  1.5     lukem void
     88  1.1       tls bdisp_init()
     89  1.1       tls {
     90  1.5     lukem 	int i, j;
     91  1.1       tls 
     92  1.1       tls 	/* top border */
     93  1.1       tls 	for (i = 1; i < BSZ1; i++) {
     94  1.1       tls 		move(0, 2 * i + 1);
     95  1.1       tls 		addch(letters[i]);
     96  1.1       tls 	}
     97  1.1       tls 	/* left and right edges */
     98  1.1       tls 	for (j = BSZ1; --j > 0; ) {
     99  1.1       tls 		move(20 - j, 0);
    100  1.1       tls 		printw("%2d ", j);
    101  1.1       tls 		move(20 - j, 2 * BSZ1 + 1);
    102  1.1       tls 		printw("%d ", j);
    103  1.1       tls 	}
    104  1.1       tls 	/* bottom border */
    105  1.1       tls 	for (i = 1; i < BSZ1; i++) {
    106  1.1       tls 		move(20, 2 * i + 1);
    107  1.1       tls 		addch(letters[i]);
    108  1.1       tls 	}
    109  1.1       tls 	bdwho(0);
    110  1.1       tls 	move(0, 47);
    111  1.1       tls 	addstr("#  black  white");
    112  1.1       tls 	lastline = 0;
    113  1.1       tls 	bdisp();
    114  1.1       tls }
    115  1.1       tls 
    116  1.1       tls /*
    117  1.1       tls  * Update who is playing whom.
    118  1.1       tls  */
    119  1.5     lukem void
    120  1.1       tls bdwho(update)
    121  1.1       tls 	int update;
    122  1.1       tls {
    123  1.1       tls 	int i;
    124  1.1       tls 
    125  1.1       tls 	move(21, 0);
    126  1.1       tls 	clrtoeol();
    127  1.1       tls 	i = 6 - strlen(plyr[BLACK]) / 2;
    128  1.1       tls 	move(21, i > 0 ? i : 0);
    129  1.1       tls 	printw("BLACK/%s", plyr[BLACK]);
    130  1.1       tls 	i = 30 - strlen(plyr[WHITE]) / 2;
    131  1.1       tls 	move(21, i);
    132  1.1       tls 	printw("WHITE/%s", plyr[WHITE]);
    133  1.1       tls 	move(21, 19);
    134  1.1       tls 	addstr(" vs. ");
    135  1.1       tls 	if (update)
    136  1.1       tls 		refresh();
    137  1.1       tls }
    138  1.1       tls 
    139  1.1       tls /*
    140  1.1       tls  * Update the board display after a move.
    141  1.1       tls  */
    142  1.5     lukem void
    143  1.1       tls bdisp()
    144  1.1       tls {
    145  1.5     lukem 	int i, j, c;
    146  1.5     lukem 	struct spotstr *sp;
    147  1.1       tls 
    148  1.1       tls 	for (j = BSZ1; --j > 0; ) {
    149  1.1       tls 		for (i = 1; i < BSZ1; i++) {
    150  1.1       tls 			move(BSZ1 - j, 2 * i + 1);
    151  1.1       tls 			sp = &board[i + j * BSZ1];
    152  1.1       tls 			if (debug > 1 && sp->s_occ == EMPTY) {
    153  1.1       tls 				if (sp->s_flg & IFLAGALL)
    154  1.1       tls 					c = '+';
    155  1.1       tls 				else if (sp->s_flg & CFLAGALL)
    156  1.1       tls 					c = '-';
    157  1.1       tls 				else
    158  1.1       tls 					c = '.';
    159  1.1       tls 			} else
    160  1.1       tls 				c = pcolor[sp->s_occ];
    161  1.1       tls 			addch(c);
    162  1.1       tls 		}
    163  1.1       tls 	}
    164  1.1       tls 	refresh();
    165  1.1       tls }
    166  1.1       tls 
    167  1.1       tls #ifdef DEBUG
    168  1.1       tls /*
    169  1.1       tls  * Dump board display to a file.
    170  1.1       tls  */
    171  1.5     lukem void
    172  1.1       tls bdump(fp)
    173  1.1       tls 	FILE *fp;
    174  1.1       tls {
    175  1.5     lukem 	int i, j, c;
    176  1.5     lukem 	struct spotstr *sp;
    177  1.1       tls 
    178  1.1       tls 	/* top border */
    179  1.1       tls 	fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
    180  1.1       tls 
    181  1.1       tls 	for (j = BSZ1; --j > 0; ) {
    182  1.1       tls 		/* left edge */
    183  1.1       tls 		fprintf(fp, "%2d ", j);
    184  1.1       tls 		for (i = 1; i < BSZ1; i++) {
    185  1.1       tls 			sp = &board[i + j * BSZ1];
    186  1.1       tls 			if (debug > 1 && sp->s_occ == EMPTY) {
    187  1.1       tls 				if (sp->s_flg & IFLAGALL)
    188  1.1       tls 					c = '+';
    189  1.1       tls 				else if (sp->s_flg & CFLAGALL)
    190  1.1       tls 					c = '-';
    191  1.1       tls 				else
    192  1.1       tls 					c = '.';
    193  1.1       tls 			} else
    194  1.1       tls 				c = pcolor[sp->s_occ];
    195  1.1       tls 			putc(c, fp);
    196  1.1       tls 			putc(' ', fp);
    197  1.1       tls 		}
    198  1.1       tls 		/* right edge */
    199  1.1       tls 		fprintf(fp, "%d\n", j);
    200  1.1       tls 	}
    201  1.1       tls 
    202  1.1       tls 	/* bottom border */
    203  1.1       tls 	fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
    204  1.1       tls }
    205  1.1       tls #endif /* DEBUG */
    206  1.1       tls 
    207  1.1       tls /*
    208  1.1       tls  * Display a transcript entry
    209  1.1       tls  */
    210  1.5     lukem void
    211  1.1       tls dislog(str)
    212  1.6       jsm 	const char *str;
    213  1.1       tls {
    214  1.1       tls 
    215  1.1       tls 	if (++lastline >= SCRNH - 1) {
    216  1.1       tls 		/* move 'em up */
    217  1.1       tls 		lastline = 1;
    218  1.1       tls 	}
    219  1.1       tls 	move(lastline, 46);
    220  1.6       jsm 	addnstr(str, SCRNW - 46 - 1);
    221  1.1       tls 	clrtoeol();
    222  1.1       tls 	move(lastline + 1, 46);
    223  1.1       tls 	clrtoeol();
    224  1.1       tls }
    225  1.1       tls 
    226  1.1       tls /*
    227  1.1       tls  * Display a question.
    228  1.1       tls  */
    229  1.5     lukem 
    230  1.5     lukem void
    231  1.1       tls ask(str)
    232  1.6       jsm 	const char *str;
    233  1.1       tls {
    234  1.1       tls 	int len = strlen(str);
    235  1.1       tls 
    236  1.1       tls 	move(23, 0);
    237  1.1       tls 	addstr(str);
    238  1.1       tls 	clrtoeol();
    239  1.1       tls 	move(23, len);
    240  1.1       tls 	refresh();
    241  1.1       tls }
    242  1.1       tls 
    243  1.5     lukem int
    244  1.1       tls getline(buf, size)
    245  1.1       tls 	char *buf;
    246  1.1       tls 	int size;
    247  1.1       tls {
    248  1.5     lukem 	char *cp, *end;
    249  1.5     lukem 	int c;
    250  1.1       tls 
    251  1.5     lukem 	c = 0;
    252  1.1       tls 	cp = buf;
    253  1.1       tls 	end = buf + size - 1;	/* save room for the '\0' */
    254  1.1       tls 	while (cp < end && (c = getchar()) != EOF && c != '\n' && c != '\r') {
    255  1.1       tls 		*cp++ = c;
    256  1.1       tls 		if (interactive) {
    257  1.1       tls 			switch (c) {
    258  1.1       tls 			case 0x0c: /* ^L */
    259  1.1       tls 				wrefresh(curscr);
    260  1.1       tls 				cp--;
    261  1.1       tls 				continue;
    262  1.1       tls 			case 0x15: /* ^U */
    263  1.1       tls 			case 0x18: /* ^X */
    264  1.1       tls 				while (cp > buf) {
    265  1.1       tls 					cp--;
    266  1.1       tls 					addch('\b');
    267  1.1       tls 				}
    268  1.1       tls 				clrtoeol();
    269  1.1       tls 				break;
    270  1.1       tls 			case '\b':
    271  1.1       tls 			case 0x7f: /* DEL */
    272  1.1       tls 				if (cp == buf + 1) {
    273  1.1       tls 					cp--;
    274  1.1       tls 					continue;
    275  1.1       tls 				}
    276  1.1       tls 				cp -= 2;
    277  1.1       tls 				addch('\b');
    278  1.1       tls 				c = ' ';
    279  1.1       tls 				/* FALLTHROUGH */
    280  1.1       tls 			default:
    281  1.1       tls 				addch(c);
    282  1.1       tls 			}
    283  1.1       tls 			refresh();
    284  1.1       tls 		}
    285  1.1       tls 	}
    286  1.1       tls 	*cp = '\0';
    287  1.1       tls 	return(c != EOF);
    288  1.1       tls }
    289