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