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