control.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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:17:32 $
  18. * $Source: /users/faustus/xchess/RCS/control.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 input from the user.
  24. */
  25. #include "xchess.h"
  26. move *moves;
  27. move *foremoves;
  28. color nexttomove = WHITE;
  29. bool noisyflag = false;
  30. static move *lastmove;
  31. static move *thismove;
  32. static void screen_move();
  33. void
  34. button_pressed(event, win)
  35. XEvent *event;
  36. windata *win;
  37. {
  38. int x, y;
  39. XKeyOrButtonEvent *ev = (XKeyOrButtonEvent *) event;
  40. if (!oneboard && (win->color != nexttomove)) {
  41. message_add(win, "Wrong player!\n", true);
  42. return;
  43. }
  44. if (progflag && (nexttomove == (blackflag ? WHITE : BLACK))) {
  45. message_add(win, "Wait for the computer...\n", true);
  46. return;
  47. }
  48. if (loading_flag) {
  49. message_add(win, "You'd better not do that now...\n", true);
  50. return;
  51. }
  52. /* Figure out what piece he is pointing at. */
  53. x = ev->x / (SQUARE_WIDTH + BORDER_WIDTH);
  54. y = ev->y / (SQUARE_HEIGHT + BORDER_WIDTH);
  55. if (win->flipped) {
  56. y = SIZE - y - 1;
  57. x = SIZE - x - 1;
  58. }
  59. if ((x < 0) || (x >= SIZE) || (y < 0) || (y >= SIZE)) {
  60. fprintf(stderr, "Bad coords (%d, %d)\n", x, y);
  61. return;
  62. }
  63. if (oneboard && (chessboard->square[y][x].color != nexttomove)) {
  64. message_add(win, "Wrong player!\n", true);
  65. return;
  66. } else if (!oneboard && (chessboard->square[y][x].color !=
  67. win->color)) {
  68. message_add(win, "Can't move that\n", true);
  69. return;
  70. }
  71. thismove = alloc(move);
  72. thismove->fromx = x;
  73. thismove->fromy = y;
  74. thismove->piece.color = chessboard->square[y][x].color;
  75. thismove->piece.type = chessboard->square[y][x].type;
  76. if (debug)
  77. fprintf(stderr, "%s selected his %s at (%d, %d)...\n",
  78. colornames[(int) thismove->piece.color],
  79. piecenames[(int) thismove->piece.type],
  80. thismove->fromy, thismove->fromx);
  81. return;
  82. }
  83. void
  84. button_released(event, win)
  85. XEvent *event;
  86. windata *win;
  87. {
  88. int x, y;
  89. XKeyOrButtonEvent *ev = (XKeyOrButtonEvent *) event;
  90. if (!thismove) {
  91. /* fprintf(stderr, "Error: button hasn't been pressed\n"); */
  92. return;
  93. }
  94. if (loading_flag)
  95. return;
  96. /* Figure out what piece he is pointing at. */
  97. x = ev->x / (SQUARE_WIDTH + BORDER_WIDTH);
  98. y = ev->y / (SQUARE_HEIGHT + BORDER_WIDTH);
  99. if (win->flipped) {
  100. y = SIZE - y - 1;
  101. x = SIZE - x - 1;
  102. }
  103. if ((x < 0) || (x >= SIZE) || (y < 0) || (y >= SIZE)) {
  104. fprintf(stderr, "Bad coords (%d, %d)\n", x, y);
  105. return;
  106. }
  107. if ((thismove->fromx == x) && (thismove->fromy == y)) {
  108. message_add(win, "Hey, you touch it, you move it, buddy.\n",
  109. true);
  110. return;
  111. }
  112. if (chessboard->square[y][x].color == thismove->piece.color) {
  113. message_add(win, "Can't put one piece on top of another\n",
  114. true);
  115. return;
  116. }
  117. thismove->tox = x;
  118. thismove->toy = y;
  119. thismove->taken.color = chessboard->square[y][x].color;
  120. thismove->taken.type = chessboard->square[y][x].type;
  121. if (thismove->taken.color != NONE)
  122. thismove->type = CAPTURE;
  123. else if ((thismove->piece.type == KING) && (thismove->fromx == 4) &&
  124. (thismove->tox == 6) &&
  125. (thismove->toy == thismove->fromy))
  126. thismove->type = KCASTLE;
  127. else if ((thismove->piece.type == KING) && (thismove->tox == 2) &&
  128. (thismove->fromx == 4) &&
  129. (thismove->toy == thismove->fromy))
  130. thismove->type = QCASTLE;
  131. else
  132. thismove->type = MOVE;
  133. /* Now check the en-passant case... */
  134. if ((thismove->type == MOVE) && ((thismove->tox == thismove->fromx + 1)
  135. || (thismove->tox == thismove->fromx - 1)) &&
  136. (thismove->piece.type == PAWN) && lastmove &&
  137. (lastmove->tox == lastmove->fromx) && (lastmove->fromx
  138. == thismove->tox) && ((lastmove->fromy + lastmove->toy)
  139. / 2 == thismove->toy)) {
  140. thismove->type = CAPTURE;
  141. thismove->enpassant = true;
  142. thismove->taken = lastmove->piece;
  143. }
  144. if (!valid_move(thismove, chessboard)) {
  145. message_add(win, "Invalid move.\n", true);
  146. return;
  147. }
  148. if (debug)
  149. fprintf(stderr, "\t... and moved it to (%d, %d), type %s\n",
  150. thismove->toy, thismove->tox,
  151. movetypenames[(int) thismove->type]);
  152. move_piece(thismove);
  153. if (thismove->check) {
  154. message_add(win1, "Check.\n", true);
  155. if (!oneboard) {
  156. message_add(win2, "Check.\n", true);
  157. }
  158. }
  159. if (!moves)
  160. moves = lastmove = thismove;
  161. else
  162. lastmove = lastmove->next = thismove;
  163. if (progflag)
  164. program_send(thismove);
  165. thismove = NULL;
  166. nexttomove = ((nexttomove == WHITE) ? BLACK : WHITE);
  167. clock_switch();
  168. return;
  169. }
  170. void
  171. prog_move(m)
  172. move *m;
  173. {
  174. if (debug)
  175. fprintf(stderr, "program moves from (%d, %d) to (%d, %d)\n",
  176. m->fromy, m->fromx, m->toy, m->tox);
  177. move_piece(m);
  178. if (!moves)
  179. moves = lastmove = m;
  180. else
  181. lastmove = lastmove->next = m;
  182. nexttomove = ((nexttomove == WHITE) ? BLACK : WHITE);
  183. clock_switch();
  184. return;
  185. }
  186. void
  187. move_piece(m)
  188. move *m;
  189. {
  190. /* Update the screen... */
  191. screen_move(m);
  192. /* Move the piece on the board... */
  193. board_move(chessboard, m);
  194. /* And record it... */
  195. record_move(m);
  196. if (noisyflag)
  197. XFeep(-4);
  198. return;
  199. }
  200. static void
  201. screen_move(m)
  202. move *m;
  203. {
  204. piece pp;
  205. switch (m->type) {
  206. case CAPTURE:
  207. jail_add(&m->taken);
  208. /* FALLTHRU */
  209. case MOVE:
  210. win_erasepiece(m->fromy, m->fromx, WHITE);
  211. if (win_flashmove)
  212. win_flash(m, WHITE);
  213. win_drawpiece(&m->piece, m->toy, m->tox, WHITE);
  214. if (m->enpassant)
  215. win_erasepiece(m->toy + ((m->piece.color == WHITE) ?
  216. 1 : -1), m->tox, WHITE);
  217. if (!oneboard) {
  218. win_erasepiece(m->fromy, m->fromx, BLACK);
  219. if (win_flashmove)
  220. win_flash(m, BLACK);
  221. win_drawpiece(&m->piece, m->toy, m->tox, BLACK);
  222. if (m->enpassant)
  223. win_erasepiece(m->toy + ((m->piece.color ==
  224. WHITE) ? 1 : -1), m->tox, WHITE);
  225. }
  226. if ((m->piece.type == PAWN) && (((m->piece.color == BLACK) &&
  227. (m->toy == 7)) || ((m->piece.color == WHITE) &&
  228. (m->toy == 0)))) {
  229. pp.color = m->piece.color;
  230. pp.type = QUEEN;
  231. win_drawpiece(&pp, m->toy, m->tox, WHITE);
  232. if (!oneboard)
  233. win_drawpiece(&m->piece, m->toy, m->tox, BLACK);
  234. }
  235. break;
  236. case KCASTLE:
  237. if (m->piece.color == WHITE) {
  238. win_erasepiece(7, 4, WHITE);
  239. win_erasepiece(7, 7, WHITE);
  240. if (win_flashmove)
  241. win_flash(m, WHITE);
  242. win_drawpiece(&m->piece, 7, 6, WHITE);
  243. win_drawpiece(&chessboard->square[7][7], 7, 5, WHITE);
  244. if (!oneboard) {
  245. win_erasepiece(7, 4, BLACK);
  246. win_erasepiece(7, 7, BLACK);
  247. if (win_flashmove)
  248. win_flash(m, BLACK);
  249. win_drawpiece(&m->piece, 7, 6, BLACK);
  250. win_drawpiece(&chessboard->square[7][7], 7, 5,
  251. BLACK);
  252. }
  253. } else {
  254. win_erasepiece(0, 4, WHITE);
  255. win_erasepiece(0, 7, WHITE);
  256. if (win_flashmove)
  257. win_flash(m, WHITE);
  258. win_drawpiece(&m->piece, 0, 6, WHITE);
  259. win_drawpiece(&chessboard->square[0][7], 0, 5, WHITE);
  260. if (!oneboard) {
  261. win_erasepiece(0, 4, BLACK);
  262. win_erasepiece(0, 7, BLACK);
  263. if (win_flashmove)
  264. win_flash(m, BLACK);
  265. win_drawpiece(&m->piece, 0, 6, BLACK);
  266. win_drawpiece(&chessboard->square[0][7], 0, 5,
  267. BLACK);
  268. }
  269. }
  270. break;
  271. case QCASTLE:
  272. if (m->piece.color == WHITE) {
  273. win_erasepiece(7, 4, WHITE);
  274. win_erasepiece(7, 0, WHITE);
  275. if (win_flashmove)
  276. win_flash(m, WHITE);
  277. win_drawpiece(&m->piece, 7, 2, WHITE);
  278. win_drawpiece(&chessboard->square[7][0], 7, 3, WHITE);
  279. if (!oneboard) {
  280. win_erasepiece(7, 4, BLACK);
  281. win_erasepiece(7, 0, BLACK);
  282. if (win_flashmove)
  283. win_flash(m, BLACK);
  284. win_drawpiece(&m->piece, 7, 2, BLACK);
  285. win_drawpiece(&chessboard->square[7][7], 7, 3,
  286. BLACK);
  287. }
  288. } else {
  289. win_erasepiece(0, 4, WHITE);
  290. win_erasepiece(0, 0, WHITE);
  291. if (win_flashmove)
  292. win_flash(m, WHITE);
  293. win_drawpiece(&m->piece, 0, 2, WHITE);
  294. win_drawpiece(&chessboard->square[0][0], 0, 3, WHITE);
  295. if (!oneboard) {
  296. win_erasepiece(0, 4, BLACK);
  297. win_erasepiece(0, 0, BLACK);
  298. if (win_flashmove)
  299. win_flash(m, BLACK);
  300. win_drawpiece(&m->piece, 0, 2, BLACK);
  301. win_drawpiece(&chessboard->square[0][7], 0, 3,
  302. BLACK);
  303. }
  304. }
  305. break;
  306. default:
  307. fprintf(stderr, "Bad move type %d\n", m->type);
  308. }
  309. return;
  310. }
  311. /* Retract the last move made... */
  312. void
  313. replay()
  314. {
  315. move *m = lastmove, bm;
  316. switch (m->type) {
  317. case MOVE:
  318. bm.type = MOVE;
  319. bm.piece = m->piece;
  320. bm.fromx = m->tox;
  321. bm.fromy = m->toy;
  322. bm.tox = m->fromx;
  323. bm.toy = m->fromy;
  324. board_move(chessboard, &bm);
  325. screen_move(&bm);
  326. break;
  327. case CAPTURE:
  328. bm.type = MOVE;
  329. bm.piece = m->piece;
  330. bm.fromx = m->tox;
  331. bm.fromy = m->toy;
  332. bm.tox = m->fromx;
  333. bm.toy = m->fromy;
  334. board_move(chessboard, &bm);
  335. screen_move(&bm);
  336. chessboard->square[m->toy][m->tox] = m->taken;
  337. bm.piece = m->taken;
  338. bm.fromx = bm.tox = m->tox;
  339. bm.fromy = bm.toy = m->toy;
  340. screen_move(&bm);
  341. jail_remove(&m->taken);
  342. break;
  343. case KCASTLE:
  344. bm.type = MOVE;
  345. bm.piece.type = KING;
  346. bm.piece.color = m->piece.color;
  347. bm.fromx = 6;
  348. bm.tox = 4;
  349. bm.fromy = bm.toy = (m->piece.color == WHITE) ? 7 : 0;
  350. board_move(chessboard, &bm);
  351. screen_move(&bm);
  352. bm.type = MOVE;
  353. bm.piece.type = ROOK;
  354. bm.piece.color = m->piece.color;
  355. bm.fromx = 5;
  356. bm.tox = 7;
  357. bm.fromy = bm.toy = (m->piece.color == WHITE) ? 7 : 0;
  358. board_move(chessboard, &bm);
  359. screen_move(&bm);
  360. if (m->piece.color == WHITE)
  361. chessboard->white_cant_castle_k = false;
  362. else
  363. chessboard->black_cant_castle_k = false;
  364. break;
  365. case QCASTLE:
  366. bm.type = MOVE;
  367. bm.piece.type = KING;
  368. bm.piece.color = m->piece.color;
  369. bm.fromx = 2;
  370. bm.tox = 4;
  371. bm.fromy = bm.toy = (m->piece.color == WHITE) ? 7 : 0;
  372. board_move(chessboard, &bm);
  373. screen_move(&bm);
  374. bm.type = MOVE;
  375. bm.piece.type = ROOK;
  376. bm.piece.color = m->piece.color;
  377. bm.fromx = 3;
  378. bm.tox = 0;
  379. bm.fromy = bm.toy = (m->piece.color == WHITE) ? 7 : 0;
  380. board_move(chessboard, &bm);
  381. screen_move(&bm);
  382. if (m->piece.color == WHITE)
  383. chessboard->white_cant_castle_q = false;
  384. else
  385. chessboard->black_cant_castle_q = false;
  386. break;
  387. }
  388. record_back();
  389. nexttomove = ((nexttomove == WHITE) ? BLACK : WHITE);
  390. clock_switch();
  391. if (!moves->next) {
  392. moves->next = foremoves;
  393. foremoves = moves;
  394. moves = lastmove = NULL;
  395. } else {
  396. for (m = moves; m->next; m = m->next)
  397. lastmove = m;
  398. lastmove->next->next = foremoves;
  399. foremoves = lastmove->next;
  400. lastmove->next = NULL;
  401. }
  402. if (progflag)
  403. program_undo();
  404. return;
  405. }
  406. /* Put back the last move undone. */
  407. void
  408. forward()
  409. {
  410. prog_move(foremoves);
  411. foremoves = foremoves->next;
  412. return;
  413. }
  414. /* End the game. */
  415. void
  416. cleanup(s)
  417. char *s;
  418. {
  419. if (progflag)
  420. program_end();
  421. record_end(s);
  422. XSync(0);
  423. exit(0);
  424. }
  425. void
  426. restart()
  427. {
  428. moves = lastmove = thismove = NULL;
  429. nexttomove = WHITE;
  430. clock_init(win1, WHITE);
  431. clock_init(win1, BLACK);
  432. jail_init(win1);
  433. if (!oneboard) {
  434. clock_init(win2, WHITE);
  435. clock_init(win2, BLACK);
  436. jail_init(win2);
  437. }
  438. board_init(chessboard);
  439. win_restart();
  440. record_reset();
  441. if (progflag) {
  442. program_end();
  443. program_init(progname);
  444. }
  445. return;
  446. }