diff --git a/src/App.vue b/src/App.vue index 68d3d43360e1421bbb52ad355a8aaed472bfc692..9006c2a0e99b6ddcb3a9eca057634004bf22e2dc 100644 --- a/src/App.vue +++ b/src/App.vue @@ -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 { - 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 - } + } + return + // 机器人下棋 + const [x, y] = robotPlay(lattices.value, is_white, win_size.value) + lattices.value[x][y] = is_white + const testRobo = checkWin({ row: x, col: y, board: lattices.value, player: is_white, win_size: win_size.value }) + if (testRobo) { + isWin = testRobo } } diff --git a/src/assets/main.css b/src/assets/main.css index 6b74af6e41e456ee02d492ac3476b4b383b5cce5..2f2af80161ad4720974b1e66cf5aa66c85932752 100644 --- a/src/assets/main.css +++ b/src/assets/main.css @@ -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 { diff --git a/src/utils/index.js b/src/utils/index.js index 7d3a6a9ec8c4350bf7c2e1b5105dc5d4287a6685..a224a2e1dcdb448bc013aea8e672a1f6c5f4682d 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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) }