Fri Jun 30 09:35:00 UTC 2023 inscode

上级 b1166327
......@@ -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)
......
......@@ -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)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册