genmov.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* This file contains the move-generator for CHESS.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. This file is part of CHESS.
  4. CHESS is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the CHESS General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. CHESS, but only under the conditions described in the
  12. CHESS General Public License. A copy of this license is
  13. supposed to have been given to you along with CHESS so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include <stdio.h>
  18. #include "algdefs.h"
  19. #include "fixeddefs.h"
  20. #include "crucialdefs.h"
  21. #include "externmap.h"
  22. #include "movelist.h"
  23. #include "bitmacs.h"
  24. #define UPDATE TRUE
  25. #define NOT_UPDATE FALSE
  26. int rookdirs[4] = {-1, 1, 8, -8};
  27. int bishdirs[4] = { 9, -9, 7, -7};
  28. char *strpiece[NPIECES] = {
  29. "pawn", "knight", "bishop", "rook", "queen", "king"};
  30. extern char *sqchar[];
  31. extern unsigned int sqbits[];
  32. extern okprint,sqorigin,sqdest,ngamemvs;
  33. extern struct gmlist game[MAXGAME];
  34. genpiece(update,color,sq,sqorigin,sqdest)
  35. {
  36. register k;
  37. for (k = 0; k < NPIECES; k++)
  38. if (MEMBER(sq,pieces[color][k]))
  39. {
  40. switch(k) {
  41. case KNIGHT:
  42. genknight(color,sq);
  43. break;
  44. case BISHOP:
  45. genbishop(color,sq,BISHOP);
  46. break;
  47. case ROOK:
  48. genrook(color,sq,ROOK);
  49. break;
  50. case QUEEN:
  51. genbishop(color,sq,BISHOP);
  52. genrook(color,sq,ROOK);
  53. break;
  54. case KING:
  55. genking(color,sq);
  56. break;
  57. case PAWN:
  58. if (update)
  59. genonepawn(color,sq);
  60. default:
  61. break;
  62. }
  63. break;
  64. }
  65. }
  66. /*
  67. * Incremental move updating.
  68. * Regenerate all moves for pieces that match the following
  69. * criteria:
  70. * 1. Piece last moved
  71. * 2. Piece last captured
  72. * 3. Piece attacking origination square of piece last moved
  73. * 4. Piece attacking destination square of piece last moved
  74. * 5. Pawn behind origination square of piece last moved
  75. * 6. Pawn behind destination square of piece last moved
  76. */
  77. genupdate(color,down)
  78. {
  79. register sq,sqorigin,sqdest;
  80. sqorigin = game[(down == TRUE) ? ngamemvs+1 : ngamemvs].move.from;
  81. sqdest = game[(down == TRUE) ? ngamemvs+1 : ngamemvs].move.to;
  82. if (down)
  83. {
  84. BIT_ZERO(attackfr[color][sqdest]);
  85. BIT_ZERO(pmovefr[color][sqdest]);
  86. }
  87. else {
  88. BIT_ZERO(attackfr[color][sqorigin]);
  89. BIT_ZERO(pmovefr[color][sqorigin]);
  90. BIT_ZERO(attackfr[OPP(color)][sqdest]);
  91. BIT_ZERO(pmovefr[OPP(color)][sqdest]);
  92. }
  93. if (down)
  94. {
  95. genpiece(UPDATE,color,sqorigin); /* gen square to which we move */
  96. }
  97. else {
  98. genpiece(UPDATE,color,sqdest); /* gen square to which we move */
  99. }
  100. for (sq = A1; sq <= H8; sq++)
  101. {
  102. if (MEMBER(sq,attackto[color][sqorigin])) /* gen sqs that attack */
  103. { /* origin */
  104. BIT_ZERO(attackfr[color][sq]);
  105. genpiece(UPDATE,color,sq);
  106. }
  107. if (MEMBER(sq,attackto[OPP(color)][sqorigin])) /* gen sqs that */
  108. { /* attack origin */
  109. BIT_ZERO(attackfr[color][sq]);
  110. genpiece(UPDATE,OPP(color),sq);
  111. }
  112. if (MEMBER(sq,attackto[color][sqdest])) /* gen sqs that attack */
  113. { /* attack destination */
  114. BIT_ZERO(attackfr[color][sq]);
  115. genpiece(UPDATE,color,sq);
  116. }
  117. if (MEMBER(sq,attackto[OPP(color)][sqdest])) /* gen sqs that attack */
  118. { /* destination */
  119. BIT_ZERO(attackfr[color][sq]);
  120. genpiece(UPDATE,OPP(color),sq);
  121. }
  122. }
  123. /* Now regenerate all white pawns behind of and all black pawns
  124. * in front of piece.
  125. */
  126. if (sqorigin > 7 && MEMBER(sqorigin-8,pieces[WHITE][PAWN]))
  127. {
  128. genonepawn(WHITE,sqorigin-8);
  129. }
  130. if (sqdest > 7 && MEMBER(sqdest-8,pieces[WHITE][PAWN]))
  131. {
  132. genonepawn(WHITE,sqdest-8);
  133. }
  134. if (sqorigin < 56 && MEMBER(sqorigin+8,pieces[BLACK][PAWN]))
  135. {
  136. genonepawn(BLACK,sqorigin+8);
  137. }
  138. if (sqdest < 56 && MEMBER(sqdest+8,pieces[BLACK][PAWN]))
  139. {
  140. genonepawn(BLACK,sqdest+8);
  141. }
  142. translmap(color,attackfr,attackto);
  143. translmap(color,pmovefr,pmoveto);
  144. }
  145. genside(color)
  146. {
  147. register i,k,sq;
  148. for (i = A1; i <= H8; i++)
  149. if (MEMBER(i,side[color]))
  150. {
  151. BIT_ZERO(attackfr[color][i]);
  152. genpiece(NOT_UPDATE,color,i);
  153. }
  154. genpawn(color);
  155. translmap(color,attackfr,attackto);
  156. }
  157. #define KSIDE 0
  158. #define QSIDE 1
  159. gencastle(color)
  160. {
  161. register sq,sq2,region,legcas;
  162. MAP kregion[2][2]; /* Region of squares must be free */
  163. if (color == WHITE) /* from enemy attack for castling */
  164. {
  165. kregion[KSIDE][LOBD] = e1|f1|g1;
  166. kregion[QSIDE][LOBD] = e1|d1|c1;
  167. kregion[KSIDE][HIBD] = 0;
  168. kregion[QSIDE][HIBD] = 0;
  169. }
  170. else {
  171. kregion[KSIDE][HIBD] = e8|f8|g8;
  172. kregion[QSIDE][HIBD] = e8|d8|c8;
  173. kregion[KSIDE][LOBD] = 0;
  174. kregion[QSIDE][LOBD] = 0;
  175. }
  176. for (region = KSIDE; region <= QSIDE; region++)
  177. {
  178. legcas = TRUE; /* Assume legal to castle that side */
  179. for (sq = A1; sq <= H8; sq++)
  180. if (MEMBER(sq,kregion[region]))
  181. if (NOT_ZERO(attackto[OPP(color)][sq]))
  182. {
  183. legcas = FALSE;
  184. break;
  185. }
  186. if (!legcas) printf("Castle illegal on %s %s-side\n",
  187. color == WHITE ? "white" : "black",
  188. region == KSIDE ? "king" : "queen");
  189. }
  190. }
  191. addlegals(color,sq,map,pc)
  192. MAP *map;
  193. {
  194. int nextsq, lastsq, i, gotone;
  195. switch(pc) {
  196. case ROOK:
  197. gotone = FALSE;
  198. for (i = 0; i < 4; i++)
  199. {
  200. nextsq = sq;
  201. lastsq = nextsq;
  202. nextsq = nextsq + rookdirs[i];
  203. while (nextsq >= 0 && nextsq < 64)
  204. {
  205. if ((MEMBER(lastsq,cols[0]) && MEMBER(nextsq,cols[7])) ||
  206. (MEMBER(lastsq,cols[7]) && MEMBER(nextsq,cols[0])))
  207. break;
  208. BIT_MSET(nextsq,sq,color,attackfr)
  209. if (!(MEMBER(nextsq,empty))) break;
  210. lastsq = nextsq;
  211. nextsq = nextsq + rookdirs[i];
  212. }
  213. }
  214. break;
  215. case BISHOP:
  216. gotone = FALSE;
  217. for (i = 0; i < 4; i++)
  218. {
  219. nextsq = sq;
  220. lastsq = nextsq;
  221. nextsq = nextsq + bishdirs[i];
  222. while (nextsq >= 0 && nextsq < 64)
  223. {
  224. if ((MEMBER(lastsq,cols[0]) && MEMBER(nextsq,cols[7])) ||
  225. (MEMBER(lastsq,cols[7]) && MEMBER(nextsq,cols[0])))
  226. break;
  227. BIT_MSET(nextsq,sq,color,attackfr);
  228. if (!(MEMBER(nextsq,empty)))
  229. break;
  230. lastsq = nextsq;
  231. nextsq = nextsq + bishdirs[i];
  232. }
  233. }
  234. break;
  235. default:
  236. BIT_OR(map,attackfr[color][sq]);
  237. break;
  238. }
  239. }
  240. addplegals(color,map,mapfr,incr)
  241. int color;
  242. MAP *map;
  243. unsigned int mapfr[NCOLORS][BPERW*WPERM][2];
  244. int incr;
  245. {
  246. register sq;
  247. for (sq = A1; sq <= H8; sq++)
  248. if (MEMBER(sq,map) && !(MEMBER(sq,cols[0]) &&
  249. MEMBER((sq+incr),cols[7])) && !(MEMBER((sq+incr),cols[0]) &&
  250. MEMBER(sq,cols[7])))
  251. BIT_MSET(sq,(sq+incr),color,mapfr);
  252. }
  253. genonepawn(color,sq)
  254. {
  255. register nxtsq,nxtnxtsq,leftsq,rightsq;
  256. register nxtsqi,nnsqi,leftsqi,rightsqi,prank2s,prank2e;
  257. MAP tempa[2],tempr[2];
  258. BIT_ZERO(pmovefr[color][sq]);
  259. BIT_ZERO(attackfr[color][sq]);
  260. BIT_ZERO(tempa);
  261. BIT_ZERO(tempr);
  262. if (color == WHITE)
  263. {
  264. nxtsq = sq + 8;
  265. nxtnxtsq = sq + 16;
  266. leftsq = sq + 7;
  267. rightsq = sq + 9;
  268. nxtsqi = -8;
  269. nnsqi = -16;
  270. leftsqi = -7;
  271. rightsqi = -9;
  272. prank2s = 8;
  273. prank2e = 15;
  274. }
  275. else {
  276. nxtsq = sq - 8;
  277. nxtnxtsq = sq - 16;
  278. leftsq = sq - 7;
  279. rightsq = sq - 9;
  280. nxtsqi = 8;
  281. nnsqi = 16;
  282. leftsqi = 7;
  283. rightsqi = 9;
  284. prank2s = 48;
  285. prank2e = 55;
  286. }
  287. if (MEMBER(nxtsq,empty))
  288. {
  289. BIT_S(nxtsq,tempr);
  290. addplegals(color,tempr,pmovefr,nxtsqi);
  291. if (sq >= prank2s && sq <= prank2e)
  292. if (MEMBER(nxtnxtsq,empty))
  293. {
  294. BIT_ZERO(tempr);
  295. BIT_S(nxtnxtsq,tempr);
  296. addplegals(color,tempr,pmovefr,nnsqi);
  297. }
  298. }
  299. BIT_ZERO(tempa);
  300. BIT_S(leftsq,tempa);
  301. addplegals(color,tempa,attackfr,leftsqi);
  302. BIT_ZERO(tempa);
  303. BIT_S(rightsq,tempa);
  304. addplegals(color,tempa,attackfr,rightsqi);
  305. }
  306. genpawn(color)
  307. {
  308. MAP temp[2];
  309. MAP temp2[2];
  310. register sq;
  311. for (sq = A1; sq <= H8; sq++)
  312. BIT_ZERO(pmovefr[color][sq]);
  313. switch (color) {
  314. case WHITE:
  315. BIT_LEFT(pieces[WHITE][PAWN],8,temp2);
  316. BIT_AND(empty,temp2);
  317. addplegals(color,temp2,pmovefr,-8);
  318. BIT_AND(rows[2],temp2);
  319. BIT_LEFT(temp2,8,temp2);
  320. BIT_AND(empty,temp2);
  321. addplegals(color,temp2,pmovefr,-16);
  322. BIT_LEFT(pieces[WHITE][PAWN],7,temp2);
  323. addplegals(color,temp2,attackfr,-7);
  324. BIT_LEFT(pieces[WHITE][PAWN],9,temp2);
  325. addplegals(color,temp2,attackfr,-9);
  326. break;
  327. case BLACK:
  328. BIT_RIGHT(pieces[BLACK][PAWN],8,temp2);
  329. BIT_AND(empty,temp2);
  330. addplegals(color,temp2,pmovefr,8);
  331. BIT_AND(rows[5],temp2);
  332. BIT_RIGHT(temp2,8,temp2);
  333. BIT_AND(empty,temp2);
  334. addplegals(color,temp2,pmovefr,16);
  335. BIT_RIGHT(pieces[BLACK][PAWN],7,temp2);
  336. addplegals(color,temp2,attackfr,7);
  337. BIT_RIGHT(pieces[BLACK][PAWN],9,temp2);
  338. addplegals(color,temp2,attackfr,9);
  339. break;
  340. default:
  341. break;
  342. }
  343. }
  344. genking(color,sq)
  345. {
  346. MAP temp[2];
  347. BIT_COPY(kingmvmaps[sq],temp);
  348. IFMAPNOTZERO(temp) addlegals(color,sq,temp,KING);
  349. }
  350. genknight(color,sq)
  351. {
  352. MAP temp[2];
  353. BIT_COPY(knightmvmaps[sq],temp);
  354. IFMAPNOTZERO(temp) addlegals(color,sq,temp,KNIGHT);
  355. }
  356. genbishop(color,sq,pc)
  357. {
  358. MAP temp[2];
  359. MAP leftdest[2], rightdest[2];
  360. int left,right,row,col;
  361. row = sq / 8 + 1;
  362. col = sq % 8 + 1;
  363. left = col + row - 2;
  364. right = col + (7 - row);
  365. BIT_COPY(side[color],temp);
  366. BIT_AND(diagleft[left],temp);
  367. BIT_AND(diagright[right],temp);
  368. IFMAPNOTZERO(temp) addlegals(color,sq,temp,BISHOP);
  369. }
  370. genrook(color,sq,pc)
  371. {
  372. MAP temp[2];
  373. MAP rowdest[2], coldest[2];
  374. int row, col;
  375. row = sq / 8 + 1;
  376. col = sq % 8 + 1;
  377. BIT_COPY(side[color],temp);
  378. BIT_AND(rows[row-1],temp);
  379. BIT_AND(cols[col-1],temp);
  380. IFMAPNOTZERO(temp) addlegals(color,sq,temp,ROOK);
  381. }
  382. gen()
  383. {
  384. genside(WHITE);
  385. genside(BLACK);
  386. gencastle(WHITE);
  387. gencastle(BLACK);
  388. }