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