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