record.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.4 $ on $Date: 86/11/23 17:18:20 $
  18. * $Source: /users/faustus/xchess/RCS/record.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. * Deal with recording moves.
  24. */
  25. #include "xchess.h"
  26. #define smartass
  27. bool record_english = true;
  28. char *record_file = DEF_RECORD_FILE;
  29. int movenum = 0;
  30. bool saveflag = true; /* Until it's debugged ... false;*/
  31. static char *colnames[] = { "qr", "qn", "qb", "q", "k", "kb", "kn", "kr" } ;
  32. static char *pcnames[] = { "P", "R", "N", "B", "Q", "K" } ;
  33. static char *movestring();
  34. static char *tstring();
  35. static FILE *backup;
  36. #define RECORD_HEADER "1 XChess Game Record0\n\n"
  37. void
  38. record_init(win)
  39. windata *win;
  40. {
  41. int i;
  42. i = XStringWidth(RECORD_HEADER, win->medium, 0, 0);
  43. i = (40 * win->small->width - i * win->medium->width) /
  44. win->medium->width / 2;
  45. TxtGrab(win->recwin, "xchess", win->small, win->textback.pixel,
  46. win->textcolor.pixel, win->cursorcolor.pixel);
  47. TxtAddFont(win->recwin, 1, win->medium, win->textcolor.pixel);
  48. for (; i > 0; i++)
  49. TxtWriteStr(win->recwin, " ");
  50. TxtWriteStr(win->recwin, RECORD_HEADER);
  51. if (saveflag) {
  52. if (!(backup = fopen(record_file, "w"))) {
  53. perror(record_file);
  54. saveflag = false;
  55. } else {
  56. fprintf(backup, "X Chess -- %s\n", datestring());
  57. if (dispname2)
  58. fprintf(backup, "\tWhite on %s, black on %s\n",
  59. dispname1, dispname2);
  60. else
  61. fprintf(backup, "\tGame played on %s\n",
  62. dispname1);
  63. fprintf(backup, "\t%s\n", record_english ? "english" :
  64. "algebraic");
  65. fflush(backup);
  66. }
  67. }
  68. movenum = 0;
  69. return;
  70. }
  71. void
  72. record_reset()
  73. {
  74. XSetDisplay(win1->display);
  75. TxtWriteStr(win1->recwin, "\n\n1 New Game0\n\n");
  76. if (!oneboard) {
  77. XSetDisplay(win2->display);
  78. TxtWriteStr(win2->recwin, "\n\n1 New Game0\n\n");
  79. }
  80. movenum = 0;
  81. if (saveflag) {
  82. fprintf(backup, "\n\nNew Game\n\n");
  83. fflush(backup);
  84. }
  85. return;
  86. }
  87. void
  88. record_end(s)
  89. char *s;
  90. {
  91. char buf[BSIZE];
  92. sprintf(buf, "\n%s\n", s);
  93. XSetDisplay(win1->display);
  94. TxtWriteStr(win1->recwin, s);
  95. if (!oneboard) {
  96. XSetDisplay(win2->display);
  97. TxtWriteStr(win2->recwin, s);
  98. }
  99. if (saveflag) {
  100. fprintf(backup, "\n%s\n", s);
  101. fprintf(backup, "Time: white: %s, ", tstring(whiteseconds));
  102. fprintf(backup, "black: %s\n", tstring(blackseconds));
  103. fclose(backup);
  104. }
  105. return;
  106. }
  107. void
  108. record_save()
  109. {
  110. move *m;
  111. FILE *fp;
  112. int i;
  113. char *s;
  114. if (!(fp = fopen(record_file, "w"))) {
  115. perror(record_file);
  116. return;
  117. }
  118. fprintf(fp, "X Chess -- %s\n", datestring());
  119. if (dispname2)
  120. fprintf(fp, "\tWhite on %s, black on %s\n",
  121. dispname1, dispname2);
  122. else
  123. fprintf(fp, "\tGame played on %s\n", dispname1);
  124. fprintf(fp, "\t%s\n", record_english ? "english" : "algebraic");
  125. for (m = moves, i = 1; m; i++) {
  126. s = movestring(m);
  127. fprintf(fp, "%2d. %-16s ", i, s);
  128. m = m->next;
  129. if (m)
  130. s = movestring(m);
  131. else
  132. s = "";
  133. fprintf(fp, "%s\n", s);
  134. if (m)
  135. m = m->next;
  136. }
  137. fclose(fp);
  138. return;
  139. }
  140. void
  141. record_move(m)
  142. move *m;
  143. {
  144. char *s, buf[BSIZE];
  145. s = movestring(m);
  146. if (m->piece.color == WHITE) {
  147. movenum++;
  148. sprintf(buf, "%2d. %-16s ", movenum, s);
  149. } else {
  150. sprintf(buf, "%-16s\n", s);
  151. }
  152. XSetDisplay(win1->display);
  153. TxtWriteStr(win1->recwin, buf);
  154. if (!oneboard) {
  155. XSetDisplay(win2->display);
  156. TxtWriteStr(win2->recwin, buf);
  157. }
  158. if (saveflag) {
  159. fprintf(backup, "%s", buf);
  160. fflush(backup);
  161. }
  162. return;
  163. }
  164. void
  165. record_back()
  166. {
  167. char *s = (nexttomove == WHITE) ? ""
  168. : "";
  169. XSetDisplay(win1->display);
  170. TxtWriteStr(win1->recwin, s);
  171. if (!oneboard) {
  172. XSetDisplay(win2->display);
  173. TxtWriteStr(win2->recwin, s);
  174. }
  175. if (nexttomove == BLACK)
  176. movenum--;
  177. if (saveflag) {
  178. fseek(backup, (nexttomove == WHITE) ? -17 : -21, 1);
  179. fflush(backup);
  180. }
  181. return;
  182. }
  183. static char *
  184. movestring(m)
  185. move *m;
  186. {
  187. int fy, ty;
  188. static char buf[BSIZE];
  189. if (!record_english || (m->piece.color == WHITE)) {
  190. fy = SIZE - m->fromy;
  191. ty = SIZE - m->toy;
  192. } else {
  193. fy = m->fromy + 1;
  194. ty = m->toy + 1;
  195. }
  196. switch (m->type) {
  197. case MOVE:
  198. if (record_english)
  199. sprintf(buf, "%s/%s%d-%s%d%s", pcnames[(int) m->piece.
  200. type], colnames[m->fromx], fy,
  201. colnames[m->tox], ty, m->check ? "+" :
  202. "");
  203. else
  204. sprintf(buf, "%c%d%c%d", 'a' + m->fromx, fy, 'a' +
  205. m->tox, ty);
  206. break;
  207. case CAPTURE:
  208. if (record_english)
  209. sprintf(buf, "%s/%s%dx%s/%s%d%s%s",
  210. pcnames[(int) m->piece.type],
  211. colnames[m->fromx], fy,
  212. pcnames[(int) m->taken.type],
  213. colnames[m->tox], ty,
  214. m->enpassant ? "e.p." : "",
  215. m->check ? "+" : "");
  216. else
  217. sprintf(buf, "%c%d%c%d", 'a' + m->fromx, fy, 'a' +
  218. m->tox, ty);
  219. break;
  220. case KCASTLE:
  221. if (record_english)
  222. sprintf(buf, "O-O%s", m->check ? "ch" : "");
  223. else if (m->piece.color == WHITE)
  224. strcpy(buf, "e1g1");
  225. else
  226. strcpy(buf, "e8g8");
  227. break;
  228. case QCASTLE:
  229. if (record_english)
  230. sprintf(buf, "O-O-O%s", m->check ? "ch" : "");
  231. else if (m->piece.color == WHITE)
  232. strcpy(buf, "e1c1");
  233. else
  234. strcpy(buf, "e8c8");
  235. break;
  236. default:
  237. sprintf(buf, "something strange");
  238. break;
  239. }
  240. if ((m->piece.type == PAWN) && (((m->piece.color == BLACK) &&
  241. (m->toy == 7)) || ((m->piece.color == WHITE) &&
  242. (m->toy == 0))))
  243. strcat(buf, "(Q)");
  244. #ifdef smartass
  245. if (!(random() % 50))
  246. strcat(buf, "?");
  247. else if (!(random() % 50))
  248. strcat(buf, "!");
  249. else if (!(random() % 500))
  250. strcat(buf, "???");
  251. else if (!(random() % 500))
  252. strcat(buf, "!!!");
  253. #endif smartass
  254. return (buf);
  255. }
  256. static char *
  257. tstring(s)
  258. int s;
  259. {
  260. static char buf[64];
  261. if (s > 3600)
  262. sprintf(buf, "%dh %dm %ds", s / 3600, (s % 3600) / 60, s % 60);
  263. else if (s > 60)
  264. sprintf(buf, "%dm %ds", (s % 3600) / 60, s % 60);
  265. else
  266. sprintf(buf, "%ds", s);
  267. return (buf);
  268. }