Fri Jun 30 09:35:00 UTC 2023 inscode

上级 b1166327
...@@ -3,7 +3,7 @@ import { onMounted, ref } from "vue" ...@@ -3,7 +3,7 @@ import { onMounted, ref } from "vue"
import { is_empty, checkWin, is_black, is_white, robotPlay } from "./utils" import { is_empty, checkWin, is_black, is_white, robotPlay } from "./utils"
// import { robotPlay } from './utils/api' // import { robotPlay } from './utils/api'
// 配置 // 配置
const size = ref(20) const size = ref(3)
const win_size = ref(5) const win_size = ref(5)
const lattices = ref([]) const lattices = ref([])
const is_load = ref(false) const is_load = ref(false)
......
...@@ -69,7 +69,7 @@ export function checkWin({ row, col, board, player, win_size, direction }) { ...@@ -69,7 +69,7 @@ export function checkWin({ row, col, board, player, win_size, direction }) {
* @returns {number[]} * @returns {number[]}
*/ */
export function robotPlay(board, robot, win_size) { export function robotPlay(board, robot, win_size) {
const maxScorePos = {}; let maxScorePos = [];
let maxScore = -1; let maxScore = -1;
// 空位 // 空位
const empty_points = board.map((item, row) => { const empty_points = board.map((item, row) => {
...@@ -94,55 +94,62 @@ export function robotPlay(board, robot, win_size) { ...@@ -94,55 +94,62 @@ export function robotPlay(board, robot, win_size) {
return [row, col] return [row, col]
} else { } else {
// 这里要应该是要返回这个位置的分数 // 这里要应该是要返回这个位置的分数
const horizontal = checkWin({ row, col, board, player: is_black, win_size, direction: getDirection("horizontal") }); score = estimateScore({ row, col, board })
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);
} }
} }
// 选取分数最高的空位 // 选取分数最高的空位
if (score > maxScore) { if (score > maxScore) {
maxScorePos.x = row; maxScorePos = [row, col];
maxScorePos.y = col;
maxScore = score; maxScore = score;
} }
}; };
return [maxScorePos.x, maxScorePos.y]; return maxScorePos;
} }
// 获取某个方向的偏移量 const direction_8 = [
function getDirection(direction) { [-1, -1],
switch (direction) { [0, -1],
case "horizontal": [1, -1],
return [0, 1]; [-1, 0],
case "vertical": [1, 0],
return [1, 0]; [-1, 1],
case "diagonalDown": [0, 1],
return [1, 1]; [1, 1]
case "diagonalUp": ]
return [1, -1];
default: // 获取一个一个方向上的棋子数
return [0, 0]; 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
* @param {object} Estimate.board * @param {object} Estimate.board
* @param {number} Estimate.row * @param {number} Estimate.row
* @param {number} Estimate.col * @param {number} Estimate.col
* @param {number} Estimate.win
* @param {number[]} Estimate.direction
* @return {number} * @return {number}
*/ */
function estimateScore({board, row, col, win, direction}){ function estimateScore({ board, row, col }) {
const player = is_white return direction_8.map((_direction) => {
let res = 0 const square = getDirectionScore(board, row, col, _direction)
if (square === 0) return square
return res return 10 ** (square - 1)
}).reduce((r, item) => {
return r + item
}, 0)
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册