123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
- <title>五道</title>
- <style>
- body {
- background-color: transparent;
- }
- canvas {
- display: block;
- margin: 50px auto;
- box-shadow: -2px -2px 2px #EFEFEF, 5px 5px 5px #B9B9B9;
- cursor: pointer;
- }
- </style>
- </head>
- <body>
- <canvas id="chess" width="500px" height="500px">
- </canvas>
- <div id="round" style="margin: 50px auto;"></div>
- </body>
- <script>
- var chess = document.getElementById("chess");
- var context = chess.getContext('2d');
- var rect = chess.getBoundingClientRect();
- context.strokeStyle = '#bfbfbf'; //边框颜色
- var chressBord = []; //棋盘
- var whitechress = 10;
- var blackchress = 20;
- var whiteactive = 11;
- var blackactive = 21;
- var nochress = 0;
- var offset = 50;
- var lineSpace = 100;
- var chressSize = 30;
- var round = {};
- Object.defineProperties(round, {
- isWhite: {
- configurable: true,
- get: function () {
- return isWhite;
- },
- set: function (newValue) {
- isWhite = newValue;
- document.getElementById("round").innerText = newValue ? '白棋回合' : '黑棋回合';
- }
- }
- });
- round.isWhite = true;
- var activeChress = false;
- var ax;
- var ay;
- var ac;
- for (var x = 0; x < 5; x++) {
- chressBord[x] = [];
- for (var y = 0; y < 5; y++) {
- if (y == 0) {
- chressBord[x][y] = whitechress;
- } else if (y == 4) {
- chressBord[x][y] = blackchress;
- } else {
- chressBord[x][y] = nochress;
- }
- }
- }
- window.onload = function () {
- drawChessBoard(); // 画棋盘
- drawChress();//画棋子
- context.save();
- };
- chess.onclick = function (e) {
- let x = Math.floor(e.clientX - rect.left);
- let y = Math.floor(e.clientY - rect.top);
- let xx = Math.floor(Math.abs((x)) / lineSpace);
- let yy = Math.floor(Math.abs((y)) / lineSpace);
- if (activeChress) {
- //黑棋取消选中
- if (chressBord[xx][yy] == blackactive) {
- chressBord[xx][yy] = blackchress;
- activeChress = false;
- //白棋取消选中
- } else if (chressBord[xx][yy] == whiteactive) {
- chressBord[xx][yy] = whitechress;
- activeChress = false;
- } else if (chressBord[xx][yy] == blackchress) {
- chressBord[xx][yy] = blackactive;
- chressBord[ax][ay] = ac == whitechress ? whitechress : blackchress;
- activeChress = true;
- ac = blackchress;
- ax = xx;
- ay = yy;
- } else if (chressBord[xx][yy] === whitechress) {
- chressBord[xx][yy] = whiteactive;
- chressBord[ax][ay] = ac === whitechress ? whitechress : blackchress;
- activeChress = true;
- ac = whitechress;
- ax = xx;
- ay = yy;
- } else {
- console.info(round.isWhite);
- if (round.isWhite ^ (ac === whitechress)) {
- //走棋 一次只能走一步
- if (Math.abs(ax - xx) + Math.abs(ay - yy) === 1) {
- chressBord[ax][ay] = nochress;
- chressBord[xx][yy] = ac === whitechress ? whitechress : blackchress;
- activeChress = false;
- round.isWhite = !round.isWhite;
- horizontalEat(xx, yy);
- } else {
- alert("一次只能走一步");
- }
- } else {
- alert("这回合不是你的回合");
- }
- }
- } else {
- //如果没有棋子选中,那么说明现在在选棋子
- //如果选中黑棋
- if (chressBord[xx][yy] == blackchress) {
- chressBord[xx][yy] = blackactive;
- activeChress = true;
- ac = blackchress;
- ax = xx;
- ay = yy;
- }
- //如果选中白棋
- if (chressBord[xx][yy] === whitechress) {
- chressBord[xx][yy] = whiteactive;
- activeChress = true;
- ac = whitechress;
- ax = xx;
- ay = yy;
- }
- }
- clearCanvas();
- drawChessBoard();
- drawChress();
- };
- //绘画棋盘
- var drawChessBoard = function () {
- for (var i = 0; i < 5; i++) {
- context.moveTo(offset + i * lineSpace, offset);
- context.lineTo(offset + i * lineSpace, 450);
- context.stroke();
- context.moveTo(offset, offset + i * lineSpace);
- context.lineTo(450, offset + i * lineSpace);
- context.stroke();
- }
- }
- function drawChress() {
- for (var x = 0; x < 5; x++) {
- for (var y = 0; y < 5; y++) {
- if (chressBord[x][y] == whitechress) {
- oneStep(x, y, true, false);
- }
- if (chressBord[x][y] == blackchress) {
- oneStep(x, y, false, false);
- }
- if (chressBord[x][y] == whiteactive) {
- oneStep(x, y, true, true);
- }
- if (chressBord[x][y] == blackactive) {
- oneStep(x, y, false, true);
- }
- }
- }
- }
- //画棋子
- var oneStep = function (i, j, isWhite, isActive) {
- context.beginPath();
- context.arc(offset + i * lineSpace, offset + j * lineSpace, chressSize, 0, 2 * Math.PI);// 画圆
- context.closePath();
- //渐变
- var gradient = context.createRadialGradient(offset + i * lineSpace
- + 2, offset + j * lineSpace - 2, chressSize, offset + i * lineSpace + 2, offset + j * lineSpace -
- 2, 0);
- if (isWhite) {
- gradient.addColorStop(0, '#0a0a0a');
- gradient.addColorStop(1, '#636766');
- } else {
- gradient.addColorStop(0, '#d1d1d1');
- gradient.addColorStop(1, '#f9f9f9');
- }
- context.fillStyle = gradient;
- context.fill();
- context.restore();
- if (isActive) {
- context.rect(offset + i * lineSpace - chressSize,
- offset + j * lineSpace - chressSize,
- offset + i * lineSpace + chressSize - (offset + i * lineSpace - chressSize),
- offset + j * lineSpace + chressSize - (offset + j * lineSpace - chressSize));
- //context.setLineDash([5, 15, 5]);
- //context.lineCap = "round";
- context.stroke();
- context.restore();
- }
- };
- function clearCanvas() {
- //擦除该圆
- context.clearRect(0, 0, 500, 500);
- context.stroke();
- context.restore();
- }
- //检测横向吃子
- function horizontalEat(x, y) {
- if (chressBord[x][y] == whitechress) {
- let t1 = [0, 0, 10, 10, 20];
- let t2 = [0, 10, 10, 20, 0];
- let t3 = [10, 10, 20, 0, 0];
- let t4 = [20, 10, 10, 0, 0];
- let t5 = [0, 20, 10, 10, 0];
- let t6 = [0, 0, 20, 10, 10];
- if (chressBord[x] == t1) {
- console.log("ok");
- }
- //吃子状态
- //00 00 10 10 20
- //00 10 10 20 00
- //10 10 20 00 00
- //20 10 10 00 00
- //00 20 10 10 00
- //00 00 20 10 10
- } else if (chressBord[x][y] == blackchress) {
- }
- }
- //检测纵向吃子
- function verticalEat(x, y) {
- }
- </script>
- </html>
|