chessboard.pde 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include <SPI.h>
  2. #include <GD.h>
  3. #include "Wood32.h"
  4. #include "staunton.h" // Chess pieces from eboard's Staunton set: http://www.bergo.eng.br
  5. #define digits (sizeof(staunton_img) / 256)
  6. #include "digits.h"
  7. int atxy(int x, int y)
  8. {
  9. return (y << 6) + x;
  10. }
  11. static void square(byte x, byte y, byte light)
  12. {
  13. prog_uchar *src = Wood32_pic + (16 * light);
  14. int addr = atxy(x, y);
  15. GD.copy(addr + 0 * 64, src, 4);
  16. GD.copy(addr + 1 * 64, src + 4, 4);
  17. GD.copy(addr + 2 * 64, src + 8, 4);
  18. GD.copy(addr + 3 * 64, src + 12, 4);
  19. }
  20. #define QROOK 0
  21. #define QKNIGHT 1
  22. #define QBISHOP 2
  23. #define QUEEN 3
  24. #define KING 4
  25. #define KBISHOP 5
  26. #define KKNIGHT 6
  27. #define KROOK 7
  28. #define PAWN 8
  29. #define WHITE 0x00
  30. #define BLACK 0x10
  31. static char board[32];
  32. static void startboard()
  33. {
  34. byte i;
  35. for (i = 0; i < 8; i++) {
  36. board[i] = 56 + i;
  37. board[8+i] = 48 + i;
  38. board[16+i] = i;
  39. board[24+i] = 8 + i;
  40. }
  41. }
  42. // Return the piece at pos, or -1 if pos is empty
  43. static char find(byte pos)
  44. {
  45. byte slot;
  46. for (slot = 0; slot < 32; slot++)
  47. if (board[slot] == pos)
  48. return slot;
  49. return -1;
  50. }
  51. byte images[16] = { 0, 1, 2, 3, 4, 2, 1, 0, 5, 5, 5, 5, 5, 5, 5, 5 };
  52. static void piece(byte slot, int x, int y)
  53. {
  54. byte i = (4 * slot);
  55. byte j = images[slot & 0xf] * 2;
  56. byte bw = (slot >> 4) & 1;
  57. GD.sprite(i, x, y, j, bw, 0);
  58. GD.sprite(i + 1, x + 16, y, j + 1, bw, 0);
  59. GD.sprite(i + 2, x, y + 16, j + 12, bw, 0);
  60. GD.sprite(i + 3, x + 16, y + 16, j + 13, bw, 0);
  61. }
  62. #define BOARDX(pos) (8 + (((pos) & 7) << 5))
  63. #define BOARDY(pos) (24 + ((((pos) >> 3) & 7) << 5))
  64. static void drawboard()
  65. {
  66. byte slot;
  67. for (slot = 0; slot < 32; slot++) {
  68. char pos = board[slot];
  69. if (pos < 0)
  70. piece(slot, 400, 400);
  71. else {
  72. piece(slot, BOARDX(pos), BOARDY(pos));
  73. }
  74. }
  75. }
  76. static float smoothstep(float x)
  77. {
  78. return x*x*(3-2*x);
  79. }
  80. // move piece 'slot' to position 'pos'.
  81. // return true if a piece was taken.
  82. static byte movepiece(byte slot, byte pos)
  83. {
  84. int x0 = BOARDX(board[slot]);
  85. int y0 = BOARDY(board[slot]);
  86. int x1 = BOARDX(pos);
  87. int y1 = BOARDY(pos);
  88. // move at 1.5 pix/frame
  89. int d = int(sqrt(pow(x0 - x1, 2) + pow(y0 - y1, 2)) / 2);
  90. int it;
  91. for (it = 0; it < d; it++) {
  92. float t = smoothstep(float(it) / d);
  93. GD.waitvblank();
  94. GD.waitvblank();
  95. piece(slot, int(x0 + t * (x1 - x0)), int(y0 + t * (y1 - y0)));
  96. }
  97. byte taken = find(pos) != -1;
  98. if (taken)
  99. board[find(pos)] = -1;
  100. board[slot] = pos;
  101. drawboard();
  102. return taken;
  103. }
  104. void setup()
  105. {
  106. int i, j;
  107. GD.begin();
  108. GD.ascii();
  109. GD.putstr(0, 0, "Chess board");
  110. GD.copy(RAM_CHR, Wood32_chr, sizeof(Wood32_chr));
  111. GD.copy(RAM_PAL, Wood32_pal, sizeof(Wood32_pal));
  112. GD.copy(RAM_SPRIMG, staunton_img, sizeof(staunton_img));
  113. GD.copy(RAM_SPRPAL, staunton_white, sizeof(staunton_white));
  114. GD.copy(RAM_SPRPAL + 512, staunton_black, sizeof(staunton_black));
  115. GD.copy(RAM_SPRIMG + (digits << 8), digits_img, sizeof(digits_img));
  116. GD.copy(RAM_SPRPAL + 2 * 512, digits_pal, sizeof(digits_pal));
  117. for (i = 0; i < 256; i++) {
  118. unsigned int b = GD.rd16(RAM_SPRPAL + 2 * 512 + 2 * i);
  119. GD.wr16(RAM_SPRPAL + 3 * 512 + 2 * i, b ^ 0x7fff);
  120. }
  121. // Draw the 64 squares of the board
  122. for (i = 0; i < 8; i++)
  123. for (j = 0; j < 8; j++)
  124. square(1 + (i << 2), 3 + (j << 2), (i ^ j) & 1);
  125. // Draw the rank and file markers 1-8 a-h
  126. for (i = 0; i < 8; i++) {
  127. GD.wr(atxy(3 + (i << 2), 2), 'a' + i);
  128. GD.wr(atxy(3 + (i << 2), 35), 'a' + i);
  129. GD.wr(atxy(0, 5 + (i << 2)), '8' - i);
  130. GD.wr(atxy(33, 5 + (i << 2)), '8' - i);
  131. }
  132. startboard();
  133. drawboard();
  134. }
  135. static int clock[2];
  136. // draw digit d in sprite slots spr,spr+1 at (x,y)
  137. static void digit(byte spr, byte d, byte bw, int x, int y)
  138. {
  139. GD.sprite(spr, x, y, digits + d, 2 + bw, 0);
  140. GD.sprite(spr + 1, x, y + 16, digits + d + 11, 2 + bw, 0);
  141. }
  142. static void showclock(byte bw)
  143. {
  144. int t = clock[bw];
  145. byte spr = 128 + (bw * 16);
  146. byte s = t % 60;
  147. int y = (bw ? 31 : 3) * 8;
  148. byte d0 = s % 10; s /= 10;
  149. digit(spr, d0, bw, 400 - 1 * 16, y);
  150. digit(spr + 2, s, bw, 400 - 2 * 16, y);
  151. digit(spr + 4, 10, bw, 400 - 3 * 16, y); // colon
  152. spr += 6;
  153. int x = 400 - 4 * 16;
  154. byte m = t / 60;
  155. do {
  156. d0 = m % 10; m /= 10;
  157. digit(spr, d0, bw, x, y);
  158. spr += 2;
  159. x -= 16;
  160. } while (m);
  161. }
  162. static int turn;
  163. #define ALG(r,f) ((r - 'a') + ((8 - f) * 8))
  164. #define CASTLE 255,255
  165. static byte game[] = {
  166. ALG('e', 2),ALG('e', 4), ALG('e', 7),ALG('e', 5),
  167. ALG('g', 1),ALG('f', 3), ALG('b', 8),ALG('c', 6),
  168. ALG('f', 1),ALG('b', 5), ALG('a', 7),ALG('a', 6),
  169. ALG('b', 5),ALG('a', 4), ALG('g', 8),ALG('f', 6),
  170. ALG('d', 1),ALG('e', 2), ALG('b', 7),ALG('b', 5),
  171. ALG('a', 4),ALG('b', 3), ALG('f', 8),ALG('e', 7),
  172. ALG('c', 2),ALG('c', 3), CASTLE,
  173. CASTLE, ALG('d', 7),ALG('d', 5),
  174. ALG('e', 4),ALG('d', 5), ALG('f', 6),ALG('d', 5),
  175. ALG('f', 3),ALG('e', 5), ALG('d', 5),ALG('f', 4),
  176. ALG('e', 2),ALG('e', 4), ALG('c', 6),ALG('e', 5),
  177. ALG('e', 4),ALG('a', 8), ALG('d', 8),ALG('d', 3),
  178. ALG('b', 3),ALG('d', 1), ALG('c', 8),ALG('h', 3),
  179. ALG('a', 8),ALG('a', 6), ALG('h', 3),ALG('g', 2),
  180. ALG('f', 1),ALG('e', 1), ALG('d', 3),ALG('f', 3),
  181. };
  182. static void putalg(byte x, byte y, byte a)
  183. {
  184. GD.wr(atxy(x, y), 'a' + (a & 7));
  185. GD.wr(atxy(x+1, y), '8' - ((a >> 3) & 7));
  186. }
  187. void loop()
  188. {
  189. byte i;
  190. for (i = random(25); i; i--) {
  191. clock[(1 & turn) ^ 1]++;
  192. GD.waitvblank();
  193. showclock(0);
  194. showclock(1);
  195. delay(20);
  196. }
  197. if (turn < (sizeof(game) / 2)) {
  198. byte yy = 8 + (turn >> 1);
  199. byte xx = (turn & 1) ? 44 : 38;
  200. byte i = 1 + (turn >> 1);
  201. if (i >= 10)
  202. GD.wr(atxy(35, yy), '0' + i / 10);
  203. GD.wr(atxy(36, yy), '0' + i % 10);
  204. GD.wr(atxy(37, yy), '.');
  205. byte from = game[2 * turn];
  206. byte to = game[2 * turn + 1];
  207. if (from != 255) {
  208. putalg(xx, yy, from);
  209. GD.wr(atxy(xx + 2, yy), movepiece(find(from), to) ? 'x' : '-');
  210. putalg(xx + 3, yy, to);
  211. } else {
  212. byte rank = (turn & 1) ? 8 : 1;
  213. movepiece(find(ALG('e', rank)), ALG('g', rank));
  214. movepiece(find(ALG('h', rank)), ALG('f', rank));
  215. GD.putstr(xx, yy, "O-O");
  216. }
  217. turn++;
  218. } else {
  219. delay(4000);
  220. setup();
  221. turn = 0;
  222. clock[0] = 0;
  223. clock[1] = 0;
  224. }
  225. }