bdisp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* $NetBSD: bdisp.c,v 1.8 2003/08/07 09:37:16 agc Exp $ */
  2. /*
  3. * Copyright (c) 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Ralph Campbell.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. #ifndef lint
  35. #if 0
  36. static char sccsid[] = "@(#)bdisp.c 8.2 (Berkeley) 5/3/95";
  37. #else
  38. __RCSID("$NetBSD: bdisp.c,v 1.8 2003/08/07 09:37:16 agc Exp $");
  39. #endif
  40. #endif /* not lint */
  41. #include <curses.h>
  42. #include <string.h>
  43. #include "gomoku.h"
  44. #define SCRNH 24 /* assume 24 lines for the moment */
  45. #define SCRNW 80 /* assume 80 chars for the moment */
  46. static int lastline;
  47. static char pcolor[] = "*O.?";
  48. extern int interactive;
  49. extern char *plyr[];
  50. /*
  51. * Initialize screen display.
  52. */
  53. void
  54. cursinit()
  55. {
  56. initscr();
  57. noecho();
  58. cbreak();
  59. leaveok(stdscr, TRUE);
  60. }
  61. /*
  62. * Restore screen display.
  63. */
  64. void
  65. cursfini()
  66. {
  67. leaveok(stdscr, FALSE);
  68. move(23, 0);
  69. clrtoeol();
  70. refresh();
  71. endwin();
  72. }
  73. /*
  74. * Initialize board display.
  75. */
  76. void
  77. bdisp_init()
  78. {
  79. int i, j;
  80. /* top border */
  81. for (i = 1; i < BSZ1; i++) {
  82. move(0, 2 * i + 1);
  83. addch(letters[i]);
  84. }
  85. /* left and right edges */
  86. for (j = BSZ1; --j > 0; ) {
  87. move(20 - j, 0);
  88. printw("%2d ", j);
  89. move(20 - j, 2 * BSZ1 + 1);
  90. printw("%d ", j);
  91. }
  92. /* bottom border */
  93. for (i = 1; i < BSZ1; i++) {
  94. move(20, 2 * i + 1);
  95. addch(letters[i]);
  96. }
  97. bdwho(0);
  98. move(0, 47);
  99. addstr("# black white");
  100. lastline = 0;
  101. bdisp();
  102. }
  103. /*
  104. * Update who is playing whom.
  105. */
  106. void
  107. bdwho(update)
  108. int update;
  109. {
  110. int i;
  111. move(21, 0);
  112. clrtoeol();
  113. i = 6 - strlen(plyr[BLACK]) / 2;
  114. move(21, i > 0 ? i : 0);
  115. printw("BLACK/%s", plyr[BLACK]);
  116. i = 30 - strlen(plyr[WHITE]) / 2;
  117. move(21, i);
  118. printw("WHITE/%s", plyr[WHITE]);
  119. move(21, 19);
  120. addstr(" vs. ");
  121. if (update)
  122. refresh();
  123. }
  124. /*
  125. * Update the board display after a move.
  126. */
  127. void
  128. bdisp()
  129. {
  130. int i, j, c;
  131. struct spotstr *sp;
  132. for (j = BSZ1; --j > 0; ) {
  133. for (i = 1; i < BSZ1; i++) {
  134. move(BSZ1 - j, 2 * i + 1);
  135. sp = &board[i + j * BSZ1];
  136. if (debug > 1 && sp->s_occ == EMPTY) {
  137. if (sp->s_flg & IFLAGALL)
  138. c = '+';
  139. else if (sp->s_flg & CFLAGALL)
  140. c = '-';
  141. else
  142. c = '.';
  143. } else
  144. c = pcolor[sp->s_occ];
  145. addch(c);
  146. }
  147. }
  148. refresh();
  149. }
  150. #ifdef DEBUG
  151. /*
  152. * Dump board display to a file.
  153. */
  154. void
  155. bdump(fp)
  156. FILE *fp;
  157. {
  158. int i, j, c;
  159. struct spotstr *sp;
  160. /* top border */
  161. fprintf(fp, " A B C D E F G H J K L M N O P Q R S T\n");
  162. for (j = BSZ1; --j > 0; ) {
  163. /* left edge */
  164. fprintf(fp, "%2d ", j);
  165. for (i = 1; i < BSZ1; i++) {
  166. sp = &board[i + j * BSZ1];
  167. if (debug > 1 && sp->s_occ == EMPTY) {
  168. if (sp->s_flg & IFLAGALL)
  169. c = '+';
  170. else if (sp->s_flg & CFLAGALL)
  171. c = '-';
  172. else
  173. c = '.';
  174. } else
  175. c = pcolor[sp->s_occ];
  176. putc(c, fp);
  177. putc(' ', fp);
  178. }
  179. /* right edge */
  180. fprintf(fp, "%d\n", j);
  181. }
  182. /* bottom border */
  183. fprintf(fp, " A B C D E F G H J K L M N O P Q R S T\n");
  184. }
  185. #endif /* DEBUG */
  186. /*
  187. * Display a transcript entry
  188. */
  189. void
  190. dislog(str)
  191. const char *str;
  192. {
  193. if (++lastline >= SCRNH - 1) {
  194. /* move 'em up */
  195. lastline = 1;
  196. }
  197. move(lastline, 46);
  198. addnstr(str, SCRNW - 46 - 1);
  199. clrtoeol();
  200. move(lastline + 1, 46);
  201. clrtoeol();
  202. }
  203. /*
  204. * Display a question.
  205. */
  206. void
  207. ask(str)
  208. const char *str;
  209. {
  210. int len = strlen(str);
  211. move(23, 0);
  212. addstr(str);
  213. clrtoeol();
  214. move(23, len);
  215. refresh();
  216. }
  217. int
  218. getline(buf, size)
  219. char *buf;
  220. int size;
  221. {
  222. char *cp, *end;
  223. int c;
  224. c = 0;
  225. cp = buf;
  226. end = buf + size - 1; /* save room for the '\0' */
  227. while (cp < end && (c = getchar()) != EOF && c != '\n' && c != '\r') {
  228. *cp++ = c;
  229. if (interactive) {
  230. switch (c) {
  231. case 0x0c: /* ^L */
  232. wrefresh(curscr);
  233. cp--;
  234. continue;
  235. case 0x15: /* ^U */
  236. case 0x18: /* ^X */
  237. while (cp > buf) {
  238. cp--;
  239. addch('\b');
  240. }
  241. clrtoeol();
  242. break;
  243. case '\b':
  244. case 0x7f: /* DEL */
  245. if (cp == buf + 1) {
  246. cp--;
  247. continue;
  248. }
  249. cp -= 2;
  250. addch('\b');
  251. c = ' ';
  252. /* FALLTHROUGH */
  253. default:
  254. addch(c);
  255. }
  256. refresh();
  257. }
  258. }
  259. *cp = '\0';
  260. return(c != EOF);
  261. }