bdisp.c revision 1.29 1 1.29 rillig /* $NetBSD: bdisp.c,v 1.29 2022/05/19 16:58:39 rillig Exp $ */
2 1.4 cgd
3 1.1 tls /*
4 1.1 tls * Copyright (c) 1994
5 1.1 tls * The Regents of the University of California. All rights reserved.
6 1.1 tls *
7 1.1 tls * This code is derived from software contributed to Berkeley by
8 1.1 tls * Ralph Campbell.
9 1.1 tls *
10 1.1 tls * Redistribution and use in source and binary forms, with or without
11 1.1 tls * modification, are permitted provided that the following conditions
12 1.1 tls * are met:
13 1.1 tls * 1. Redistributions of source code must retain the above copyright
14 1.1 tls * notice, this list of conditions and the following disclaimer.
15 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tls * notice, this list of conditions and the following disclaimer in the
17 1.1 tls * documentation and/or other materials provided with the distribution.
18 1.8 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 tls * may be used to endorse or promote products derived from this software
20 1.1 tls * without specific prior written permission.
21 1.1 tls *
22 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 tls * SUCH DAMAGE.
33 1.1 tls */
34 1.1 tls
35 1.5 lukem #include <sys/cdefs.h>
36 1.1 tls #ifndef lint
37 1.2 tls #if 0
38 1.1 tls static char sccsid[] = "@(#)bdisp.c 8.2 (Berkeley) 5/3/95";
39 1.2 tls #else
40 1.29 rillig __RCSID("$NetBSD: bdisp.c,v 1.29 2022/05/19 16:58:39 rillig Exp $");
41 1.2 tls #endif
42 1.1 tls #endif /* not lint */
43 1.1 tls
44 1.5 lukem #include <curses.h>
45 1.5 lukem #include <string.h>
46 1.9 drochner #include <stdlib.h>
47 1.13 dholland #include <err.h>
48 1.1 tls #include "gomoku.h"
49 1.1 tls
50 1.1 tls #define SCRNH 24 /* assume 24 lines for the moment */
51 1.1 tls #define SCRNW 80 /* assume 80 chars for the moment */
52 1.1 tls
53 1.1 tls static int lastline;
54 1.1 tls static char pcolor[] = "*O.?";
55 1.1 tls
56 1.1 tls /*
57 1.1 tls * Initialize screen display.
58 1.1 tls */
59 1.5 lukem void
60 1.10 dholland cursinit(void)
61 1.1 tls {
62 1.1 tls
63 1.24 rillig if (initscr() == NULL) {
64 1.13 dholland errx(EXIT_FAILURE, "Couldn't initialize screen");
65 1.9 drochner }
66 1.14 dholland if ((LINES < SCRNH) || (COLS < SCRNW)) {
67 1.14 dholland errx(EXIT_FAILURE, "Screen too small (need %d%xd)",
68 1.14 dholland SCRNW, SCRNH);
69 1.14 dholland }
70 1.24 rillig keypad(stdscr, true);
71 1.14 dholland nonl();
72 1.1 tls noecho();
73 1.1 tls cbreak();
74 1.29 rillig leaveok(stdscr, false);
75 1.14 dholland
76 1.14 dholland #if 0 /* no mouse support in netbsd curses yet */
77 1.14 dholland mousemask(BUTTON1_CLICKED, NULL);
78 1.14 dholland #endif
79 1.1 tls }
80 1.1 tls
81 1.1 tls /*
82 1.1 tls * Restore screen display.
83 1.1 tls */
84 1.5 lukem void
85 1.10 dholland cursfini(void)
86 1.1 tls {
87 1.1 tls
88 1.22 rillig move(BSZ + 4, 0);
89 1.1 tls clrtoeol();
90 1.1 tls refresh();
91 1.14 dholland echo();
92 1.1 tls endwin();
93 1.1 tls }
94 1.1 tls
95 1.1 tls /*
96 1.1 tls * Initialize board display.
97 1.1 tls */
98 1.5 lukem void
99 1.10 dholland bdisp_init(void)
100 1.1 tls {
101 1.1 tls
102 1.1 tls /* top border */
103 1.28 rillig for (int i = 1; i < BSZ + 1; i++) {
104 1.1 tls move(0, 2 * i + 1);
105 1.1 tls addch(letters[i]);
106 1.1 tls }
107 1.1 tls /* left and right edges */
108 1.28 rillig for (int j = BSZ + 1; --j > 0; ) {
109 1.1 tls move(20 - j, 0);
110 1.1 tls printw("%2d ", j);
111 1.22 rillig move(20 - j, 2 * (BSZ + 1) + 1);
112 1.1 tls printw("%d ", j);
113 1.1 tls }
114 1.1 tls /* bottom border */
115 1.28 rillig for (int i = 1; i < BSZ + 1; i++) {
116 1.1 tls move(20, 2 * i + 1);
117 1.1 tls addch(letters[i]);
118 1.1 tls }
119 1.24 rillig bdwho(false);
120 1.1 tls move(0, 47);
121 1.1 tls addstr("# black white");
122 1.1 tls lastline = 0;
123 1.1 tls bdisp();
124 1.1 tls }
125 1.1 tls
126 1.1 tls /*
127 1.1 tls * Update who is playing whom.
128 1.1 tls */
129 1.5 lukem void
130 1.26 rillig bdwho(bool update)
131 1.1 tls {
132 1.14 dholland int i, j;
133 1.1 tls
134 1.1 tls move(21, 0);
135 1.20 rillig printw(" ");
136 1.23 rillig i = (int)strlen(plyr[BLACK]);
137 1.23 rillig j = (int)strlen(plyr[WHITE]);
138 1.14 dholland if (i + j <= 20) {
139 1.20 rillig move(21, 10 - (i + j) / 2);
140 1.14 dholland printw("BLACK/%s (*) vs. WHITE/%s (O)",
141 1.14 dholland plyr[BLACK], plyr[WHITE]);
142 1.14 dholland } else {
143 1.14 dholland move(21, 0);
144 1.14 dholland if (i <= 10) {
145 1.14 dholland j = 20 - i;
146 1.14 dholland } else if (j <= 10) {
147 1.14 dholland i = 20 - j;
148 1.14 dholland } else {
149 1.14 dholland i = j = 10;
150 1.14 dholland }
151 1.14 dholland printw("BLACK/%.*s (*) vs. WHITE/%.*s (O)",
152 1.14 dholland i, plyr[BLACK], j, plyr[WHITE]);
153 1.14 dholland }
154 1.1 tls if (update)
155 1.1 tls refresh();
156 1.1 tls }
157 1.1 tls
158 1.1 tls /*
159 1.1 tls * Update the board display after a move.
160 1.1 tls */
161 1.5 lukem void
162 1.10 dholland bdisp(void)
163 1.1 tls {
164 1.28 rillig int c;
165 1.5 lukem struct spotstr *sp;
166 1.1 tls
167 1.28 rillig for (int j = BSZ + 1; --j > 0; ) {
168 1.28 rillig for (int i = 1; i < BSZ + 1; i++) {
169 1.22 rillig move(BSZ + 1 - j, 2 * i + 1);
170 1.22 rillig sp = &board[i + j * (BSZ + 1)];
171 1.1 tls if (debug > 1 && sp->s_occ == EMPTY) {
172 1.24 rillig if ((sp->s_flags & IFLAGALL) != 0)
173 1.1 tls c = '+';
174 1.24 rillig else if ((sp->s_flags & CFLAGALL) != 0)
175 1.1 tls c = '-';
176 1.1 tls else
177 1.1 tls c = '.';
178 1.1 tls } else
179 1.1 tls c = pcolor[sp->s_occ];
180 1.27 rillig if (movenum > 1 && movelog[movenum - 2] == PT(i, j)) {
181 1.27 rillig attron(A_BOLD);
182 1.27 rillig addch(c);
183 1.27 rillig attroff(A_BOLD);
184 1.27 rillig } else
185 1.27 rillig addch(c);
186 1.1 tls }
187 1.1 tls }
188 1.1 tls refresh();
189 1.1 tls }
190 1.1 tls
191 1.1 tls #ifdef DEBUG
192 1.1 tls /*
193 1.1 tls * Dump board display to a file.
194 1.1 tls */
195 1.5 lukem void
196 1.10 dholland bdump(FILE *fp)
197 1.1 tls {
198 1.28 rillig int c;
199 1.5 lukem struct spotstr *sp;
200 1.1 tls
201 1.1 tls /* top border */
202 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");
203 1.1 tls
204 1.28 rillig for (int j = BSZ + 1; --j > 0; ) {
205 1.1 tls /* left edge */
206 1.1 tls fprintf(fp, "%2d ", j);
207 1.28 rillig for (int i = 1; i < BSZ + 1; i++) {
208 1.22 rillig sp = &board[i + j * (BSZ + 1)];
209 1.1 tls if (debug > 1 && sp->s_occ == EMPTY) {
210 1.25 rillig if ((sp->s_flags & IFLAGALL) != 0)
211 1.1 tls c = '+';
212 1.25 rillig else if ((sp->s_flags & CFLAGALL) != 0)
213 1.1 tls c = '-';
214 1.1 tls else
215 1.1 tls c = '.';
216 1.1 tls } else
217 1.1 tls c = pcolor[sp->s_occ];
218 1.1 tls putc(c, fp);
219 1.1 tls putc(' ', fp);
220 1.1 tls }
221 1.1 tls /* right edge */
222 1.1 tls fprintf(fp, "%d\n", j);
223 1.1 tls }
224 1.1 tls
225 1.1 tls /* bottom border */
226 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");
227 1.1 tls }
228 1.1 tls #endif /* DEBUG */
229 1.1 tls
230 1.1 tls /*
231 1.1 tls * Display a transcript entry
232 1.1 tls */
233 1.5 lukem void
234 1.10 dholland dislog(const char *str)
235 1.1 tls {
236 1.1 tls
237 1.1 tls if (++lastline >= SCRNH - 1) {
238 1.1 tls /* move 'em up */
239 1.1 tls lastline = 1;
240 1.1 tls }
241 1.14 dholland move(lastline, TRANSCRIPT_COL);
242 1.14 dholland addnstr(str, SCRNW - TRANSCRIPT_COL - 1);
243 1.1 tls clrtoeol();
244 1.14 dholland move(lastline + 1, TRANSCRIPT_COL);
245 1.1 tls clrtoeol();
246 1.1 tls }
247 1.1 tls
248 1.1 tls /*
249 1.1 tls * Display a question.
250 1.1 tls */
251 1.5 lukem
252 1.5 lukem void
253 1.10 dholland ask(const char *str)
254 1.1 tls {
255 1.23 rillig int len = (int)strlen(str);
256 1.1 tls
257 1.22 rillig move(BSZ + 4, 0);
258 1.1 tls addstr(str);
259 1.1 tls clrtoeol();
260 1.22 rillig move(BSZ + 4, len);
261 1.1 tls refresh();
262 1.1 tls }
263 1.1 tls
264 1.5 lukem int
265 1.15 dholland get_key(const char *allowed)
266 1.15 dholland {
267 1.15 dholland int ch;
268 1.15 dholland
269 1.19 rillig for (;;) {
270 1.15 dholland ch = getch();
271 1.15 dholland if (allowed != NULL &&
272 1.15 dholland ch != '\0' && strchr(allowed, ch) == NULL) {
273 1.15 dholland beep();
274 1.15 dholland refresh();
275 1.15 dholland continue;
276 1.15 dholland }
277 1.15 dholland break;
278 1.15 dholland }
279 1.15 dholland return ch;
280 1.15 dholland }
281 1.15 dholland
282 1.26 rillig bool
283 1.12 roy get_line(char *buf, int size)
284 1.1 tls {
285 1.5 lukem char *cp, *end;
286 1.5 lukem int c;
287 1.1 tls
288 1.5 lukem c = 0;
289 1.1 tls cp = buf;
290 1.1 tls end = buf + size - 1; /* save room for the '\0' */
291 1.1 tls while (cp < end && (c = getchar()) != EOF && c != '\n' && c != '\r') {
292 1.1 tls *cp++ = c;
293 1.1 tls if (interactive) {
294 1.1 tls switch (c) {
295 1.1 tls case 0x0c: /* ^L */
296 1.1 tls wrefresh(curscr);
297 1.1 tls cp--;
298 1.1 tls continue;
299 1.1 tls case 0x15: /* ^U */
300 1.1 tls case 0x18: /* ^X */
301 1.1 tls while (cp > buf) {
302 1.1 tls cp--;
303 1.1 tls addch('\b');
304 1.1 tls }
305 1.1 tls clrtoeol();
306 1.1 tls break;
307 1.1 tls case '\b':
308 1.1 tls case 0x7f: /* DEL */
309 1.1 tls if (cp == buf + 1) {
310 1.1 tls cp--;
311 1.1 tls continue;
312 1.1 tls }
313 1.1 tls cp -= 2;
314 1.1 tls addch('\b');
315 1.1 tls c = ' ';
316 1.1 tls /* FALLTHROUGH */
317 1.1 tls default:
318 1.1 tls addch(c);
319 1.1 tls }
320 1.1 tls refresh();
321 1.1 tls }
322 1.1 tls }
323 1.1 tls *cp = '\0';
324 1.21 rillig return c != EOF;
325 1.1 tls }
326 1.14 dholland
327 1.14 dholland /*
328 1.14 dholland * Decent (n)curses interface for the game, based on Eric S. Raymond's
329 1.14 dholland * modifications to the battleship (bs) user interface.
330 1.14 dholland */
331 1.14 dholland int
332 1.14 dholland get_coord(void)
333 1.14 dholland {
334 1.14 dholland static int curx = BSZ / 2;
335 1.14 dholland static int cury = BSZ / 2;
336 1.14 dholland int ny, nx, ch;
337 1.14 dholland
338 1.14 dholland BGOTO(cury, curx);
339 1.14 dholland refresh();
340 1.14 dholland nx = curx;
341 1.14 dholland ny = cury;
342 1.14 dholland for (;;) {
343 1.22 rillig mvprintw(BSZ + 3, (BSZ - 6) / 2, "(%c %d) ",
344 1.20 rillig 'A' + ((curx > 7) ? (curx + 1) : curx), cury + 1);
345 1.14 dholland BGOTO(cury, curx);
346 1.14 dholland
347 1.14 dholland ch = getch();
348 1.14 dholland switch (ch) {
349 1.14 dholland case 'k':
350 1.14 dholland case '8':
351 1.14 dholland case KEY_UP:
352 1.14 dholland nx = curx;
353 1.14 dholland ny = cury + 1;
354 1.14 dholland break;
355 1.14 dholland case 'j':
356 1.14 dholland case '2':
357 1.14 dholland case KEY_DOWN:
358 1.14 dholland nx = curx;
359 1.14 dholland ny = BSZ + cury - 1;
360 1.14 dholland break;
361 1.14 dholland case 'h':
362 1.14 dholland case '4':
363 1.14 dholland case KEY_LEFT:
364 1.14 dholland nx = BSZ + curx - 1;
365 1.14 dholland ny = cury;
366 1.14 dholland break;
367 1.14 dholland case 'l':
368 1.14 dholland case '6':
369 1.14 dholland case KEY_RIGHT:
370 1.14 dholland nx = curx + 1;
371 1.14 dholland ny = cury;
372 1.14 dholland break;
373 1.14 dholland case 'y':
374 1.14 dholland case '7':
375 1.14 dholland case KEY_A1:
376 1.14 dholland nx = BSZ + curx - 1;
377 1.14 dholland ny = cury + 1;
378 1.14 dholland break;
379 1.14 dholland case 'b':
380 1.14 dholland case '1':
381 1.14 dholland case KEY_C1:
382 1.14 dholland nx = BSZ + curx - 1;
383 1.14 dholland ny = BSZ + cury - 1;
384 1.14 dholland break;
385 1.14 dholland case 'u':
386 1.14 dholland case '9':
387 1.14 dholland case KEY_A3:
388 1.14 dholland nx = curx + 1;
389 1.14 dholland ny = cury + 1;
390 1.14 dholland break;
391 1.14 dholland case 'n':
392 1.14 dholland case '3':
393 1.14 dholland case KEY_C3:
394 1.14 dholland nx = curx + 1;
395 1.14 dholland ny = BSZ + cury - 1;
396 1.14 dholland break;
397 1.14 dholland case 'K':
398 1.14 dholland nx = curx;
399 1.14 dholland ny = cury + 5;
400 1.14 dholland break;
401 1.14 dholland case 'J':
402 1.14 dholland nx = curx;
403 1.14 dholland ny = BSZ + cury - 5;
404 1.14 dholland break;
405 1.14 dholland case 'H':
406 1.14 dholland nx = BSZ + curx - 5;
407 1.14 dholland ny = cury;
408 1.14 dholland break;
409 1.14 dholland case 'L':
410 1.14 dholland nx = curx + 5;
411 1.14 dholland ny = cury;
412 1.14 dholland break;
413 1.14 dholland case 'Y':
414 1.20 rillig nx = BSZ + curx - 5;
415 1.14 dholland ny = cury + 5;
416 1.14 dholland break;
417 1.14 dholland case 'B':
418 1.14 dholland nx = BSZ + curx - 5;
419 1.14 dholland ny = BSZ + cury - 5;
420 1.14 dholland break;
421 1.14 dholland case 'U':
422 1.14 dholland nx = curx + 5;
423 1.14 dholland ny = cury + 5;
424 1.14 dholland break;
425 1.14 dholland case 'N':
426 1.14 dholland nx = curx + 5;
427 1.14 dholland ny = BSZ + cury - 5;
428 1.14 dholland break;
429 1.14 dholland case '\f':
430 1.14 dholland nx = curx;
431 1.14 dholland ny = cury;
432 1.24 rillig (void)clearok(stdscr, true);
433 1.14 dholland (void)refresh();
434 1.14 dholland break;
435 1.14 dholland #if 0 /* notyet */
436 1.14 dholland case KEY_MOUSE:
437 1.14 dholland {
438 1.14 dholland MEVENT myevent;
439 1.14 dholland
440 1.14 dholland getmouse(&myevent);
441 1.22 rillig if (myevent.y >= 1 && myevent.y <= BSZ + 1 &&
442 1.22 rillig myevent.x >= 3 && myevent.x <= 2 * BSZ + 1) {
443 1.14 dholland curx = (myevent.x - 3) / 2;
444 1.14 dholland cury = BSZ - myevent.y;
445 1.14 dholland return PT(curx,cury);
446 1.14 dholland } else {
447 1.14 dholland beep();
448 1.14 dholland }
449 1.14 dholland }
450 1.14 dholland break;
451 1.14 dholland #endif /* 0 */
452 1.14 dholland case 'Q':
453 1.15 dholland case 'q':
454 1.14 dholland return RESIGN;
455 1.14 dholland case 'S':
456 1.15 dholland case 's':
457 1.14 dholland return SAVE;
458 1.14 dholland case ' ':
459 1.14 dholland case '\r':
460 1.22 rillig (void)mvaddstr(BSZ + 3, (BSZ - 6) / 2, " ");
461 1.20 rillig return PT(curx + 1, cury + 1);
462 1.20 rillig }
463 1.20 rillig
464 1.20 rillig curx = nx % BSZ;
465 1.20 rillig cury = ny % BSZ;
466 1.14 dholland }
467 1.14 dholland }
468