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