five-line-canvas.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
  6. <title>五道</title>
  7. <style>
  8. body {
  9. background-color: transparent;
  10. }
  11. canvas {
  12. display: block;
  13. margin: 50px auto;
  14. box-shadow: -2px -2px 2px #EFEFEF, 5px 5px 5px #B9B9B9;
  15. cursor: pointer;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="chess" width="500px" height="500px">
  21. </canvas>
  22. <div id="round" style="margin: 50px auto;"></div>
  23. </body>
  24. <script>
  25. var chess = document.getElementById("chess");
  26. var context = chess.getContext('2d');
  27. var rect = chess.getBoundingClientRect();
  28. context.strokeStyle = '#bfbfbf'; //边框颜色
  29. var chressBord = []; //棋盘
  30. var whitechress = 10;
  31. var blackchress = 20;
  32. var whiteactive = 11;
  33. var blackactive = 21;
  34. var nochress = 0;
  35. var offset = 50;
  36. var lineSpace = 100;
  37. var chressSize = 30;
  38. var round = {};
  39. Object.defineProperties(round, {
  40. isWhite: {
  41. configurable: true,
  42. get: function () {
  43. return isWhite;
  44. },
  45. set: function (newValue) {
  46. isWhite = newValue;
  47. document.getElementById("round").innerText = newValue ? '白棋回合' : '黑棋回合';
  48. }
  49. }
  50. });
  51. round.isWhite = true;
  52. var activeChress = false;
  53. var ax;
  54. var ay;
  55. var ac;
  56. for (var x = 0; x < 5; x++) {
  57. chressBord[x] = [];
  58. for (var y = 0; y < 5; y++) {
  59. if (y == 0) {
  60. chressBord[x][y] = whitechress;
  61. } else if (y == 4) {
  62. chressBord[x][y] = blackchress;
  63. } else {
  64. chressBord[x][y] = nochress;
  65. }
  66. }
  67. }
  68. window.onload = function () {
  69. drawChessBoard(); // 画棋盘
  70. drawChress();//画棋子
  71. context.save();
  72. };
  73. chess.onclick = function (e) {
  74. let x = Math.floor(e.clientX - rect.left);
  75. let y = Math.floor(e.clientY - rect.top);
  76. let xx = Math.floor(Math.abs((x)) / lineSpace);
  77. let yy = Math.floor(Math.abs((y)) / lineSpace);
  78. if (activeChress) {
  79. //黑棋取消选中
  80. if (chressBord[xx][yy] == blackactive) {
  81. chressBord[xx][yy] = blackchress;
  82. activeChress = false;
  83. //白棋取消选中
  84. } else if (chressBord[xx][yy] == whiteactive) {
  85. chressBord[xx][yy] = whitechress;
  86. activeChress = false;
  87. } else if (chressBord[xx][yy] == blackchress) {
  88. chressBord[xx][yy] = blackactive;
  89. chressBord[ax][ay] = ac == whitechress ? whitechress : blackchress;
  90. activeChress = true;
  91. ac = blackchress;
  92. ax = xx;
  93. ay = yy;
  94. } else if (chressBord[xx][yy] === whitechress) {
  95. chressBord[xx][yy] = whiteactive;
  96. chressBord[ax][ay] = ac === whitechress ? whitechress : blackchress;
  97. activeChress = true;
  98. ac = whitechress;
  99. ax = xx;
  100. ay = yy;
  101. } else {
  102. console.info(round.isWhite);
  103. if (round.isWhite ^ (ac === whitechress)) {
  104. //走棋 一次只能走一步
  105. if (Math.abs(ax - xx) + Math.abs(ay - yy) === 1) {
  106. chressBord[ax][ay] = nochress;
  107. chressBord[xx][yy] = ac === whitechress ? whitechress : blackchress;
  108. activeChress = false;
  109. round.isWhite = !round.isWhite;
  110. horizontalEat(xx, yy);
  111. } else {
  112. alert("一次只能走一步");
  113. }
  114. } else {
  115. alert("这回合不是你的回合");
  116. }
  117. }
  118. } else {
  119. //如果没有棋子选中,那么说明现在在选棋子
  120. //如果选中黑棋
  121. if (chressBord[xx][yy] == blackchress) {
  122. chressBord[xx][yy] = blackactive;
  123. activeChress = true;
  124. ac = blackchress;
  125. ax = xx;
  126. ay = yy;
  127. }
  128. //如果选中白棋
  129. if (chressBord[xx][yy] === whitechress) {
  130. chressBord[xx][yy] = whiteactive;
  131. activeChress = true;
  132. ac = whitechress;
  133. ax = xx;
  134. ay = yy;
  135. }
  136. }
  137. clearCanvas();
  138. drawChessBoard();
  139. drawChress();
  140. };
  141. //绘画棋盘
  142. var drawChessBoard = function () {
  143. for (var i = 0; i < 5; i++) {
  144. context.moveTo(offset + i * lineSpace, offset);
  145. context.lineTo(offset + i * lineSpace, 450);
  146. context.stroke();
  147. context.moveTo(offset, offset + i * lineSpace);
  148. context.lineTo(450, offset + i * lineSpace);
  149. context.stroke();
  150. }
  151. }
  152. function drawChress() {
  153. for (var x = 0; x < 5; x++) {
  154. for (var y = 0; y < 5; y++) {
  155. if (chressBord[x][y] == whitechress) {
  156. oneStep(x, y, true, false);
  157. }
  158. if (chressBord[x][y] == blackchress) {
  159. oneStep(x, y, false, false);
  160. }
  161. if (chressBord[x][y] == whiteactive) {
  162. oneStep(x, y, true, true);
  163. }
  164. if (chressBord[x][y] == blackactive) {
  165. oneStep(x, y, false, true);
  166. }
  167. }
  168. }
  169. }
  170. //画棋子
  171. var oneStep = function (i, j, isWhite, isActive) {
  172. context.beginPath();
  173. context.arc(offset + i * lineSpace, offset + j * lineSpace, chressSize, 0, 2 * Math.PI);// 画圆
  174. context.closePath();
  175. //渐变
  176. var gradient = context.createRadialGradient(offset + i * lineSpace
  177. + 2, offset + j * lineSpace - 2, chressSize, offset + i * lineSpace + 2, offset + j * lineSpace -
  178. 2, 0);
  179. if (isWhite) {
  180. gradient.addColorStop(0, '#0a0a0a');
  181. gradient.addColorStop(1, '#636766');
  182. } else {
  183. gradient.addColorStop(0, '#d1d1d1');
  184. gradient.addColorStop(1, '#f9f9f9');
  185. }
  186. context.fillStyle = gradient;
  187. context.fill();
  188. context.restore();
  189. if (isActive) {
  190. context.rect(offset + i * lineSpace - chressSize,
  191. offset + j * lineSpace - chressSize,
  192. offset + i * lineSpace + chressSize - (offset + i * lineSpace - chressSize),
  193. offset + j * lineSpace + chressSize - (offset + j * lineSpace - chressSize));
  194. //context.setLineDash([5, 15, 5]);
  195. //context.lineCap = "round";
  196. context.stroke();
  197. context.restore();
  198. }
  199. };
  200. function clearCanvas() {
  201. //擦除该圆
  202. context.clearRect(0, 0, 500, 500);
  203. context.stroke();
  204. context.restore();
  205. }
  206. //检测横向吃子
  207. function horizontalEat(x, y) {
  208. if (chressBord[x][y] == whitechress) {
  209. let t1 = [0, 0, 10, 10, 20];
  210. let t2 = [0, 10, 10, 20, 0];
  211. let t3 = [10, 10, 20, 0, 0];
  212. let t4 = [20, 10, 10, 0, 0];
  213. let t5 = [0, 20, 10, 10, 0];
  214. let t6 = [0, 0, 20, 10, 10];
  215. if (chressBord[x] == t1) {
  216. console.log("ok");
  217. }
  218. //吃子状态
  219. //00 00 10 10 20
  220. //00 10 10 20 00
  221. //10 10 20 00 00
  222. //20 10 10 00 00
  223. //00 20 10 10 00
  224. //00 00 20 10 10
  225. } else if (chressBord[x][y] == blackchress) {
  226. }
  227. }
  228. //检测纵向吃子
  229. function verticalEat(x, y) {
  230. }
  231. </script>
  232. </html>