xref: /netbsd-src/games/gomoku/bdisp.c (revision 3b435a73967be44dfb4a27315acd72bfacde430c)
1 /*	$NetBSD: bdisp.c,v 1.6 1999/09/08 21:17:49 jsm 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 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)bdisp.c	8.2 (Berkeley) 5/3/95";
43 #else
44 __RCSID("$NetBSD: bdisp.c,v 1.6 1999/09/08 21:17:49 jsm Exp $");
45 #endif
46 #endif /* not lint */
47 
48 #include <curses.h>
49 #include <string.h>
50 #include "gomoku.h"
51 
52 #define	SCRNH		24		/* assume 24 lines for the moment */
53 #define	SCRNW		80		/* assume 80 chars for the moment */
54 
55 static	int	lastline;
56 static	char	pcolor[] = "*O.?";
57 
58 /*
59  * Initialize screen display.
60  */
61 void
62 cursinit()
63 {
64 
65 	initscr();
66 	noecho();
67 	cbreak();
68 	leaveok(stdscr, TRUE);
69 }
70 
71 /*
72  * Restore screen display.
73  */
74 void
75 cursfini()
76 {
77 
78 	leaveok(stdscr, FALSE);
79 	move(23, 0);
80 	clrtoeol();
81 	refresh();
82 	endwin();
83 }
84 
85 /*
86  * Initialize board display.
87  */
88 void
89 bdisp_init()
90 {
91 	int i, j;
92 
93 	/* top border */
94 	for (i = 1; i < BSZ1; i++) {
95 		move(0, 2 * i + 1);
96 		addch(letters[i]);
97 	}
98 	/* left and right edges */
99 	for (j = BSZ1; --j > 0; ) {
100 		move(20 - j, 0);
101 		printw("%2d ", j);
102 		move(20 - j, 2 * BSZ1 + 1);
103 		printw("%d ", j);
104 	}
105 	/* bottom border */
106 	for (i = 1; i < BSZ1; i++) {
107 		move(20, 2 * i + 1);
108 		addch(letters[i]);
109 	}
110 	bdwho(0);
111 	move(0, 47);
112 	addstr("#  black  white");
113 	lastline = 0;
114 	bdisp();
115 }
116 
117 /*
118  * Update who is playing whom.
119  */
120 void
121 bdwho(update)
122 	int update;
123 {
124 	int i;
125 	extern char *plyr[];
126 
127 	move(21, 0);
128 	clrtoeol();
129 	i = 6 - strlen(plyr[BLACK]) / 2;
130 	move(21, i > 0 ? i : 0);
131 	printw("BLACK/%s", plyr[BLACK]);
132 	i = 30 - strlen(plyr[WHITE]) / 2;
133 	move(21, i);
134 	printw("WHITE/%s", plyr[WHITE]);
135 	move(21, 19);
136 	addstr(" vs. ");
137 	if (update)
138 		refresh();
139 }
140 
141 /*
142  * Update the board display after a move.
143  */
144 void
145 bdisp()
146 {
147 	int i, j, c;
148 	struct spotstr *sp;
149 
150 	for (j = BSZ1; --j > 0; ) {
151 		for (i = 1; i < BSZ1; i++) {
152 			move(BSZ1 - j, 2 * i + 1);
153 			sp = &board[i + j * BSZ1];
154 			if (debug > 1 && sp->s_occ == EMPTY) {
155 				if (sp->s_flg & IFLAGALL)
156 					c = '+';
157 				else if (sp->s_flg & CFLAGALL)
158 					c = '-';
159 				else
160 					c = '.';
161 			} else
162 				c = pcolor[sp->s_occ];
163 			addch(c);
164 		}
165 	}
166 	refresh();
167 }
168 
169 #ifdef DEBUG
170 /*
171  * Dump board display to a file.
172  */
173 void
174 bdump(fp)
175 	FILE *fp;
176 {
177 	int i, j, c;
178 	struct spotstr *sp;
179 
180 	/* top border */
181 	fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
182 
183 	for (j = BSZ1; --j > 0; ) {
184 		/* left edge */
185 		fprintf(fp, "%2d ", j);
186 		for (i = 1; i < BSZ1; i++) {
187 			sp = &board[i + j * BSZ1];
188 			if (debug > 1 && sp->s_occ == EMPTY) {
189 				if (sp->s_flg & IFLAGALL)
190 					c = '+';
191 				else if (sp->s_flg & CFLAGALL)
192 					c = '-';
193 				else
194 					c = '.';
195 			} else
196 				c = pcolor[sp->s_occ];
197 			putc(c, fp);
198 			putc(' ', fp);
199 		}
200 		/* right edge */
201 		fprintf(fp, "%d\n", j);
202 	}
203 
204 	/* bottom border */
205 	fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
206 }
207 #endif /* DEBUG */
208 
209 /*
210  * Display a transcript entry
211  */
212 void
213 dislog(str)
214 	const char *str;
215 {
216 
217 	if (++lastline >= SCRNH - 1) {
218 		/* move 'em up */
219 		lastline = 1;
220 	}
221 	move(lastline, 46);
222 	addnstr(str, SCRNW - 46 - 1);
223 	clrtoeol();
224 	move(lastline + 1, 46);
225 	clrtoeol();
226 }
227 
228 /*
229  * Display a question.
230  */
231 
232 void
233 ask(str)
234 	const char *str;
235 {
236 	int len = strlen(str);
237 
238 	move(23, 0);
239 	addstr(str);
240 	clrtoeol();
241 	move(23, len);
242 	refresh();
243 }
244 
245 int
246 getline(buf, size)
247 	char *buf;
248 	int size;
249 {
250 	char *cp, *end;
251 	int c;
252 	extern int interactive;
253 
254 	c = 0;
255 	cp = buf;
256 	end = buf + size - 1;	/* save room for the '\0' */
257 	while (cp < end && (c = getchar()) != EOF && c != '\n' && c != '\r') {
258 		*cp++ = c;
259 		if (interactive) {
260 			switch (c) {
261 			case 0x0c: /* ^L */
262 				wrefresh(curscr);
263 				cp--;
264 				continue;
265 			case 0x15: /* ^U */
266 			case 0x18: /* ^X */
267 				while (cp > buf) {
268 					cp--;
269 					addch('\b');
270 				}
271 				clrtoeol();
272 				break;
273 			case '\b':
274 			case 0x7f: /* DEL */
275 				if (cp == buf + 1) {
276 					cp--;
277 					continue;
278 				}
279 				cp -= 2;
280 				addch('\b');
281 				c = ' ';
282 				/* FALLTHROUGH */
283 			default:
284 				addch(c);
285 			}
286 			refresh();
287 		}
288 	}
289 	*cp = '\0';
290 	return(c != EOF);
291 }
292