bdisp.c revision 1.11
1/*	$NetBSD: bdisp.c,v 1.11 2009/06/04 05:43:29 dholland 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
38static char sccsid[] = "@(#)bdisp.c	8.2 (Berkeley) 5/3/95";
39#else
40__RCSID("$NetBSD: bdisp.c,v 1.11 2009/06/04 05:43:29 dholland Exp $");
41#endif
42#endif /* not lint */
43
44#include <curses.h>
45#include <string.h>
46#include <stdlib.h>
47#include "gomoku.h"
48
49#define	SCRNH		24		/* assume 24 lines for the moment */
50#define	SCRNW		80		/* assume 80 chars for the moment */
51
52static	int	lastline;
53static	char	pcolor[] = "*O.?";
54
55extern int interactive;
56extern char *plyr[];
57
58/*
59 * Initialize screen display.
60 */
61void
62cursinit(void)
63{
64
65	if (!initscr()) {
66		fprintf(stderr, "couldn't initialize screen\n");
67		exit (0);
68	}
69	noecho();
70	cbreak();
71	leaveok(stdscr, TRUE);
72}
73
74/*
75 * Restore screen display.
76 */
77void
78cursfini(void)
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(void)
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(int update)
125{
126	int i;
127
128	move(21, 0);
129	clrtoeol();
130	i = 6 - strlen(plyr[BLACK]) / 2;
131	move(21, i > 0 ? i : 0);
132	printw("BLACK/%s", plyr[BLACK]);
133	i = 30 - strlen(plyr[WHITE]) / 2;
134	move(21, i);
135	printw("WHITE/%s", plyr[WHITE]);
136	move(21, 19);
137	addstr(" vs. ");
138	if (update)
139		refresh();
140}
141
142/*
143 * Update the board display after a move.
144 */
145void
146bdisp(void)
147{
148	int i, j, c;
149	struct spotstr *sp;
150
151	for (j = BSZ1; --j > 0; ) {
152		for (i = 1; i < BSZ1; i++) {
153			move(BSZ1 - j, 2 * i + 1);
154			sp = &board[i + j * BSZ1];
155			if (debug > 1 && sp->s_occ == EMPTY) {
156				if (sp->s_flags & IFLAGALL)
157					c = '+';
158				else if (sp->s_flags & CFLAGALL)
159					c = '-';
160				else
161					c = '.';
162			} else
163				c = pcolor[sp->s_occ];
164			addch(c);
165		}
166	}
167	refresh();
168}
169
170#ifdef DEBUG
171/*
172 * Dump board display to a file.
173 */
174void
175bdump(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_flags & IFLAGALL)
190					c = '+';
191				else if (sp->s_flags & 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 */
212void
213dislog(const char *str)
214{
215
216	if (++lastline >= SCRNH - 1) {
217		/* move 'em up */
218		lastline = 1;
219	}
220	move(lastline, 46);
221	addnstr(str, SCRNW - 46 - 1);
222	clrtoeol();
223	move(lastline + 1, 46);
224	clrtoeol();
225}
226
227/*
228 * Display a question.
229 */
230
231void
232ask(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
243int
244getline(char *buf, int size)
245{
246	char *cp, *end;
247	int c;
248
249	c = 0;
250	cp = buf;
251	end = buf + size - 1;	/* save room for the '\0' */
252	while (cp < end && (c = getchar()) != EOF && c != '\n' && c != '\r') {
253		*cp++ = c;
254		if (interactive) {
255			switch (c) {
256			case 0x0c: /* ^L */
257				wrefresh(curscr);
258				cp--;
259				continue;
260			case 0x15: /* ^U */
261			case 0x18: /* ^X */
262				while (cp > buf) {
263					cp--;
264					addch('\b');
265				}
266				clrtoeol();
267				break;
268			case '\b':
269			case 0x7f: /* DEL */
270				if (cp == buf + 1) {
271					cp--;
272					continue;
273				}
274				cp -= 2;
275				addch('\b');
276				c = ' ';
277				/* FALLTHROUGH */
278			default:
279				addch(c);
280			}
281			refresh();
282		}
283	}
284	*cp = '\0';
285	return(c != EOF);
286}
287