valid.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* This file contains code for X-CHESS.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. This file is part of X-CHESS.
  4. X-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 X-CHESS General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. X-CHESS, but only under the conditions described in the
  12. X-CHESS General Public License. A copy of this license is
  13. supposed to have been given to you along with X-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. /* RCS Info: $Revision: 1.3 $ on $Date: 86/11/23 17:18:35 $
  18. * $Source: /users/faustus/xchess/RCS/valid.c,v $
  19. * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
  20. * Permission is granted to do anything with this code except sell it
  21. * or remove this message.
  22. *
  23. * Validate a move.
  24. */
  25. #include "xchess.h"
  26. extern bool ischeck(), couldmove();
  27. bool
  28. valid_move(m, b)
  29. move *m;
  30. board *b;
  31. {
  32. board tb;
  33. /* First check that the piece can make the move at all... */
  34. if (!couldmove(m, b))
  35. return (false);
  36. /* Now see if the king is in check now. */
  37. bcopy((char *) b, (char *) &tb, sizeof (board));
  38. board_move(&tb, m);
  39. if (ischeck(&tb, m->piece.color))
  40. return (false);
  41. if (ischeck(&tb, ((m->piece.color == WHITE) ? BLACK : WHITE)))
  42. m->check = true;
  43. return (true);
  44. }
  45. static bool
  46. couldmove(m, b)
  47. move *m;
  48. board *b;
  49. {
  50. int x, y;
  51. switch (m->type) {
  52. case KCASTLE:
  53. if ((m->piece.color == WHITE) && (b->white_cant_castle_k) ||
  54. (m->piece.color == BLACK) &&
  55. (b->black_cant_castle_k))
  56. return (false);
  57. if ((b->square[m->fromy][5].color != NONE) ||
  58. (b->square[m->fromy][6].color != NONE))
  59. return (false);
  60. if (ischeck(b, m->piece.color))
  61. return (false);
  62. break;
  63. case QCASTLE:
  64. if ((m->piece.color == WHITE) && (b->white_cant_castle_q) ||
  65. (m->piece.color == BLACK) &&
  66. (b->black_cant_castle_q))
  67. return (false);
  68. if ((b->square[m->fromy][1].color != NONE) ||
  69. (b->square[m->fromy][2].color != NONE) ||
  70. (b->square[m->fromy][3].color != NONE))
  71. return (false);
  72. if (ischeck(b, m->piece.color))
  73. return (false);
  74. break;
  75. case MOVE:
  76. case CAPTURE:
  77. /* There is one special case here, that of taking a pawn
  78. * en passant. In this case we change the move field to
  79. * CAPTURE if it's ok.
  80. */
  81. switch (m->piece.type) {
  82. case PAWN:
  83. if ((m->type == MOVE) && (m->fromx == m->tox)) {
  84. /* A normal move. */
  85. if ((m->piece.color == WHITE) && (m->fromy ==
  86. m->toy + 1))
  87. break;
  88. if ((m->piece.color == WHITE) && (m->fromy ==
  89. 6) && (m->toy == 4) &&
  90. (b->square[5][m->fromx].color
  91. == NONE))
  92. break;
  93. if ((m->piece.color == BLACK) && (m->fromy ==
  94. m->toy - 1))
  95. break;
  96. if ((m->piece.color == BLACK) && (m->fromy ==
  97. 1) && (m->toy == 3) &&
  98. (b->square[2][m->fromx].color
  99. == NONE))
  100. break;
  101. return (false);
  102. } else if (m->type == CAPTURE) {
  103. if ((((m->piece.color == WHITE) && (m->fromy ==
  104. m->toy + 1)) || ((m->piece.color ==
  105. BLACK) && (m->fromy == m->toy -
  106. 1))) && ((m->fromx == m->tox + 1) ||
  107. (m->fromx == m->tox - 1)))
  108. break;
  109. /* Now maybe it's enpassant... We've already
  110. * checked for some of these things in the
  111. * calling routine.
  112. */
  113. if (m->enpassant) {
  114. if (b->square[(m->piece.color == WHITE)
  115. ? 3 : 4][m->tox].color ==
  116. ((m->piece.color == WHITE) ?
  117. BLACK : WHITE))
  118. break;
  119. }
  120. return (false);
  121. }
  122. return (false);
  123. case ROOK:
  124. if (m->fromx == m->tox) {
  125. for (y = m->fromy + ((m->fromy > m->toy) ? -1 :
  126. 1); y != m->toy; y += ((m->fromy
  127. > m->toy) ? -1 : 1))
  128. if (b->square[y][m->tox].color != NONE)
  129. return (false);
  130. break;
  131. }
  132. if (m->fromy == m->toy) {
  133. for (x = m->fromx + ((m->fromx > m->tox) ? -1 :
  134. 1); x != m->tox; x += ((m->fromx
  135. > m->tox) ? -1 : 1))
  136. if (b->square[m->toy][x].color != NONE)
  137. return (false);
  138. break;
  139. }
  140. return (false);
  141. case KNIGHT:
  142. x = m->fromx - m->tox;
  143. y = m->fromy - m->toy;
  144. if ((((x == 2) || (x == -2)) &&
  145. ((y == 1) || (y == -1))) ||
  146. (((x == 1) || (x == -1)) &&
  147. ((y == 2) || (y == -2))))
  148. break;
  149. return (false);
  150. case BISHOP:
  151. x = m->fromx - m->tox;
  152. y = m->fromy - m->toy;
  153. if ((x != y) && (x != - y))
  154. return (false);
  155. for (x = m->fromx + ((m->fromx > m->tox) ? -1 : 1), y =
  156. m->fromy + ((m->fromy > m->toy) ? -1 :
  157. 1); x != m->tox;
  158. x += ((m->fromx > m->tox) ? -1 : 1),
  159. y += ((m->fromy > m->toy) ? -1 : 1))
  160. if (b->square[y][x].color != NONE)
  161. return (false);
  162. break;
  163. case QUEEN:
  164. if (m->fromx == m->tox) {
  165. for (y = m->fromy + ((m->fromy > m->toy) ? -1 :
  166. 1); y != m->toy; y += ((m->fromy
  167. > m->toy) ? -1 : 1))
  168. if (b->square[y][m->tox].color != NONE)
  169. return (false);
  170. break;
  171. }
  172. if (m->fromy == m->toy) {
  173. for (x = m->fromx + ((m->fromx > m->tox) ? -1 :
  174. 1); x != m->tox; x += ((m->fromx
  175. > m->tox) ? -1 : 1))
  176. if (b->square[m->toy][x].color != NONE)
  177. return (false);
  178. break;
  179. }
  180. x = m->fromx - m->tox;
  181. y = m->fromy - m->toy;
  182. if ((x != y) && (x != - y))
  183. return (false);
  184. for (x = m->fromx + ((m->fromx > m->tox) ? -1 : 1), y =
  185. m->fromy + ((m->fromy > m->toy) ? -1 :
  186. 1); x != m->tox;
  187. x += ((m->fromx > m->tox) ? -1 : 1),
  188. y += ((m->fromy > m->toy) ? -1 : 1))
  189. if (b->square[y][x].color != NONE)
  190. return (false);
  191. break;
  192. case KING:
  193. x = m->fromx - m->tox;
  194. y = m->fromy - m->toy;
  195. if ((x >= -1) && (x <= 1) && (y >= -1) && (y <= 1))
  196. break;
  197. return (false);
  198. }
  199. break;
  200. }
  201. return (true);
  202. }
  203. /* Say whether either king is in check... If move is non-NULL, say whether he
  204. * in in check after the move takes place. We do this in a rather stupid way.
  205. */
  206. static bool
  207. ischeck(b, col)
  208. board *b;
  209. color col;
  210. {
  211. int x, y, kx, ky;
  212. move ch;
  213. for (x = 0; x < SIZE; x++)
  214. for (y = 0; y < SIZE; y++)
  215. if ((b->square[y][x].color == col) &&
  216. (b->square[y][x].type == KING)) {
  217. kx = x;
  218. ky = y;
  219. }
  220. for (x = 0; x < SIZE; x++)
  221. for (y = 0; y < SIZE; y++)
  222. if (b->square[y][x].color == ((col == WHITE) ?
  223. BLACK : WHITE)) {
  224. ch.type = CAPTURE;
  225. ch.piece.color = b->square[y][x].color;
  226. ch.piece.type = b->square[y][x].type;
  227. ch.fromx = x;
  228. ch.fromy = y;
  229. ch.tox = kx;
  230. ch.toy = ky;
  231. ch.enpassant = false;
  232. if (couldmove(&ch, b))
  233. return (true);
  234. }
  235. return (false);
  236. }