diff --git a/src/utils/index.js b/src/utils/index.js index 40f48f5eb7724f0889a3a57ec2907bda8b8a65f6..e9e55e71cd4d36b5b8d7e0877e047bd85ae079ef 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -45,6 +45,11 @@ function inRange(n, start = 0, end) { } return n >= start && n < end } +function inChessboard(ROW, COL){ + return function(row, col){ + return inRange(row, ROW) && inRange(col, COL); + } +} /** * 判断周围八个方向为空 @@ -54,10 +59,11 @@ function inRange(n, start = 0, end) { * @returns {boolean} */ function aroundIsEmpty({ ROW, COL, OBJ }, y, x) { + const _inChessboard = inChessboard(ROW, COL) for (let i = 0; i < directions.length; i++) { const [_y, _x] = directions[i] for (let j = -1; j < 3; j += 2) { - if (inRange(y + _y * j, ROW) && inRange(x + _x * j, COL) && OBJ[`${_y * j + y}_${_x * j + x}`] !== is_empty) { + if (_inChessboard(y + _y * j, x + _x * j) && OBJ[`${_y * j + y}_${_x * j + x}`] !== is_empty) { return false } } @@ -78,6 +84,7 @@ function aroundIsEmpty({ ROW, COL, OBJ }, y, x) { export function checkWin({ row, col, board, player, win_size }) { const ROW = board.length; const COL = board[0].length + const _inChessboard = inChessboard(ROW, COL) let res = [] for (let i = 0; i < directions.length; i++) { @@ -89,7 +96,7 @@ export function checkWin({ row, col, board, player, win_size }) { let y = row + dy * j; // 向正反两个方向扩展,检查是否有连续的五个相同棋子 - while (inRange(x, COL) && inRange(y, ROW) && board[y][x] === player) { + while (_inChessboard(y, x) && board[y][x] === player) { res.push([y, x]) x += dx * j; y += dy * j; @@ -150,6 +157,7 @@ export function robotPlay(board, win_size) { return [] }) }).flat(1) + console.log(scores) // 按分数分组 const scores_group_obj = scores.reduce((r, item) => { const key = Object.values(item)[0] @@ -212,7 +220,8 @@ function getDirectionsInfo({ ROW, COL, OBJ }, row, col, [y, x], player) { const villain = player === is_black ? is_white : is_black; - while (inRange(_row, ROW) && inRange(_col, COL)) { + const _inChessboard = inChessboard(ROW, COL) + while (_inChessboard(_row, _col)) { const item = OBJ[`${_row}_${_col}`] if (item === villain) {