parse.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.2 $ on $Date: 86/11/23 17:17:59 $
  18. * $Source: /users/faustus/xchess/RCS/parse.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. * Parse a sequence of chess moves...
  24. */
  25. #include "xchess.h"
  26. bool loading_flag = false;
  27. bool loading_paused = false;
  28. static char *line;
  29. /* Load a record file in. This returns a number of things -- the board, the
  30. * list of moves, and whose turn it is.
  31. */
  32. void
  33. load_game(file)
  34. char *file;
  35. {
  36. FILE *fp;
  37. char buf[BSIZE];
  38. bool eflag;
  39. move *m;
  40. board *tmpboard = alloc(board);
  41. if (eq(file, "xchess.game") && saveflag) {
  42. message_add(win1,
  43. "Oops, I just overwrote the\nfile xchess.game...\n",
  44. true);
  45. message_add(win1, "I hope you had another copy.\n", true);
  46. return;
  47. }
  48. if (!(fp = fopen(file, "r"))) {
  49. perror(file);
  50. return;
  51. }
  52. /* Get a few lines... */
  53. fgets(buf, BSIZE, fp);
  54. message_add(win1, buf, false);
  55. if (!oneboard)
  56. message_add(win2, buf, false);
  57. fgets(buf, BSIZE, fp);
  58. message_add(win1, buf, false);
  59. if (!oneboard)
  60. message_add(win2, buf, false);
  61. fgets(buf, BSIZE, fp);
  62. if (eq(buf, "\tenglish\n"))
  63. eflag = true;
  64. else if (eq(buf, "\talgebraic\n"))
  65. eflag = false;
  66. else {
  67. fprintf(stderr, "Can't decide whether this is english...\n");
  68. return;
  69. }
  70. board_init(tmpboard);
  71. line = NULL;
  72. m = parse_file(fp, tmpboard, eflag);
  73. tfree(tmpboard);
  74. /* Now apply these moves to the board we were given... */
  75. loading_flag = true;
  76. while (m) {
  77. if (!quickflag)
  78. XSync(0);
  79. win_process(true);
  80. if (!quickflag)
  81. sleep(1);
  82. if (!loading_paused) {
  83. prog_move(m);
  84. m = m->next;
  85. }
  86. }
  87. loading_flag = false;
  88. if (line)
  89. message_add(win1, line, false);
  90. while (fgets(buf, BSIZE, fp))
  91. message_add(win1, buf, false);
  92. fclose(fp);
  93. return;
  94. }
  95. /* Given a starting position (usually the beginning board configuration),
  96. * read in a file of moves.
  97. */
  98. move *
  99. parse_file(fp, b, english)
  100. FILE *fp;
  101. board *b;
  102. bool english;
  103. {
  104. move *mvs = NULL, *end = NULL;
  105. char buf[BSIZE], *s, *t;
  106. color whosemove = WHITE;
  107. int c, i, j;
  108. while (fgets(buf, BSIZE, fp)) {
  109. if (*buf == '#')
  110. continue;
  111. s = buf;
  112. /* The move number... */
  113. if (!(t = gettok(&s)))
  114. break;
  115. if (!isdigit(*t)) {
  116. line = copy(buf);
  117. break;
  118. }
  119. if (!(t = gettok(&s)))
  120. break;
  121. if (end)
  122. end = end->next = (english ? parse_move(b, t, WHITE) :
  123. parse_imove(b, t, WHITE));
  124. else
  125. mvs = end = (english ? parse_move(b, t, WHITE) :
  126. parse_imove(b, t, WHITE));
  127. if (!end) {
  128. fprintf(stderr, "Can't parse %s\n", buf);
  129. return (NULL);
  130. }
  131. board_move(b, end);
  132. if (!(t = gettok(&s)))
  133. break;
  134. if (end)
  135. end = end->next = (english ? parse_move(b, t, BLACK) :
  136. parse_imove(b, t, BLACK));
  137. else
  138. mvs = end = (english ? parse_move(b, t, BLACK) :
  139. parse_imove(b, t, BLACK));
  140. if (!end) {
  141. fprintf(stderr, "Can't parse %s\n", buf);
  142. return (NULL);
  143. }
  144. board_move(b, end);
  145. }
  146. return (mvs);
  147. }
  148. /* Parse a move. The move format accepted is as follows -
  149. * move: spec-spec
  150. * capture: specxspec
  151. * kcastle: 2 o's
  152. * qcastle: 3 o's
  153. * A spec is either piece/pos, piece, or just pos. A pos consists of a column
  154. * name followed by a row number. If the column name is kr, kn, kb, k, q,
  155. * qb, qn, or qr, then the row number is according to the english system,
  156. * or if it is a-h then it is according to the international system.
  157. *
  158. *** As of now the spec must include the position.
  159. */
  160. move *
  161. parse_move(b, str, w)
  162. board *b;
  163. char *str;
  164. color w;
  165. {
  166. move *m = alloc(move);
  167. char *s;
  168. char spec1[16], spec2[16];
  169. int i, j;
  170. if (debug) fprintf(stderr, "parsing %s\n", str);
  171. /* Check for castles. */
  172. for (s = str, i = 0; *s; s++)
  173. if ((*s == 'o') || (*s == 'O'))
  174. i++;
  175. if (i == 2) {
  176. m->type = KCASTLE;
  177. m->piece.type = KING;
  178. m->piece.color = w;
  179. return (m);
  180. } else if (i == 3) {
  181. m->type = QCASTLE;
  182. m->piece.type = KING;
  183. m->piece.color = w;
  184. return (m);
  185. }
  186. if (index(str, '-'))
  187. m->type = MOVE;
  188. else if (index(str, 'x'))
  189. m->type = CAPTURE;
  190. else
  191. return (NULL);
  192. for (i = 0; str[i]; i++)
  193. if ((str[i] == 'x') || (str[i] == '-'))
  194. break;
  195. else
  196. spec1[i] = str[i];
  197. spec1[i] = '\0';
  198. for (i++, j = 0; str[i]; i++, j++)
  199. if ((str[i] == 'x') || (str[i] == '-'))
  200. break;
  201. else
  202. spec2[j] = str[i];
  203. spec2[j] = '\0';
  204. /* Now decode the specifications. */
  205. s = spec1;
  206. switch (*s) {
  207. case 'p': case 'P':
  208. m->piece.type = PAWN; break;
  209. case 'r': case 'R':
  210. m->piece.type = ROOK; break;
  211. case 'n': case 'N':
  212. m->piece.type = KNIGHT; break;
  213. case 'b': case 'B':
  214. m->piece.type = BISHOP; break;
  215. case 'q': case 'Q':
  216. m->piece.type = QUEEN; break;
  217. case 'k': case 'K':
  218. m->piece.type = KING; break;
  219. default:
  220. return (NULL);
  221. }
  222. m->piece.color = w;
  223. s += 2;
  224. /* Now get the {q,k}{,b,n,r}n string... */
  225. if ((s[0] == 'q') && (s[1] == 'r'))
  226. m->fromx = 0, s += 2;
  227. else if ((s[0] == 'q') && (s[1] == 'n'))
  228. m->fromx = 1, s += 2;
  229. else if ((s[0] == 'q') && (s[1] == 'b'))
  230. m->fromx = 2, s += 2;
  231. else if ((s[0] == 'q') && isdigit(s[1]))
  232. m->fromx = 3, s += 1;
  233. else if ((s[0] == 'k') && isdigit(s[1]))
  234. m->fromx = 4, s += 1;
  235. else if ((s[0] == 'k') && (s[1] == 'b'))
  236. m->fromx = 5, s += 2;
  237. else if ((s[0] == 'k') && (s[1] == 'n'))
  238. m->fromx = 6, s += 2;
  239. else if ((s[0] == 'k') && (s[1] == 'r'))
  240. m->fromx = 7, s += 2;
  241. m->fromy = ((w == WHITE) ? (SIZE - atoi(s)) : (atoi(s) - 1));
  242. if ((b->square[m->fromy][m->fromx].color != w) ||
  243. (b->square[m->fromy][m->fromx].type != m->piece.type)) {
  244. fprintf(stderr, "Error: bad stuff\n");
  245. return (NULL);
  246. }
  247. s = spec2;
  248. if (m->type == CAPTURE) {
  249. switch (*s) {
  250. case 'p': case 'P':
  251. m->taken.type = PAWN; break;
  252. case 'r': case 'R':
  253. m->taken.type = ROOK; break;
  254. case 'n': case 'N':
  255. m->taken.type = KNIGHT; break;
  256. case 'b': case 'B':
  257. m->taken.type = BISHOP; break;
  258. case 'q': case 'Q':
  259. m->taken.type = QUEEN; break;
  260. case 'k': case 'K':
  261. m->taken.type = KING; break;
  262. default:
  263. return (NULL);
  264. }
  265. m->taken.color = ((w == WHITE) ? BLACK : WHITE);
  266. s += 2;
  267. }
  268. /* Now get the {q,k}{,b,n,r}n string... */
  269. if ((s[0] == 'q') && (s[1] == 'r'))
  270. m->tox = 0, s += 2;
  271. else if ((s[0] == 'q') && (s[1] == 'n'))
  272. m->tox = 1, s += 2;
  273. else if ((s[0] == 'q') && (s[1] == 'b'))
  274. m->tox = 2, s += 2;
  275. else if ((s[0] == 'q') && isdigit(s[1]))
  276. m->tox = 3, s += 1;
  277. else if ((s[0] == 'k') && isdigit(s[1]))
  278. m->tox = 4, s += 1;
  279. else if ((s[0] == 'k') && (s[1] == 'b'))
  280. m->tox = 5, s += 2;
  281. else if ((s[0] == 'k') && (s[1] == 'n'))
  282. m->tox = 6, s += 2;
  283. else if ((s[0] == 'k') && (s[1] == 'r'))
  284. m->tox = 7, s += 2;
  285. m->toy = ((w == WHITE) ? (SIZE - atoi(s)) : (atoi(s) - 1));
  286. if ((m->type == CAPTURE) && ((b->square[m->toy][m->tox].color !=
  287. m->taken.color) || (b->square[m->toy][m->tox].type !=
  288. m->taken.type))) {
  289. fprintf(stderr, "Error: bad stuff\n");
  290. return (NULL);
  291. }
  292. return (m);
  293. }
  294. /* Parse an algebraic notation move. This is a lot easier... */
  295. move *
  296. parse_imove(b, buf, w)
  297. board *b;
  298. char *buf;
  299. color w;
  300. {
  301. char *s;
  302. move *m = alloc(move);
  303. int n;
  304. if (debug) fprintf(stderr, "(alg) parsing %s\n", buf);
  305. for (s = buf, n = 0; *s; s++)
  306. if ((*s == 'o') || (*s == 'O'))
  307. n++;
  308. s = buf;
  309. if (n == 2)
  310. m->type = KCASTLE;
  311. else if (n == 3)
  312. m->type = QCASTLE;
  313. else {
  314. m->fromx = *s++ - 'a';
  315. m->fromy = SIZE - (*s++ - '0');
  316. m->tox = *s++ - 'a';
  317. m->toy = SIZE - (*s++ - '0');
  318. m->piece = b->square[m->fromy][m->fromx];
  319. m->taken = b->square[m->toy][m->tox];
  320. if (m->taken.color == NONE)
  321. m->type = MOVE;
  322. else
  323. m->type = CAPTURE;
  324. }
  325. if (m->piece.color != w) {
  326. fprintf(stderr, "Error: parse_imove: piece of wrong color!\n");
  327. return (NULL);
  328. }
  329. if ((m->piece.type == KING) && (m->fromy == m->toy) && (m->fromx == 4)
  330. && (m->tox == 6))
  331. m->type = KCASTLE;
  332. else if ((m->piece.type == KING) && (m->fromy == m->toy) &&
  333. (m->fromx == 4) && (m->tox == 2))
  334. m->type = QCASTLE;
  335. return (m);
  336. }