123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <!DOCTYPE html>
- <html>
- <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 style="margin: 50px auto;"><span>准备</span> <span>开始</span> <span>悔棋</span></div>
- <div id="round" style="margin: 50px auto;"></div>
- </body>
- <script>
- let chess = document.getElementById("chess");
- let context = chess.getContext('2d');
- let rect = chess.getBoundingClientRect();
- context.strokeStyle = '#bfbfbf'; //边框颜色
- let chressBord = []; //棋盘
- const whitechress = 10;
- const blackchress = 20;
- const whiteactive = 11;
- const blackactive = 21;
- const nochress = 0;
- const offset = 50;
- const lineSpace = 100;
- const chressSize = 30;
- let round = {};
- Object.defineProperties(round, {
- isWhite: {
- configurable: true,
- get: function () {
- return isWhite;
- },
- set: function (newValue) {
- isWhite = newValue;
- document.getElementById("round").innerText = newValue ? '白棋回合' : '黑棋回合';
- }
- }
- });
- round.isWhite = true;
- let activeChress = false;
- let ax;
- let ay;
- let ac;
- for (let x = 0; x < 5; x++) {
- chressBord[x] = [];
- for (let 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 {
- 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);
- verticalEat(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 (let x = 0; x < 5; x++) {
- for (let 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();
- //渐变
- let 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 rule() {
- whiteCount = 0;
- blackCount = 0;
- for (let i = 0; i <= 4; i++) {
- for (let j = 0; j <= 4; j++) {
- if (chressBord[i][j] == blackchress || chressBord[i][j] == blackactive) {
- blackCount++;
- }
- if (chressBord[i][j] == whitechress || chressBord[i][j] == whiteactive) {
- whiteCount++;
- }
- }
- }
- if (whiteCount < 2) {
- alert("黑棋赢");
- //location.reload();
- }
- if (blackCount < 2) {
- alert("白棋赢");
- //location.reload();
- }
- }
- //检测横向吃子,y固定
- function horizontalEat(x, y) {
- let blackCount = 0;
- let whiteCount = 0;
- let min = -1;
- let max = -1;
- let whiteX = -1;
- let blackX = -1;
- for (let i = 0; i <= 4; i++) {
- if (chressBord[i][y] == nochress) {
- continue;
- }
- if (chressBord[i][y] == blackchress) {
- blackCount++;
- if (min == -1) {
- min = i;
- }
- if (max < i) {
- max = i;
- }
- blackX = i;
- }
- if (chressBord[i][y] == whitechress) {
- whiteCount++;
- if (min == -1) {
- min = i;
- }
- if (max < i) {
- max = i;
- }
- whiteX = i;
- }
- }
- if (chressBord[min][y] == chressBord[max][y]) {
- return;
- }
- if (min + 2 != max) {
- return;
- }
- if ((blackCount + whiteCount != 3) || (blackCount * whiteCount != 2)) {
- return;
- }
- if (blackCount > whiteCount) {
- if (!round.isWhite) {
- chressBord[whiteX][y] = nochress
- }
- } else {
- if (round.isWhite) {
- chressBord[blackX][y] = nochress
- }
- }
- }
- //检测纵向吃子 x固定
- function verticalEat(x, y) {
- let blackCount = 0;
- let whiteCount = 0;
- let min = -1;
- let max = -1;
- let whiteY = -1;
- let blackY = -1;
- for (let i = 0; i <= 4; i++) {
- if (chressBord[x][i] == nochress) {
- continue;
- }
- if (chressBord[x][i] == blackchress) {
- blackCount++;
- if (min == -1) {
- min = i;
- }
- if (max < i) {
- max = i;
- }
- blackY = i;
- }
- if (chressBord[x][i] == whitechress) {
- whiteCount++;
- if (min == -1) {
- min = i;
- }
- if (max < i) {
- max = i;
- }
- whiteY = i;
- }
- }
- if (chressBord[x][min] == chressBord[x][max]) {
- return;
- }
- if (min + 2 != max) {
- return;
- }
- if ((blackCount + whiteCount != 3) || (blackCount * whiteCount != 2)) {
- return;
- }
- if (blackCount > whiteCount) {
- if (!round.isWhite) {
- chressBord[x][whiteY] = nochress
- }
- } else {
- if (round.isWhite) {
- chressBord[x][blackY] = nochress
- }
- }
- }
- </script>
- </html>
|