From 73674f09a19fd78dc9e15c2d461f41a7745a8026 Mon Sep 17 00:00:00 2001 From: 63db3122f0950a2aef64df95 <63db3122f0950a2aef64df95@devide> Date: Fri, 30 Jun 2023 09:35:00 +0000 Subject: [PATCH] Fri Jun 30 09:35:00 UTC 2023 inscode --- src/App.vue | 2 +- src/utils/index.js | 69 +++++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/App.vue b/src/App.vue index 106a6e2..a8c3d8b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -3,7 +3,7 @@ import { onMounted, ref } from "vue" import { is_empty, checkWin, is_black, is_white, robotPlay } from "./utils" // import { robotPlay } from './utils/api' // 配置 -const size = ref(20) +const size = ref(3) const win_size = ref(5) const lattices = ref([]) const is_load = ref(false) diff --git a/src/utils/index.js b/src/utils/index.js index 3461206..5fc802a 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -69,7 +69,7 @@ export function checkWin({ row, col, board, player, win_size, direction }) { * @returns {number[]} */ export function robotPlay(board, robot, win_size) { - const maxScorePos = {}; + let maxScorePos = []; let maxScore = -1; // 空位 const empty_points = board.map((item, row) => { @@ -94,55 +94,62 @@ export function robotPlay(board, robot, win_size) { return [row, col] } else { // 这里要应该是要返回这个位置的分数 - const horizontal = checkWin({ row, col, board, player: is_black, win_size, direction: getDirection("horizontal") }); - const vertical = checkWin({ row, col, board, player: is_black, win_size, direction: getDirection("vertical") }); - const diagonalDown = checkWin({ row, col, board, player: is_black, win_size, direction: getDirection("diagonalDown") }); - const diagonalUp = checkWin({ row, col, board, player: is_black, win_size, direction: getDirection("diagonalUp") }); - - score = Math.max(horizontal.length, vertical.length, diagonalDown.length, diagonalUp.length); + score = estimateScore({ row, col, board }) } } // 选取分数最高的空位 if (score > maxScore) { - maxScorePos.x = row; - maxScorePos.y = col; + maxScorePos = [row, col]; maxScore = score; } }; - return [maxScorePos.x, maxScorePos.y]; + return maxScorePos; } -// 获取某个方向的偏移量 -function getDirection(direction) { - switch (direction) { - case "horizontal": - return [0, 1]; - case "vertical": - return [1, 0]; - case "diagonalDown": - return [1, 1]; - case "diagonalUp": - return [1, -1]; - default: - return [0, 0]; +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 - * @param {number} Estimate.win - * @param {number[]} Estimate.direction * @return {number} */ -function estimateScore({board, row, col, win, direction}){ - const player = is_white - let res = 0 - - return res +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) } -- GitLab