From eea4f3d75ed18f7f884eae14ff8bc53dd465cc93 Mon Sep 17 00:00:00 2001 From: u014301111 Date: Sat, 29 Jul 2023 04:55:00 +0800 Subject: [PATCH] Sat Jul 29 04:55:00 CST 2023 inscode --- src/utils/index.js | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 4b78a48..a8de1b0 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -57,7 +57,6 @@ function aroundIsEmpty({ ROW, COL, OBJ }, y, x) { } } } - return res_arr === 0 } @@ -78,36 +77,25 @@ export function checkWin({ row, col, board, player, win_size }) { for (let i = 0; i < directions.length; i++) { res = [[row, col]]; - const [dy, dx] = directions[i]; - let x = col + dx; - let y = row + dy; - - // 向正反两个方向扩展,检查是否有连续的五个相同棋子 - while (inRange(x, COL) && inRange(y, ROW) && board[y][x] === player) { - res.push([y, x]) - x += dx; - y += dy; - } + for (let j = -1; j < 3; j += 2) { - // 出现五连珠,返回胜利 - if (res.length >= win_size) { - return res; - } + const [dy, dx] = directions[i]; + let x = col + dx * j; + let y = row + dy * j; - x = col - dx; - y = row - dy; - while (inRange(x, COL) && inRange(y, ROW) && board[y][x] === player) { - res.push([y, x]) - x -= dx; - y -= dy; - } + // 向正反两个方向扩展,检查是否有连续的五个相同棋子 + while (inRange(x, COL) && inRange(y, ROW) && board[y][x] === player) { + res.push([y, x]) + x += dx * j; + y += dy * j; + } - // 出现五连珠,返回胜利 - if (res.length >= win_size) { - return res; + // 出现五连珠,返回胜利 + if (res.length >= win_size) { + return res; + } } } - // 当前空位,可以结成多少颗连珠 return false; } @@ -176,6 +164,7 @@ export function robotPlay(board, win_size) { if (max_scores_arr.length === 1) { res_key = Object.keys(max_scores_arr[0])[0] } else if (max_scores_arr.length > 1) { + // 多个相同的分数的,随机选择一个 const random_index = Math.floor(Math.random() * max_scores_arr.length) res_key = Object.keys(max_scores_arr[random_index])[0] -- GitLab