board.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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:15 $
  18. * $Source: /users/faustus/xchess/RCS/board.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. * Stuff to deal with the board.
  24. */
  25. #include "xchess.h"
  26. board *chessboard;
  27. void
  28. board_setup()
  29. {
  30. chessboard = alloc(board);
  31. board_init(chessboard);
  32. return;
  33. }
  34. void
  35. board_init(b)
  36. board *b;
  37. {
  38. int i, j;
  39. for (i = 0; i < 2; i++)
  40. for (j = 0; j < SIZE; j++)
  41. b->square[i][j].color = BLACK;
  42. for (i = 2; i < 6; i++)
  43. for (j = 0; j < SIZE; j++)
  44. b->square[i][j].color = NONE;
  45. for (i = 6; i < 8; i++)
  46. for (j = 0; j < SIZE; j++)
  47. b->square[i][j].color = WHITE;
  48. for (i = 0; i < SIZE; i++)
  49. b->square[1][i].type = b->square[6][i].type =
  50. PAWN;
  51. b->square[0][0].type = b->square[7][0].type = ROOK;
  52. b->square[0][1].type = b->square[7][1].type = KNIGHT;
  53. b->square[0][2].type = b->square[7][2].type = BISHOP;
  54. b->square[0][3].type = b->square[7][3].type = QUEEN;
  55. b->square[0][4].type = b->square[7][4].type = KING;
  56. b->square[0][5].type = b->square[7][5].type = BISHOP;
  57. b->square[0][6].type = b->square[7][6].type = KNIGHT;
  58. b->square[0][7].type = b->square[7][7].type = ROOK;
  59. return;
  60. }
  61. void
  62. board_drawall()
  63. {
  64. int i, j;
  65. for (i = 0; i < SIZE; i++)
  66. for (j = 0; j < SIZE; j++)
  67. if (chessboard->square[i][j].color != NONE) {
  68. win_drawpiece(&chessboard->square[i][j], i,
  69. j, WHITE);
  70. if (!oneboard)
  71. win_drawpiece(&chessboard->square[i][j],
  72. i, j, BLACK);
  73. }
  74. return;
  75. }
  76. void
  77. board_move(b, m)
  78. board *b;
  79. move *m;
  80. {
  81. switch (m->type) {
  82. case MOVE:
  83. case CAPTURE:
  84. b->square[m->fromy][m->fromx].color = NONE;
  85. b->square[m->toy][m->tox].color = m->piece.color;
  86. b->square[m->toy][m->tox].type = m->piece.type;
  87. if ((m->piece.type == PAWN) && (((m->piece.color == BLACK) &&
  88. (m->toy == 7)) || ((m->piece.color == WHITE) &&
  89. (m->toy == 0))))
  90. b->square[m->toy][m->tox].type = QUEEN;
  91. if (m->enpassant)
  92. b->square[m->toy + ((m->piece.color == WHITE) ? 1 :
  93. -1)][m->tox].color = NONE;
  94. break;
  95. case KCASTLE:
  96. if (m->piece.color == WHITE) {
  97. b->square[7][5].color = m->piece.color;
  98. b->square[7][5].type = ROOK;
  99. b->square[7][6].color = m->piece.color;
  100. b->square[7][6].type = KING;
  101. b->square[7][4].color = NONE;
  102. b->square[7][7].color = NONE;
  103. } else {
  104. b->square[0][5].color = m->piece.color;
  105. b->square[0][5].type = ROOK;
  106. b->square[0][6].color = m->piece.color;
  107. b->square[0][6].type = KING;
  108. b->square[0][4].color = NONE;
  109. b->square[0][7].color = NONE;
  110. }
  111. break;
  112. case QCASTLE:
  113. if (m->piece.color == WHITE) {
  114. b->square[7][3].color = m->piece.color;
  115. b->square[7][3].type = ROOK;
  116. b->square[7][2].color = m->piece.color;
  117. b->square[7][2].type = KING;
  118. b->square[7][4].color = NONE;
  119. b->square[7][0].color = NONE;
  120. } else {
  121. b->square[0][3].color = m->piece.color;
  122. b->square[0][3].type = ROOK;
  123. b->square[0][2].color = m->piece.color;
  124. b->square[0][2].type = KING;
  125. b->square[0][4].color = NONE;
  126. b->square[0][0].color = NONE;
  127. }
  128. break;
  129. default:
  130. fprintf(stderr, "Bad move type %d\n", m->type);
  131. }
  132. if (m->piece.type == KING) {
  133. if (m->piece.color == WHITE)
  134. b->white_cant_castle_q =
  135. b->white_cant_castle_k= true;
  136. else
  137. b->black_cant_castle_q =
  138. b->black_cant_castle_k= true;
  139. } else if (m->piece.type == ROOK) {
  140. if (m->piece.color == WHITE) {
  141. if (m->fromx == 0)
  142. b->white_cant_castle_q = true;
  143. else if (m->fromx == 7)
  144. b->white_cant_castle_k = true;
  145. } else {
  146. if (m->fromx == 0)
  147. b->black_cant_castle_q = true;
  148. else if (m->fromx == 7)
  149. b->black_cant_castle_k = true;
  150. }
  151. }
  152. return;
  153. }