提交 907bd65a 编写于 作者: U u014301111

Sun Mar 3 12:04:00 CST 2024 inscode

上级 d34eb8df
...@@ -45,6 +45,11 @@ function inRange(n, start = 0, end) { ...@@ -45,6 +45,11 @@ function inRange(n, start = 0, end) {
} }
return n >= start && n < 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) { ...@@ -54,10 +59,11 @@ function inRange(n, start = 0, end) {
* @returns {boolean} * @returns {boolean}
*/ */
function aroundIsEmpty({ ROW, COL, OBJ }, y, x) { function aroundIsEmpty({ ROW, COL, OBJ }, y, x) {
const _inChessboard = inChessboard(ROW, COL)
for (let i = 0; i < directions.length; i++) { for (let i = 0; i < directions.length; i++) {
const [_y, _x] = directions[i] const [_y, _x] = directions[i]
for (let j = -1; j < 3; j += 2) { 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 return false
} }
} }
...@@ -78,6 +84,7 @@ function aroundIsEmpty({ ROW, COL, OBJ }, y, x) { ...@@ -78,6 +84,7 @@ function aroundIsEmpty({ ROW, COL, OBJ }, y, x) {
export function checkWin({ row, col, board, player, win_size }) { export function checkWin({ row, col, board, player, win_size }) {
const ROW = board.length; const ROW = board.length;
const COL = board[0].length const COL = board[0].length
const _inChessboard = inChessboard(ROW, COL)
let res = [] let res = []
for (let i = 0; i < directions.length; i++) { for (let i = 0; i < directions.length; i++) {
...@@ -89,7 +96,7 @@ export function checkWin({ row, col, board, player, win_size }) { ...@@ -89,7 +96,7 @@ export function checkWin({ row, col, board, player, win_size }) {
let y = row + dy * j; 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]) res.push([y, x])
x += dx * j; x += dx * j;
y += dy * j; y += dy * j;
...@@ -150,6 +157,7 @@ export function robotPlay(board, win_size) { ...@@ -150,6 +157,7 @@ export function robotPlay(board, win_size) {
return [] return []
}) })
}).flat(1) }).flat(1)
console.log(scores)
// 按分数分组 // 按分数分组
const scores_group_obj = scores.reduce((r, item) => { const scores_group_obj = scores.reduce((r, item) => {
const key = Object.values(item)[0] const key = Object.values(item)[0]
...@@ -212,7 +220,8 @@ function getDirectionsInfo({ ROW, COL, OBJ }, row, col, [y, x], player) { ...@@ -212,7 +220,8 @@ function getDirectionsInfo({ ROW, COL, OBJ }, row, col, [y, x], player) {
const villain = player === is_black ? is_white : is_black; 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}`] const item = OBJ[`${_row}_${_col}`]
if (item === villain) { if (item === villain) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册