Thu Jul 6 06:39:00 UTC 2023 inscode

上级 b04d2fd3
......@@ -165,36 +165,46 @@ function getBoundary(size, num) {
}
/**
* 获取这一边方向的信息,连珠颗数,有没有阻拦,是否已经到边界了
* 获取这一边方向的信息,连珠颗数,延伸出去的空位数
* @param {(is_empty|is_white|is_black)[][]} board 棋盘
* @param {number} row 行
* @param {number} col 列
* @param {number} win_size 连珠数
* @return {num: number, empty_num: number} 说明:num: 棋子个数;empty_num: 空格数量
*/
function getJoinInfo(board, row, col, win_size) {
function getJoinInfo(board, row, col, [y, x]) {
const ROW = board.length
const COL = board[0].length
// 连续棋子数
let res = 0
let num = 0
// 一侧到边缘的距离
let side = 0
let empty_num = 0
// 不包括当前位置
let _row = row + y
let _col = col + x
while (inRange(_row, ROW) && inRange(_col, COL)) {
if (board[_row][_col] === is_white) {
res += 1
while (true) {
if (!inRange(_row, ROW) || !inRange(_col, COL)) {
break;
}
const item = board[_row][_col]
if (item === is_white) {
break;
}
// 连珠计数
if (item === is_black) {
num += 1
}
// 计算空位数
if (item === is_empty) {
empty_num += 1
}
side += 1
_row += y
_col += x
}
return {
num: 1,
empty_num: 1,
num,
empty_num,
}
}
......@@ -208,29 +218,9 @@ function getJoinInfo(board, row, col, win_size) {
* @returns
*/
function getDirectionScore(board, row, col, [y, x], win_size) {
let res = 0
let _res = 0
let _row = row + y
let _col = col + x
while (true) {
if (_row < 0 || _col < 0 || !board[_row] || board[_row][_col] !== is_black) {
break;
}
res += 1
_row += y
_col += x
}
const { num: r_num, empty_num: r_empty_num } = getJoinInfo(board, row, col, [y, x])
const { num: l_num, empty_num: l_empty_num } = getJoinInfo(board, row, col, [y * -1, x * -1])
_row = row - y
_col = col - x
while (true) {
if (_row < 0 || _col < 0 || !board[_row] || board[_row][_col] !== is_black) {
break;
}
_res += 1
_row -= y
_col -= x
}
return res + _res
return r_num + l_num
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册