bdinit.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* $NetBSD: bdinit.c,v 1.5 2003/08/07 09:37:15 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[] = "from: @(#)bdinit.c 8.2 (Berkeley) 5/3/95";
  37. #else
  38. __RCSID("$NetBSD: bdinit.c,v 1.5 2003/08/07 09:37:15 agc Exp $");
  39. #endif
  40. #endif /* not lint */
  41. #include <string.h>
  42. #include "gomoku.h"
  43. void
  44. bdinit(bp)
  45. struct spotstr *bp;
  46. {
  47. int i, j, r;
  48. struct spotstr *sp;
  49. struct combostr *cbp;
  50. movenum = 1;
  51. /* mark the borders as such */
  52. sp = bp;
  53. for (i = BSZ2; --i >= 0; sp++) {
  54. sp->s_occ = BORDER; /* top border */
  55. sp->s_flg = BFLAGALL;
  56. }
  57. /* fill entire board with EMPTY spots */
  58. memset(frames, 0, sizeof(frames));
  59. cbp = frames;
  60. for (j = 0; ++j < BSZ1; sp++) { /* for each row */
  61. for (i = 0; ++i < BSZ1; sp++) { /* for each column */
  62. sp->s_occ = EMPTY;
  63. sp->s_flg = 0;
  64. sp->s_wval = 0;
  65. if (j < 5) {
  66. /* directions 1, 2, 3 are blocked */
  67. sp->s_flg |= (BFLAG << 1) | (BFLAG << 2) |
  68. (BFLAG << 3);
  69. sp->s_fval[BLACK][1].s = MAXCOMBO;
  70. sp->s_fval[BLACK][2].s = MAXCOMBO;
  71. sp->s_fval[BLACK][3].s = MAXCOMBO;
  72. sp->s_fval[WHITE][1].s = MAXCOMBO;
  73. sp->s_fval[WHITE][2].s = MAXCOMBO;
  74. sp->s_fval[WHITE][3].s = MAXCOMBO;
  75. } else if (j == 5) {
  76. /* five spaces, blocked on one side */
  77. sp->s_fval[BLACK][1].s = 0x500;
  78. sp->s_fval[BLACK][2].s = 0x500;
  79. sp->s_fval[BLACK][3].s = 0x500;
  80. sp->s_fval[WHITE][1].s = 0x500;
  81. sp->s_fval[WHITE][2].s = 0x500;
  82. sp->s_fval[WHITE][3].s = 0x500;
  83. } else {
  84. /* six spaces, not blocked */
  85. sp->s_fval[BLACK][1].s = 0x401;
  86. sp->s_fval[BLACK][2].s = 0x401;
  87. sp->s_fval[BLACK][3].s = 0x401;
  88. sp->s_fval[WHITE][1].s = 0x401;
  89. sp->s_fval[WHITE][2].s = 0x401;
  90. sp->s_fval[WHITE][3].s = 0x401;
  91. }
  92. if (i > (BSZ - 4)) {
  93. /* directions 0, 1 are blocked */
  94. sp->s_flg |= BFLAG | (BFLAG << 1);
  95. sp->s_fval[BLACK][0].s = MAXCOMBO;
  96. sp->s_fval[BLACK][1].s = MAXCOMBO;
  97. sp->s_fval[WHITE][0].s = MAXCOMBO;
  98. sp->s_fval[WHITE][1].s = MAXCOMBO;
  99. } else if (i == (BSZ - 4)) {
  100. sp->s_fval[BLACK][0].s = 0x500;
  101. sp->s_fval[WHITE][0].s = 0x500;
  102. /* if direction 1 is not blocked */
  103. if (!(sp->s_flg & (BFLAG << 1))) {
  104. sp->s_fval[BLACK][1].s = 0x500;
  105. sp->s_fval[WHITE][1].s = 0x500;
  106. }
  107. } else {
  108. sp->s_fval[BLACK][0].s = 0x401;
  109. sp->s_fval[WHITE][0].s = 0x401;
  110. if (i < 5) {
  111. /* direction 3 is blocked */
  112. sp->s_flg |= (BFLAG << 3);
  113. sp->s_fval[BLACK][3].s = MAXCOMBO;
  114. sp->s_fval[WHITE][3].s = MAXCOMBO;
  115. } else if (i == 5 &&
  116. !(sp->s_flg & (BFLAG << 3))) {
  117. sp->s_fval[BLACK][3].s = 0x500;
  118. sp->s_fval[WHITE][3].s = 0x500;
  119. }
  120. }
  121. /*
  122. * Allocate a frame structure for non blocked frames.
  123. */
  124. for (r = 4; --r >= 0; ) {
  125. if (sp->s_flg & (BFLAG << r))
  126. continue;
  127. cbp->c_combo.s = sp->s_fval[BLACK][r].s;
  128. cbp->c_vertex = sp - board;
  129. cbp->c_nframes = 1;
  130. cbp->c_dir = r;
  131. sp->s_frame[r] = cbp;
  132. cbp++;
  133. }
  134. }
  135. sp->s_occ = BORDER; /* left & right border */
  136. sp->s_flg = BFLAGALL;
  137. }
  138. /* mark the borders as such */
  139. for (i = BSZ1; --i >= 0; sp++) {
  140. sp->s_occ = BORDER; /* bottom border */
  141. sp->s_flg = BFLAGALL;
  142. }
  143. sortframes[BLACK] = (struct combostr *)0;
  144. sortframes[WHITE] = (struct combostr *)0;
  145. init_overlap();
  146. }
  147. /*
  148. * Initialize the overlap array.
  149. * Each entry in the array is a bit mask with eight bits corresponding
  150. * to whether frame B overlaps frame A (as indexed by overlap[A * FAREA + B]).
  151. * The eight bits coorespond to whether A and B are open ended (length 6) or
  152. * closed (length 5).
  153. * 0 A closed and B closed
  154. * 1 A closed and B open
  155. * 2 A open and B closed
  156. * 3 A open and B open
  157. * 4 A closed and B closed and overlaps in more than one spot
  158. * 5 A closed and B open and overlaps in more than one spot
  159. * 6 A open and B closed and overlaps in more than one spot
  160. * 7 A open and B open and overlaps in more than one spot
  161. * As pieces are played, it can make frames not overlap if there are no
  162. * common open spaces shared between the two frames.
  163. */
  164. void
  165. init_overlap()
  166. {
  167. struct spotstr *sp1, *sp2;
  168. struct combostr *cbp;
  169. int i, f, r, n, d1, d2;
  170. int mask, bmask, vertex, s;
  171. u_char *str;
  172. short *ip;
  173. memset(overlap, 0, sizeof(overlap));
  174. memset(intersect, 0, sizeof(intersect));
  175. str = &overlap[FAREA * FAREA];
  176. ip = &intersect[FAREA * FAREA];
  177. for (cbp = frames + FAREA; --cbp >= frames; ) { /* each frame */
  178. str -= FAREA;
  179. ip -= FAREA;
  180. sp1 = &board[vertex = cbp->c_vertex];
  181. d1 = dd[r = cbp->c_dir];
  182. /*
  183. * s = 5 if closed, 6 if open.
  184. * At this point black & white are the same.
  185. */
  186. s = 5 + sp1->s_fval[BLACK][r].c.b;
  187. /* for each spot in frame A */
  188. for (i = 0; i < s; i++, sp1 += d1, vertex += d1) {
  189. /* the sixth spot in frame A only overlaps if it is open */
  190. mask = (i == 5) ? 0xC : 0xF;
  191. /* for each direction */
  192. for (r = 4; --r >= 0; ) {
  193. bmask = BFLAG << r;
  194. sp2 = sp1;
  195. d2 = dd[r];
  196. /* for each frame that intersects at spot sp1 */
  197. for (f = 0; f < 6; f++, sp2 -= d2) {
  198. if (sp2->s_occ == BORDER)
  199. break;
  200. if (sp2->s_flg & bmask)
  201. continue;
  202. n = sp2->s_frame[r] - frames;
  203. ip[n] = vertex;
  204. str[n] |= (f == 5) ? mask & 0xA : mask;
  205. if (r == cbp->c_dir) {
  206. /* compute the multiple spot overlap values */
  207. switch (i) {
  208. case 0: /* sp1 is the first spot in A */
  209. if (f == 4)
  210. str[n] |= 0xA0;
  211. else if (f != 5)
  212. str[n] |= 0xF0;
  213. break;
  214. case 1: /* sp1 is the second spot in A */
  215. if (f == 5)
  216. str[n] |= 0xA0;
  217. else
  218. str[n] |= 0xF0;
  219. break;
  220. case 4: /* sp1 is the penultimate spot in A */
  221. if (f == 0)
  222. str[n] |= 0xC0;
  223. else
  224. str[n] |= 0xF0;
  225. break;
  226. case 5: /* sp1 is the last spot in A */
  227. if (f == 1)
  228. str[n] |= 0xC0;
  229. else if (f != 0)
  230. str[n] |= 0xF0;
  231. break;
  232. default:
  233. str[n] |= 0xF0;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }