export const is_empty = 'isEmpty'; /** * 机器人 */ export const is_white = 'isWhite'; export const is_black = 'isBlack'; export const directions = [ [1, 0], // 水平方向 [0, 1], // 垂直方向 [1, 1], // 右下方向 [1, -1] // 左下方向 ] /** * 检查四个方向连续的棋子数 * @param {object} param * @param {number} param.row 行 * @param {number} param.col 列 * @param {(is_empty|is_white|is_black)[]} param.board 棋盘 * @param {is_white|is_black} [param.player] 当前棋子类型 * @param {number} param.win_size 需要几个棋子才赢 * @param {[][]} [param.direction] 方向 * @returns */ export function checkWin({ row, col, board, player, win_size, direction }) { console.log(win_size) const _row = board.length; const _col = board[0].length const pieceType = player ?? board[row][col] let res = [] const _directions = direction ? [direction] : directions; for (let i = 0; i < _directions.length; i++) { res = [[row, col]]; const [dx, dy] = _directions[i]; let x = row + dx; let y = col + dy; // 向正反两个方向扩展,检查是否有连续的五个相同棋子 while (x >= 0 && x < _row && y >= 0 && y < _col && board[x][y] === pieceType) { res.push([x, y]) x += dx; y += dy; } x = row - dx; y = col - dy; while (x >= 0 && x < _row && y >= 0 && y < _col && board[x][y] === pieceType) { res.push([x, y]) x -= dx; y -= dy; } // 出现五连珠,返回胜利 if (res.length >= win_size) { return res; } } // 当前空位,可以结成多少颗连珠 return res; } /** * 机器人下棋位置 * @param {(is_empty|is_white|is_black)[][]} board 棋盘 * @param {is_white} robot 机器人 * @param {number} win_size 赢棋的棋子颗数 * @returns {number[]} */ export function robotPlay(board, robot, win_size) { let maxScorePos = []; let maxScore = -1; // 空位 const empty_points = board.map((item, row) => { return item.flatMap((_item, col) => _item === is_empty ? [[row, col]] : []) }).flat(1) // 对每个空位进行评分 for (let e_i = 0; e_i < empty_points.length; e_i++) { const point = empty_points[e_i] let score = 0; const [row, col] = point; // 判断下子后是否获胜 const win = checkWin({ row, col, board, player: robot, win_size }); if (win.length >= win_size) { return [row, col] } else { // 判断对手是否能在下一步获胜 const oppWin = checkWin({ row, col, board, player: is_black, win_size }); if (oppWin.length >= win_size) { return [row, col] } else { // 这里要应该是要返回这个位置的分数 score = estimateScore({ row, col, board }) } } // 选取分数最高的空位 if (score > maxScore) { maxScorePos = [row, col]; maxScore = score; } }; return maxScorePos; } const direction_8 = [ [-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1] ] // 获取一个一个方向上的棋子数 function getDirectionScore(board, row, col, direction) { let res = 0 const [x, y] = direction let _row = row let _col = col while (true) { if (board[_row][_col] !== is_black) { return res } res += 1 _row += x _col += y } } /** * 评估每个空位置的价值,从八个方向去计算, * 计分规则 0,1,10,100,1000,10000,100000 根据棋子数量计分,每多一颗棋子,分数乘以10 * @param {object} Estimate * @param {object} Estimate.board * @param {number} Estimate.row * @param {number} Estimate.col * @return {number} */ function estimateScore({ board, row, col }) { return direction_8.map((_direction) => { const square = getDirectionScore(board, row, col, _direction) if (square === 0) return square return 10 ** (square - 1) }).reduce((r, item) => { return r + item }, 0) }