xref: /original-bsd/games/gomoku/bdisp.c (revision deff14a8)
1 /*
2  * Copyright (c) 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ralph Campbell.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)bdisp.c	8.1 (Berkeley) 07/24/94";
13 #endif /* not lint */
14 
15 #include "gomoku.h"
16 #include <stdio.h>
17 #include <curses.h>
18 
19 #define	SCRNH		24		/* assume 24 lines for the moment */
20 #define	SCRNW		80		/* assume 80 chars for the moment */
21 
22 static	int	lastline;
23 
24 /*
25  * Initialize screen display.
26  */
27 cursinit()
28 {
29 
30 	initscr();
31 	noecho();
32 	cbreak();
33 	leaveok(stdscr, TRUE);
34 }
35 
36 /*
37  * Restore screen display.
38  */
39 cursfini()
40 {
41 
42 	leaveok(stdscr, FALSE);
43 	move(23, 0);
44 	clrtoeol();
45 	refresh();
46 	endwin();
47 }
48 
49 /*
50  * Initialize board display.
51  */
52 bdisp_init()
53 {
54 	register int i, j;
55 
56 	/* top border */
57 	for (i = 1; i < BSZ1; i++) {
58 		move(0, 2 * i + 1);
59 		addch(letters[i]);
60 	}
61 	/* left and right edges */
62 	for (j = BSZ1; --j > 0; ) {
63 		move(20 - j, 0);
64 		printw("%2d ", j);
65 		move(20 - j, 2 * BSZ1 + 1);
66 		printw("%d ", j);
67 	}
68 	/* bottom border */
69 	for (i = 1; i < BSZ1; i++) {
70 		move(20, 2 * i + 1);
71 		addch(letters[i]);
72 	}
73 	bdwho(0);
74 	move(0, 47);
75 	addstr("#  black  white");
76 	lastline = 0;
77 	bdisp();
78 }
79 
80 /*
81  * Update who is playing whom.
82  */
83 bdwho(update)
84 	int update;
85 {
86 	int i;
87 	extern char *plyr[];
88 
89 	move(21, 0);
90 	clrtoeol();
91 	i = 6 - strlen(plyr[BLACK]) / 2;
92 	move(21, i > 0 ? i : 0);
93 	printw("BLACK/%s", plyr[BLACK]);
94 	i = 30 - strlen(plyr[WHITE]) / 2;
95 	move(21, i);
96 	printw("WHITE/%s", plyr[WHITE]);
97 	move(21, 19);
98 	addstr(" vs. ");
99 	if (update)
100 		refresh();
101 }
102 
103 /*
104  * Update the board display after a move.
105  */
106 bdisp()
107 {
108 	register int i, j, c;
109 	register struct spotstr *sp;
110 
111 	for (j = BSZ1; --j > 0; ) {
112 		for (i = 1; i < BSZ1; i++) {
113 			move(BSZ1 - j, 2 * i + 1);
114 			sp = &board[i + j * BSZ1];
115 			if (debug > 1 && sp->s_occ == EMPTY) {
116 				if (sp->s_flg & IFLAGALL)
117 					c = '+';
118 				else if (sp->s_flg & CFLAGALL)
119 					c = '-';
120 				else
121 					c = '.';
122 			} else
123 				c = "*O.?"[sp->s_occ];
124 			addch(c);
125 		}
126 	}
127 	refresh();
128 }
129 
130 #ifdef DEBUG
131 /*
132  * Dump board display to a file.
133  */
134 bdump(fp)
135 	FILE *fp;
136 {
137 	register int i, j, c;
138 	register struct spotstr *sp;
139 
140 	/* top border */
141 	fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
142 
143 	for (j = BSZ1; --j > 0; ) {
144 		/* left edge */
145 		fprintf(fp, "%2d ", j);
146 		for (i = 1; i < BSZ1; i++) {
147 			sp = &board[i + j * BSZ1];
148 			if (debug > 1 && sp->s_occ == EMPTY) {
149 				if (sp->s_flg & IFLAGALL)
150 					c = '+';
151 				else if (sp->s_flg & CFLAGALL)
152 					c = '-';
153 				else
154 					c = '.';
155 			} else
156 				c = "*O.?"[sp->s_occ];
157 			putc(c, fp);
158 			putc(' ', fp);
159 		}
160 		/* right edge */
161 		fprintf(fp, "%d\n", j);
162 	}
163 
164 	/* bottom border */
165 	fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
166 }
167 #endif /* DEBUG */
168 
169 /*
170  * Display a transcript entry
171  */
172 dislog(str)
173 	char *str;
174 {
175 
176 	if (++lastline >= SCRNH - 1) {
177 		/* move 'em up */
178 		lastline = 1;
179 	}
180 	if (strlen(str) >= SCRNW - 46)
181 		str[SCRNW - 46 - 1] = '\0';
182 	move(lastline, 46);
183 	addstr(str);
184 	clrtoeol();
185 	move(lastline + 1, 46);
186 	clrtoeol();
187 }
188 
189 /*
190  * Display a question.
191  */
192 ask(str)
193 	char *str;
194 {
195 	int len = strlen(str);
196 
197 	move(23, 0);
198 	addstr(str);
199 	clrtoeol();
200 	move(23, len);
201 	refresh();
202 }
203 
204 getline(buf, size)
205 	char *buf;
206 	int size;
207 {
208 	register char *cp, *end;
209 	register int c;
210 	extern int interactive;
211 
212 	cp = buf;
213 	end = buf + size - 1;	/* save room for the '\0' */
214 	while (cp < end && (c = getchar()) != EOF && c != '\n' && c != '\r') {
215 		*cp++ = c;
216 		if (interactive) {
217 			switch (c) {
218 			case 0x0c: /* ^L */
219 				wrefresh(curscr);
220 				cp--;
221 				continue;
222 			case 0x15: /* ^U */
223 			case 0x18: /* ^X */
224 				while (cp > buf) {
225 					cp--;
226 					addch('\b');
227 				}
228 				clrtoeol();
229 				break;
230 			case '\b':
231 			case 0x7f: /* DEL */
232 				cp -= 2;
233 				addch('\b');
234 				c = ' ';
235 				/* FALLTHROUGH */
236 			default:
237 				addch(c);
238 			}
239 			refresh();
240 		}
241 	}
242 	*cp = '\0';
243 	return(c != EOF);
244 }
245