Mon Jul 3 06:35:00 UTC 2023 inscode

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