bdisp.c revision 1.33 1 1.33 rillig /* $NetBSD: bdisp.c,v 1.33 2022/05/19 19:52:56 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.33 rillig __RCSID("$NetBSD: bdisp.c,v 1.33 2022/05/19 19:52:56 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.31 rillig #define scr_y(by) (1 + (BSZ - 1) - ((by) - 1))
57 1.31 rillig #define scr_x(bx) (3 + 2 * ((bx) - 1))
58 1.31 rillig
59 1.1 tls /*
60 1.1 tls * Initialize screen display.
61 1.1 tls */
62 1.5 lukem void
63 1.10 dholland cursinit(void)
64 1.1 tls {
65 1.1 tls
66 1.24 rillig if (initscr() == NULL) {
67 1.13 dholland errx(EXIT_FAILURE, "Couldn't initialize screen");
68 1.9 drochner }
69 1.14 dholland if ((LINES < SCRNH) || (COLS < SCRNW)) {
70 1.30 rillig errx(EXIT_FAILURE, "Screen too small (need %dx%d)",
71 1.14 dholland SCRNW, SCRNH);
72 1.14 dholland }
73 1.24 rillig keypad(stdscr, true);
74 1.14 dholland nonl();
75 1.1 tls noecho();
76 1.1 tls cbreak();
77 1.29 rillig leaveok(stdscr, false);
78 1.14 dholland
79 1.14 dholland #if 0 /* no mouse support in netbsd curses yet */
80 1.14 dholland mousemask(BUTTON1_CLICKED, NULL);
81 1.14 dholland #endif
82 1.1 tls }
83 1.1 tls
84 1.1 tls /*
85 1.1 tls * Restore screen display.
86 1.1 tls */
87 1.5 lukem void
88 1.10 dholland cursfini(void)
89 1.1 tls {
90 1.1 tls
91 1.22 rillig move(BSZ + 4, 0);
92 1.1 tls clrtoeol();
93 1.1 tls refresh();
94 1.14 dholland echo();
95 1.1 tls endwin();
96 1.1 tls }
97 1.1 tls
98 1.1 tls /*
99 1.1 tls * Initialize board display.
100 1.1 tls */
101 1.5 lukem void
102 1.10 dholland bdisp_init(void)
103 1.1 tls {
104 1.1 tls
105 1.1 tls /* top border */
106 1.28 rillig for (int i = 1; i < BSZ + 1; i++) {
107 1.31 rillig move(scr_y(BSZ + 1), scr_x(i));
108 1.1 tls addch(letters[i]);
109 1.1 tls }
110 1.1 tls /* left and right edges */
111 1.28 rillig for (int j = BSZ + 1; --j > 0; ) {
112 1.31 rillig move(scr_y(j), 0);
113 1.1 tls printw("%2d ", j);
114 1.31 rillig move(scr_y(j), scr_x(BSZ) + 2);
115 1.1 tls printw("%d ", j);
116 1.1 tls }
117 1.1 tls /* bottom border */
118 1.28 rillig for (int i = 1; i < BSZ + 1; i++) {
119 1.31 rillig move(scr_y(0), scr_x(i));
120 1.1 tls addch(letters[i]);
121 1.1 tls }
122 1.24 rillig bdwho(false);
123 1.31 rillig move(0, TRANSCRIPT_COL + 1);
124 1.1 tls addstr("# black white");
125 1.1 tls lastline = 0;
126 1.1 tls bdisp();
127 1.1 tls }
128 1.1 tls
129 1.1 tls /*
130 1.1 tls * Update who is playing whom.
131 1.1 tls */
132 1.5 lukem void
133 1.26 rillig bdwho(bool update)
134 1.1 tls {
135 1.33 rillig int bw = (int)strlen(plyr[BLACK]);
136 1.33 rillig int ww = (int)strlen(plyr[WHITE]);
137 1.33 rillig int available = 3 + (1 + scr_x(BSZ) - scr_x(1)) + 3;
138 1.33 rillig int fixed = (int)sizeof("BLACK/ (*) vs. WHITE/ (O)") - 1;
139 1.33 rillig int total = fixed + bw + ww;
140 1.1 tls
141 1.31 rillig move(BSZ + 2, 0);
142 1.33 rillig hline(' ', available);
143 1.33 rillig
144 1.33 rillig if (total <= available) {
145 1.33 rillig move(BSZ + 2, (available - total) / 2);
146 1.14 dholland printw("BLACK/%s (*) vs. WHITE/%s (O)",
147 1.14 dholland plyr[BLACK], plyr[WHITE]);
148 1.14 dholland } else {
149 1.33 rillig int remaining = available - fixed;
150 1.33 rillig int half = remaining / 2;
151 1.33 rillig
152 1.33 rillig if (bw <= half)
153 1.33 rillig ww = remaining - bw;
154 1.33 rillig else if (ww <= half)
155 1.33 rillig bw = remaining - ww;
156 1.33 rillig else
157 1.33 rillig bw = half, ww = remaining - half;
158 1.33 rillig
159 1.31 rillig move(BSZ + 2, 0);
160 1.14 dholland printw("BLACK/%.*s (*) vs. WHITE/%.*s (O)",
161 1.33 rillig bw, plyr[BLACK], ww, plyr[WHITE]);
162 1.14 dholland }
163 1.1 tls if (update)
164 1.1 tls refresh();
165 1.1 tls }
166 1.1 tls
167 1.1 tls /*
168 1.1 tls * Update the board display after a move.
169 1.1 tls */
170 1.5 lukem void
171 1.10 dholland bdisp(void)
172 1.1 tls {
173 1.28 rillig int c;
174 1.5 lukem struct spotstr *sp;
175 1.1 tls
176 1.28 rillig for (int j = BSZ + 1; --j > 0; ) {
177 1.28 rillig for (int i = 1; i < BSZ + 1; i++) {
178 1.31 rillig move(scr_y(j), scr_x(i));
179 1.22 rillig sp = &board[i + j * (BSZ + 1)];
180 1.1 tls if (debug > 1 && sp->s_occ == EMPTY) {
181 1.24 rillig if ((sp->s_flags & IFLAGALL) != 0)
182 1.1 tls c = '+';
183 1.24 rillig else if ((sp->s_flags & CFLAGALL) != 0)
184 1.1 tls c = '-';
185 1.1 tls else
186 1.1 tls c = '.';
187 1.1 tls } else
188 1.1 tls c = pcolor[sp->s_occ];
189 1.27 rillig if (movenum > 1 && movelog[movenum - 2] == PT(i, j)) {
190 1.27 rillig attron(A_BOLD);
191 1.27 rillig addch(c);
192 1.27 rillig attroff(A_BOLD);
193 1.27 rillig } else
194 1.27 rillig addch(c);
195 1.1 tls }
196 1.1 tls }
197 1.1 tls refresh();
198 1.1 tls }
199 1.1 tls
200 1.1 tls #ifdef DEBUG
201 1.1 tls /*
202 1.1 tls * Dump board display to a file.
203 1.1 tls */
204 1.5 lukem void
205 1.10 dholland bdump(FILE *fp)
206 1.1 tls {
207 1.28 rillig int c;
208 1.5 lukem struct spotstr *sp;
209 1.1 tls
210 1.1 tls /* top border */
211 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");
212 1.1 tls
213 1.28 rillig for (int j = BSZ + 1; --j > 0; ) {
214 1.1 tls /* left edge */
215 1.1 tls fprintf(fp, "%2d ", j);
216 1.28 rillig for (int i = 1; i < BSZ + 1; i++) {
217 1.22 rillig sp = &board[i + j * (BSZ + 1)];
218 1.1 tls if (debug > 1 && sp->s_occ == EMPTY) {
219 1.25 rillig if ((sp->s_flags & IFLAGALL) != 0)
220 1.1 tls c = '+';
221 1.25 rillig else if ((sp->s_flags & CFLAGALL) != 0)
222 1.1 tls c = '-';
223 1.1 tls else
224 1.1 tls c = '.';
225 1.1 tls } else
226 1.1 tls c = pcolor[sp->s_occ];
227 1.1 tls putc(c, fp);
228 1.1 tls putc(' ', fp);
229 1.1 tls }
230 1.1 tls /* right edge */
231 1.1 tls fprintf(fp, "%d\n", j);
232 1.1 tls }
233 1.1 tls
234 1.1 tls /* bottom border */
235 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");
236 1.1 tls }
237 1.1 tls #endif /* DEBUG */
238 1.1 tls
239 1.1 tls /*
240 1.1 tls * Display a transcript entry
241 1.1 tls */
242 1.5 lukem void
243 1.10 dholland dislog(const char *str)
244 1.1 tls {
245 1.1 tls
246 1.1 tls if (++lastline >= SCRNH - 1) {
247 1.1 tls /* move 'em up */
248 1.1 tls lastline = 1;
249 1.1 tls }
250 1.14 dholland move(lastline, TRANSCRIPT_COL);
251 1.14 dholland addnstr(str, SCRNW - TRANSCRIPT_COL - 1);
252 1.1 tls clrtoeol();
253 1.14 dholland move(lastline + 1, TRANSCRIPT_COL);
254 1.1 tls clrtoeol();
255 1.1 tls }
256 1.1 tls
257 1.1 tls /*
258 1.1 tls * Display a question.
259 1.1 tls */
260 1.5 lukem
261 1.5 lukem void
262 1.10 dholland ask(const char *str)
263 1.1 tls {
264 1.23 rillig int len = (int)strlen(str);
265 1.1 tls
266 1.22 rillig move(BSZ + 4, 0);
267 1.1 tls addstr(str);
268 1.1 tls clrtoeol();
269 1.22 rillig move(BSZ + 4, len);
270 1.1 tls refresh();
271 1.1 tls }
272 1.1 tls
273 1.5 lukem int
274 1.15 dholland get_key(const char *allowed)
275 1.15 dholland {
276 1.15 dholland int ch;
277 1.15 dholland
278 1.19 rillig for (;;) {
279 1.15 dholland ch = getch();
280 1.15 dholland if (allowed != NULL &&
281 1.15 dholland ch != '\0' && strchr(allowed, ch) == NULL) {
282 1.15 dholland beep();
283 1.15 dholland refresh();
284 1.15 dholland continue;
285 1.15 dholland }
286 1.15 dholland break;
287 1.15 dholland }
288 1.15 dholland return ch;
289 1.15 dholland }
290 1.15 dholland
291 1.26 rillig bool
292 1.12 roy get_line(char *buf, int size)
293 1.1 tls {
294 1.5 lukem char *cp, *end;
295 1.5 lukem int c;
296 1.1 tls
297 1.5 lukem c = 0;
298 1.1 tls cp = buf;
299 1.1 tls end = buf + size - 1; /* save room for the '\0' */
300 1.1 tls while (cp < end && (c = getchar()) != EOF && c != '\n' && c != '\r') {
301 1.1 tls *cp++ = c;
302 1.1 tls if (interactive) {
303 1.1 tls switch (c) {
304 1.1 tls case 0x0c: /* ^L */
305 1.1 tls wrefresh(curscr);
306 1.1 tls cp--;
307 1.1 tls continue;
308 1.1 tls case 0x15: /* ^U */
309 1.1 tls case 0x18: /* ^X */
310 1.1 tls while (cp > buf) {
311 1.1 tls cp--;
312 1.1 tls addch('\b');
313 1.1 tls }
314 1.1 tls clrtoeol();
315 1.1 tls break;
316 1.1 tls case '\b':
317 1.1 tls case 0x7f: /* DEL */
318 1.1 tls if (cp == buf + 1) {
319 1.1 tls cp--;
320 1.1 tls continue;
321 1.1 tls }
322 1.1 tls cp -= 2;
323 1.1 tls addch('\b');
324 1.1 tls c = ' ';
325 1.1 tls /* FALLTHROUGH */
326 1.1 tls default:
327 1.1 tls addch(c);
328 1.1 tls }
329 1.1 tls refresh();
330 1.1 tls }
331 1.1 tls }
332 1.1 tls *cp = '\0';
333 1.21 rillig return c != EOF;
334 1.1 tls }
335 1.14 dholland
336 1.14 dholland /*
337 1.14 dholland * Decent (n)curses interface for the game, based on Eric S. Raymond's
338 1.14 dholland * modifications to the battleship (bs) user interface.
339 1.14 dholland */
340 1.14 dholland int
341 1.14 dholland get_coord(void)
342 1.14 dholland {
343 1.31 rillig /* XXX: These coordinates are 0-based, all others are 1-based. */
344 1.14 dholland static int curx = BSZ / 2;
345 1.14 dholland static int cury = BSZ / 2;
346 1.14 dholland int ny, nx, ch;
347 1.14 dholland
348 1.31 rillig move(scr_y(cury + 1), scr_x(curx + 1));
349 1.14 dholland refresh();
350 1.14 dholland nx = curx;
351 1.14 dholland ny = cury;
352 1.14 dholland for (;;) {
353 1.22 rillig mvprintw(BSZ + 3, (BSZ - 6) / 2, "(%c %d) ",
354 1.32 rillig letters[curx + 1], cury + 1);
355 1.31 rillig move(scr_y(cury + 1), scr_x(curx + 1));
356 1.14 dholland
357 1.14 dholland ch = getch();
358 1.14 dholland switch (ch) {
359 1.14 dholland case 'k':
360 1.14 dholland case '8':
361 1.14 dholland case KEY_UP:
362 1.14 dholland nx = curx;
363 1.14 dholland ny = cury + 1;
364 1.14 dholland break;
365 1.14 dholland case 'j':
366 1.14 dholland case '2':
367 1.14 dholland case KEY_DOWN:
368 1.14 dholland nx = curx;
369 1.14 dholland ny = BSZ + cury - 1;
370 1.14 dholland break;
371 1.14 dholland case 'h':
372 1.14 dholland case '4':
373 1.14 dholland case KEY_LEFT:
374 1.14 dholland nx = BSZ + curx - 1;
375 1.14 dholland ny = cury;
376 1.14 dholland break;
377 1.14 dholland case 'l':
378 1.14 dholland case '6':
379 1.14 dholland case KEY_RIGHT:
380 1.14 dholland nx = curx + 1;
381 1.14 dholland ny = cury;
382 1.14 dholland break;
383 1.14 dholland case 'y':
384 1.14 dholland case '7':
385 1.14 dholland case KEY_A1:
386 1.14 dholland nx = BSZ + curx - 1;
387 1.14 dholland ny = cury + 1;
388 1.14 dholland break;
389 1.14 dholland case 'b':
390 1.14 dholland case '1':
391 1.14 dholland case KEY_C1:
392 1.14 dholland nx = BSZ + curx - 1;
393 1.14 dholland ny = BSZ + cury - 1;
394 1.14 dholland break;
395 1.14 dholland case 'u':
396 1.14 dholland case '9':
397 1.14 dholland case KEY_A3:
398 1.14 dholland nx = curx + 1;
399 1.14 dholland ny = cury + 1;
400 1.14 dholland break;
401 1.14 dholland case 'n':
402 1.14 dholland case '3':
403 1.14 dholland case KEY_C3:
404 1.14 dholland nx = curx + 1;
405 1.14 dholland ny = BSZ + cury - 1;
406 1.14 dholland break;
407 1.14 dholland case 'K':
408 1.14 dholland nx = curx;
409 1.14 dholland ny = cury + 5;
410 1.14 dholland break;
411 1.14 dholland case 'J':
412 1.14 dholland nx = curx;
413 1.14 dholland ny = BSZ + cury - 5;
414 1.14 dholland break;
415 1.14 dholland case 'H':
416 1.14 dholland nx = BSZ + curx - 5;
417 1.14 dholland ny = cury;
418 1.14 dholland break;
419 1.14 dholland case 'L':
420 1.14 dholland nx = curx + 5;
421 1.14 dholland ny = cury;
422 1.14 dholland break;
423 1.14 dholland case 'Y':
424 1.20 rillig nx = BSZ + curx - 5;
425 1.14 dholland ny = cury + 5;
426 1.14 dholland break;
427 1.14 dholland case 'B':
428 1.14 dholland nx = BSZ + curx - 5;
429 1.14 dholland ny = BSZ + cury - 5;
430 1.14 dholland break;
431 1.14 dholland case 'U':
432 1.14 dholland nx = curx + 5;
433 1.14 dholland ny = cury + 5;
434 1.14 dholland break;
435 1.14 dholland case 'N':
436 1.14 dholland nx = curx + 5;
437 1.14 dholland ny = BSZ + cury - 5;
438 1.14 dholland break;
439 1.14 dholland case '\f':
440 1.14 dholland nx = curx;
441 1.14 dholland ny = cury;
442 1.24 rillig (void)clearok(stdscr, true);
443 1.14 dholland (void)refresh();
444 1.14 dholland break;
445 1.14 dholland #if 0 /* notyet */
446 1.14 dholland case KEY_MOUSE:
447 1.14 dholland {
448 1.14 dholland MEVENT myevent;
449 1.14 dholland
450 1.14 dholland getmouse(&myevent);
451 1.22 rillig if (myevent.y >= 1 && myevent.y <= BSZ + 1 &&
452 1.22 rillig myevent.x >= 3 && myevent.x <= 2 * BSZ + 1) {
453 1.14 dholland curx = (myevent.x - 3) / 2;
454 1.14 dholland cury = BSZ - myevent.y;
455 1.14 dholland return PT(curx,cury);
456 1.14 dholland } else {
457 1.14 dholland beep();
458 1.14 dholland }
459 1.14 dholland }
460 1.14 dholland break;
461 1.14 dholland #endif /* 0 */
462 1.14 dholland case 'Q':
463 1.15 dholland case 'q':
464 1.14 dholland return RESIGN;
465 1.14 dholland case 'S':
466 1.15 dholland case 's':
467 1.14 dholland return SAVE;
468 1.14 dholland case ' ':
469 1.14 dholland case '\r':
470 1.22 rillig (void)mvaddstr(BSZ + 3, (BSZ - 6) / 2, " ");
471 1.20 rillig return PT(curx + 1, cury + 1);
472 1.20 rillig }
473 1.20 rillig
474 1.20 rillig curx = nx % BSZ;
475 1.20 rillig cury = ny % BSZ;
476 1.14 dholland }
477 1.14 dholland }
478