前端canvas写五子棋超过五个的判断的代码写一份
时间: 2023-04-03 15:04:55 浏览: 79
以下是前端canvas写五子棋超过五个的判断的代码:
```javascript
function checkWin(x, y, chessBoard) {
var count = 1;
var color = chessBoard[x][y];
// 横向判断
for (var i = x - 1; i >= 0; i--) {
if (chessBoard[i][y] === color) {
count++;
} else {
break;
}
}
for (var i = x + 1; i < 15; i++) {
if (chessBoard[i][y] === color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 纵向判断
count = 1;
for (var i = y - 1; i >= 0; i--) {
if (chessBoard[x][i] === color) {
count++;
} else {
break;
}
}
for (var i = y + 1; i < 15; i++) {
if (chessBoard[x][i] === color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 左上到右下判断
count = 1;
for (var i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
if (chessBoard[i][j] === color) {
count++;
} else {
break;
}
}
for (var i = x + 1, j = y + 1; i < 15 && j < 15; i++, j++) {
if (chessBoard[i][j] === color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 右上到左下判断
count = 1;
for (var i = x - 1, j = y + 1; i >= 0 && j < 15; i--, j++) {
if (chessBoard[i][j] === color) {
count++;
} else {
break;
}
}
for (var i = x + 1, j = y - 1; i < 15 && j >= 0; i++, j--) {
if (chessBoard[i][j] === color) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
```
阅读全文