Mon Jun 26 03:25:00 UTC 2023 inscode

上级 d1b5eb86
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
import { onMounted, ref } from "vue" import { onMounted, ref } from "vue"
import { is_empty, checkWin, robotPlay, is_black, is_white } from "./utils" import { is_empty, checkWin, robotPlay, is_black, is_white } from "./utils"
// 配置 // 配置
const size = ref(20) const size = ref(3)
const win_size = ref(5) const win_size = ref(3)
const lattices = ref([]) const lattices = ref([])
let nextPlay = is_black // is_white let nextPlay = is_black // is_white
// 5 连珠的下标 // 5 连珠的下标
...@@ -25,8 +25,8 @@ function playChess(item, row, col) { ...@@ -25,8 +25,8 @@ function playChess(item, row, col) {
isWin = test isWin = test
return return
} else { } else {
robotPlay(lattices.value, is_black, win_size.value)
} }
nextPlay = nextPlay == is_white ? is_black : is_white
} }
......
export const is_empty = null; export const is_empty = null;
export const is_white = 'isWhite'; export const is_white = 'isWhite';
export const is_black = 'isBlack'; export const is_black = 'isBlack';
export const directions = [
[1, 0], // 水平方向
[0, 1], // 垂直方向
[1, 1], // 右下方向
[1, -1] // 左下方向
]
/** /**
* 检查四个方向连续的棋子数 * 检查四个方向连续的棋子数
...@@ -10,23 +16,20 @@ export const is_black = 'isBlack'; ...@@ -10,23 +16,20 @@ export const is_black = 'isBlack';
* @param {(null|is_white|is_black)[]} param.board 棋盘 * @param {(null|is_white|is_black)[]} param.board 棋盘
* @param {is_white|is_black} [param.player] 当前棋子类型 * @param {is_white|is_black} [param.player] 当前棋子类型
* @param {number} param.win_size 需要几个棋子才赢 * @param {number} param.win_size 需要几个棋子才赢
* @param {[]} [param.direction] 方向
* @returns * @returns
*/ */
export function checkWin({ row, col, board, player, win_size }) { export function checkWin({ row, col, board, player, win_size, direction }) {
const _row = board.length; const _row = board.length;
const _col = board[0].length const _col = board[0].length
const pieceType = player ?? board[row][col] const pieceType = player ?? board[row][col]
const directions = [
[1, 0], // 水平方向
[0, 1], // 垂直方向
[1, 1], // 右下方向
[1, -1] // 左下方向
];
for (let i = 0; i < directions.length; i++) { const _directions = direction ? [direction] : directions;
for (let i = 0; i < _directions.length; i++) {
const res = [[row, col]]; const res = [[row, col]];
const [dx, dy] = directions[i]; const [dx, dy] = _directions[i];
let x = row + dx; let x = row + dx;
let y = col + dy; let y = col + dy;
...@@ -55,13 +58,16 @@ export function checkWin({ row, col, board, player, win_size }) { ...@@ -55,13 +58,16 @@ export function checkWin({ row, col, board, player, win_size }) {
// 机器人下棋 // 机器人下棋
export function robotPlay(board, player, win_size) { export function robotPlay(board, player, win_size) {
// 深度复制一份棋盘
const _board = JSON.parse(JSON.stringify(board))
const maxScorePos = {}; const maxScorePos = {};
let maxScore = -1; let maxScore = -1;
const opponent = (player === is_black) ? is_white : is_black; const opponent = (player === is_black) ? is_white : is_black;
// 空格位置 // 空格位置
const empty_points = board.map((item, row) => { const empty_points = _board.map((item, row) => {
return item.flatMap((_item, col) => _item === is_empty ? [row, col] : []) return item.flatMap((_item, col) => _item === is_empty ? [[row, col]] : [])
}).flat(2) }).flat(1)
console.log(empty_points)
// 对每个空位进行评分 // 对每个空位进行评分
empty_points.forEach((point) => { empty_points.forEach((point) => {
...@@ -69,28 +75,28 @@ export function robotPlay(board, player, win_size) { ...@@ -69,28 +75,28 @@ export function robotPlay(board, player, win_size) {
const [i, j] = point; const [i, j] = point;
// 判断下子后是否获胜 // 判断下子后是否获胜
board[i][j] = player; _board[i][j] = player;
const win = checkWin({ row: i, col: j, board, player, win_size }); const win = checkWin({ row: i, col: j, board: _board, player, win_size });
if (win) { if (win) {
score = Infinity; score = Infinity;
} else { } else {
// 判断对手是否能在下一步获胜,如果能,则这个点的分数为 0 // 判断对手是否能在下一步获胜,如果能,则这个点的分数为 0
board[i][j] = opponent; _board[i][j] = opponent;
const oppWin = checkWin({ row: i, col: j, board, player: opponent, win_size }); const oppWin = checkWin({ row: i, col: j, board: _board, player: opponent, win_size });
if (oppWin) { if (oppWin) {
score = 0; score = 0;
} else { } else {
// 计算当前棋盘局面的得分 // 计算当前棋盘局面的得分
const horizontal = countPieces({ row: i, col: j, board, player, win_size, direction: "horizontal" }); const horizontal = countPieces({ row: i, col: j, _board, player, win_size, direction: "horizontal" });
const vertical = countPieces({ row: i, col: j, board, player, win_size, direction: "vertical" }); const vertical = countPieces({ row: i, col: j, _board, player, win_size, direction: "vertical" });
const diagonalDown = countPieces({ row: i, col: j, board, player, win_size, direction: "diagonalDown" }); const diagonalDown = countPieces({ row: i, col: j, _board, player, win_size, direction: "diagonalDown" });
const diagonalUp = countPieces({ row: i, col: j, board, player, win_size, direction: "diagonalUp" }); const diagonalUp = countPieces({ row: i, col: j, _board, player, win_size, direction: "diagonalUp" });
score = Math.max(horizontal, vertical, diagonalDown, diagonalUp); score = Math.max(horizontal, vertical, diagonalDown, diagonalUp);
} }
} }
board[i][j] = null; // 恢复棋盘状态 _board[i][j] = null; // 恢复棋盘状态
// 选取分数最高的空位 // 选取分数最高的空位
if (score > maxScore) { if (score > maxScore) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册