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