Mon Jul 3 06:35:00 UTC 2023 inscode

上级 3601b359
......@@ -16,22 +16,24 @@ function confirm() {
isWin = []
lattices.value = Array(size.value).fill([]).map(() => Array(size.value).fill(is_empty))
}
// 玩家下棋
async function playChess(item, row, col) {
if (is_load.value) return
if (isWin.length || item !== is_empty) return
// 玩家下棋
lattices.value[row][col] = is_black
const test = checkWin({ row, col, board: lattices.value, player: is_black, win_size: win_size.value })
if (test) {
isWin = test
return
} else {
}
return
// 机器人下棋
const [x, y] = robotPlay(lattices.value, is_white, win_size.value)
lattices.value[x][y] = is_white
const test = checkWin({ row: x, col: y, board: lattices.value, player: is_white, win_size: win_size.value })
if (test) {
isWin = test
}
const testRobo = checkWin({ row: x, col: y, board: lattices.value, player: is_white, win_size: win_size.value })
if (testRobo) {
isWin = testRobo
}
}
......
......@@ -49,12 +49,13 @@ input {
.content-lattice {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
.row {
display: flex;
flex-direction: column;
flex-direction: row;
}
.col {
......
......@@ -11,25 +11,20 @@ export const directions = [
[1, -1] // 左下方向
]
/**
* @typedef Param
* @property {object} param 对象a
* @property {number} param.row 行
* @property {number} param.col 列
* @property {(is_empty|is_white|is_black)[]} param.board 棋盘
* @property {is_white|is_black|is_empty} [param.player] 当前棋子类型
* @property {number} param.win_size 需要几个棋子才赢
*/
/**
* 检查四个方向连续的棋子数
* @param {Param}
* @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|is_empty} [param.player] 当前棋子类型
* @param {number} param.win_size 需要几个棋子才赢
* @returns {[number, number][] | false} 赢棋的下标二维数组 或者 不能赢
*/
export function checkWin({ row, col, board, player, win_size }) {
const _row = board.length;
const _col = board[0].length
const pieceType = player ?? board[row][col]
const ROW = board.length;
const COL = board[0].length
const TYPE = player ?? board[row][col]
let res = []
......@@ -40,7 +35,7 @@ export function checkWin({ row, col, board, player, win_size }) {
let y = col + dy;
// 向正反两个方向扩展,检查是否有连续的五个相同棋子
while (x >= 0 && x < _row && y >= 0 && y < _col && board[x][y] === pieceType) {
while (x >= 0 && x < ROW && y >= 0 && y < COL && board[x][y] === TYPE) {
res.push([x, y])
x += dx;
y += dy;
......@@ -48,7 +43,7 @@ export function checkWin({ row, col, board, player, win_size }) {
x = row - dx;
y = col - dy;
while (x >= 0 && x < _row && y >= 0 && y < _col && board[x][y] === pieceType) {
while (x >= 0 && x < ROW && y >= 0 && y < COL && board[x][y] === TYPE) {
res.push([x, y])
x -= dx;
y -= dy;
......@@ -90,7 +85,7 @@ export function robotPlay(board, robot, win_size) {
if (win) {
return [row, col]
} else {
// 判断对手是否能在下一步获胜
// 判断对手是否能在这一个空位上获胜
const oppWin = checkWin({ row, col, board, player: is_black, win_size });
if (oppWin) {
return [row, col]
......@@ -192,7 +187,7 @@ function getDirectionScore(board, row, col, [x, y], piece_type) {
*/
function estimateScore({ board, row, col, win_size }) {
return direction_4.reduce((r, [p1, p2]) => {
const square = getDirectionScore(board, row, col, p1, win_size, is_black) + getDirectionScore(board, row, col, p2, win_size, is_black)
const square = getDirectionScore(board, row, col, p1, win_size, is_white) + getDirectionScore(board, row, col, p2, win_size, is_white)
return r + 10 ** square
}, 0)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册