five-line-canvas.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <!DOCTYPE html>
  2. <html>
  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 style="margin: 50px auto;"><span>准备</span> <span>开始</span> <span>悔棋</span></div>
  23. <div id="round" style="margin: 50px auto;"></div>
  24. </body>
  25. <script>
  26. let chess = document.getElementById("chess");
  27. let context = chess.getContext('2d');
  28. let rect = chess.getBoundingClientRect();
  29. context.strokeStyle = '#bfbfbf'; //边框颜色
  30. let chressBord = []; //棋盘
  31. const whitechress = 10;
  32. const blackchress = 20;
  33. const whiteactive = 11;
  34. const blackactive = 21;
  35. const nochress = 0;
  36. const offset = 50;
  37. const lineSpace = 100;
  38. const chressSize = 30;
  39. let round = {};
  40. Object.defineProperties(round, {
  41. isWhite: {
  42. configurable: true,
  43. get: function () {
  44. return isWhite;
  45. },
  46. set: function (newValue) {
  47. isWhite = newValue;
  48. document.getElementById("round").innerText = newValue ? '白棋回合' : '黑棋回合';
  49. }
  50. }
  51. });
  52. round.isWhite = true;
  53. let activeChress = false;
  54. let ax;
  55. let ay;
  56. let ac;
  57. for (let x = 0; x < 5; x++) {
  58. chressBord[x] = [];
  59. for (let y = 0; y < 5; y++) {
  60. if (y == 0) {
  61. chressBord[x][y] = whitechress;
  62. } else if (y == 4) {
  63. chressBord[x][y] = blackchress;
  64. } else {
  65. chressBord[x][y] = nochress;
  66. }
  67. }
  68. }
  69. window.onload = function () {
  70. drawChessBoard(); // 画棋盘
  71. drawChress();//画棋子
  72. context.save();
  73. };
  74. chess.onclick = function (e) {
  75. let x = Math.floor(e.clientX - rect.left);
  76. let y = Math.floor(e.clientY - rect.top);
  77. let xx = Math.floor(Math.abs((x)) / lineSpace);
  78. let yy = Math.floor(Math.abs((y)) / lineSpace);
  79. if (activeChress) {
  80. //黑棋取消选中
  81. if (chressBord[xx][yy] == blackactive) {
  82. chressBord[xx][yy] = blackchress;
  83. activeChress = false;
  84. //白棋取消选中
  85. } else if (chressBord[xx][yy] == whiteactive) {
  86. chressBord[xx][yy] = whitechress;
  87. activeChress = false;
  88. } else if (chressBord[xx][yy] == blackchress) {
  89. chressBord[xx][yy] = blackactive;
  90. chressBord[ax][ay] = ac == whitechress ? whitechress : blackchress;
  91. activeChress = true;
  92. ac = blackchress;
  93. ax = xx;
  94. ay = yy;
  95. } else if (chressBord[xx][yy] === whitechress) {
  96. chressBord[xx][yy] = whiteactive;
  97. chressBord[ax][ay] = ac === whitechress ? whitechress : blackchress;
  98. activeChress = true;
  99. ac = whitechress;
  100. ax = xx;
  101. ay = yy;
  102. } else {
  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. verticalEat(xx, yy);
  112. } else {
  113. alert("一次只能走一步");
  114. }
  115. } else {
  116. alert("这回合不是你的回合");
  117. }
  118. }
  119. } else {
  120. //如果没有棋子选中,那么说明现在在选棋子
  121. //如果选中黑棋
  122. if (chressBord[xx][yy] === blackchress) {
  123. chressBord[xx][yy] = blackactive;
  124. activeChress = true;
  125. ac = blackchress;
  126. ax = xx;
  127. ay = yy;
  128. }
  129. //如果选中白棋
  130. if (chressBord[xx][yy] === whitechress) {
  131. chressBord[xx][yy] = whiteactive;
  132. activeChress = true;
  133. ac = whitechress;
  134. ax = xx;
  135. ay = yy;
  136. }
  137. }
  138. clearCanvas();
  139. drawChessBoard();
  140. drawChress();
  141. }
  142. //绘画棋盘
  143. var drawChessBoard = function () {
  144. for (var i = 0; i < 5; i++) {
  145. context.moveTo(offset + i * lineSpace, offset);
  146. context.lineTo(offset + i * lineSpace, 450);
  147. context.stroke();
  148. context.moveTo(offset, offset + i * lineSpace);
  149. context.lineTo(450, offset + i * lineSpace);
  150. context.stroke();
  151. }
  152. }
  153. function drawChress() {
  154. for (let x = 0; x < 5; x++) {
  155. for (let y = 0; y < 5; y++) {
  156. if (chressBord[x][y] == whitechress) {
  157. oneStep(x, y, true, false);
  158. }
  159. if (chressBord[x][y] == blackchress) {
  160. oneStep(x, y, false, false);
  161. }
  162. if (chressBord[x][y] == whiteactive) {
  163. oneStep(x, y, true, true);
  164. }
  165. if (chressBord[x][y] == blackactive) {
  166. oneStep(x, y, false, true);
  167. }
  168. }
  169. }
  170. }
  171. //画棋子
  172. var oneStep = function (i, j, isWhite, isActive) {
  173. context.beginPath();
  174. context.arc(offset + i * lineSpace, offset + j * lineSpace, chressSize, 0, 2 * Math.PI);// 画圆
  175. context.closePath();
  176. //渐变
  177. let gradient = context.createRadialGradient(offset + i * lineSpace
  178. + 2, offset + j * lineSpace - 2, chressSize, offset + i * lineSpace + 2, offset + j * lineSpace -
  179. 2, 0);
  180. if (isWhite) {
  181. gradient.addColorStop(0, '#0a0a0a');
  182. gradient.addColorStop(1, '#636766');
  183. } else {
  184. gradient.addColorStop(0, '#d1d1d1');
  185. gradient.addColorStop(1, '#f9f9f9');
  186. }
  187. context.fillStyle = gradient;
  188. context.fill();
  189. context.restore();
  190. if (isActive) {
  191. context.rect(offset + i * lineSpace - chressSize,
  192. offset + j * lineSpace - chressSize,
  193. offset + i * lineSpace + chressSize - (offset + i * lineSpace - chressSize),
  194. offset + j * lineSpace + chressSize - (offset + j * lineSpace - chressSize));
  195. //context.setLineDash([5, 15, 5]);
  196. //context.lineCap = "round";
  197. context.stroke();
  198. context.restore();
  199. }
  200. };
  201. function clearCanvas() {
  202. //擦除该圆
  203. context.clearRect(0, 0, 500, 500);
  204. context.stroke();
  205. context.restore();
  206. }
  207. function rule() {
  208. whiteCount = 0;
  209. blackCount = 0;
  210. for (let i = 0; i <= 4; i++) {
  211. for (let j = 0; j <= 4; j++) {
  212. if (chressBord[i][j] == blackchress || chressBord[i][j] == blackactive) {
  213. blackCount++;
  214. }
  215. if (chressBord[i][j] == whitechress || chressBord[i][j] == whiteactive) {
  216. whiteCount++;
  217. }
  218. }
  219. }
  220. if (whiteCount < 2) {
  221. alert("黑棋赢");
  222. //location.reload();
  223. }
  224. if (blackCount < 2) {
  225. alert("白棋赢");
  226. //location.reload();
  227. }
  228. }
  229. //检测横向吃子,y固定
  230. function horizontalEat(x, y) {
  231. let blackCount = 0;
  232. let whiteCount = 0;
  233. let min = -1;
  234. let max = -1;
  235. let whiteX = -1;
  236. let blackX = -1;
  237. for (let i = 0; i <= 4; i++) {
  238. if (chressBord[i][y] == nochress) {
  239. continue;
  240. }
  241. if (chressBord[i][y] == blackchress) {
  242. blackCount++;
  243. if (min == -1) {
  244. min = i;
  245. }
  246. if (max < i) {
  247. max = i;
  248. }
  249. blackX = i;
  250. }
  251. if (chressBord[i][y] == whitechress) {
  252. whiteCount++;
  253. if (min == -1) {
  254. min = i;
  255. }
  256. if (max < i) {
  257. max = i;
  258. }
  259. whiteX = i;
  260. }
  261. }
  262. if (chressBord[min][y] == chressBord[max][y]) {
  263. return;
  264. }
  265. if (min + 2 != max) {
  266. return;
  267. }
  268. if ((blackCount + whiteCount != 3) || (blackCount * whiteCount != 2)) {
  269. return;
  270. }
  271. if (blackCount > whiteCount) {
  272. if (!round.isWhite) {
  273. chressBord[whiteX][y] = nochress
  274. }
  275. } else {
  276. if (round.isWhite) {
  277. chressBord[blackX][y] = nochress
  278. }
  279. }
  280. }
  281. //检测纵向吃子 x固定
  282. function verticalEat(x, y) {
  283. let blackCount = 0;
  284. let whiteCount = 0;
  285. let min = -1;
  286. let max = -1;
  287. let whiteY = -1;
  288. let blackY = -1;
  289. for (let i = 0; i <= 4; i++) {
  290. if (chressBord[x][i] == nochress) {
  291. continue;
  292. }
  293. if (chressBord[x][i] == blackchress) {
  294. blackCount++;
  295. if (min == -1) {
  296. min = i;
  297. }
  298. if (max < i) {
  299. max = i;
  300. }
  301. blackY = i;
  302. }
  303. if (chressBord[x][i] == whitechress) {
  304. whiteCount++;
  305. if (min == -1) {
  306. min = i;
  307. }
  308. if (max < i) {
  309. max = i;
  310. }
  311. whiteY = i;
  312. }
  313. }
  314. if (chressBord[x][min] == chressBord[x][max]) {
  315. return;
  316. }
  317. if (min + 2 != max) {
  318. return;
  319. }
  320. if ((blackCount + whiteCount != 3) || (blackCount * whiteCount != 2)) {
  321. return;
  322. }
  323. if (blackCount > whiteCount) {
  324. if (!round.isWhite) {
  325. chressBord[x][whiteY] = nochress
  326. }
  327. } else {
  328. if (round.isWhite) {
  329. chressBord[x][blackY] = nochress
  330. }
  331. }
  332. }
  333. </script>
  334. </html>