diff --git a/src/utils/index.js b/src/utils/index.js index a224a2e1dcdb448bc013aea8e672a1f6c5f4682d..a1b0998d37edfd8b62370f5a613c6b7e4fb24b69 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -4,11 +4,14 @@ export const is_empty = 'isEmpty'; */ export const is_white = 'isWhite'; export const is_black = 'isBlack'; +/** + * [y, x] === [row, col] === [行,列] + */ export const directions = [ - [1, 0], // 水平方向 - [0, 1], // 垂直方向 - [1, 1], // 右下方向 - [1, -1] // 左下方向 + [1, 0], // 垂直方向 + [0, 1], // 水平方向 + [1, 1], // \ 方向 + [1, -1] // / 方向 ] /** @@ -30,21 +33,21 @@ export function checkWin({ row, col, board, player, win_size }) { for (let i = 0; i < directions.length; i++) { res = [[row, col]]; - const [dx, dy] = directions[i]; - let x = row + dx; - let y = col + dy; + const [dy, dx] = directions[i]; + let x = col + dx; + let y = row + dy; // 向正反两个方向扩展,检查是否有连续的五个相同棋子 - while (x >= 0 && x < ROW && y >= 0 && y < COL && board[x][y] === TYPE) { - res.push([x, y]) + while (x >= 0 && x < COL && y >= 0 && y < ROW && board[x][y] === TYPE) { + res.push([y, x]) x += dx; y += dy; } - x = row - dx; - y = col - dy; - while (x >= 0 && x < ROW && y >= 0 && y < COL && board[x][y] === TYPE) { - res.push([x, y]) + x = col - dx; + y = row - dy; + while (x >= 0 && x < COL && y >= 0 && y < ROW && board[x][y] === TYPE) { + res.push([y, x]) x -= dx; y -= dy; }