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