首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在为我的reversi游戏开发一个极小极大算法(w/ react.js),但它给出了RangeError

我正在为我的reversi游戏开发一个极小极大算法(w/ react.js),但它给出了RangeError
EN

Stack Overflow用户
提问于 2021-05-07 18:05:47
回答 1查看 52关注 0票数 0

我正在为我的reversi游戏开发一个Minimax算法,这样我就可以有一个强大的AI对手面对玩家。但我遇到了这个错误:"RangeError:超出了最大调用堆栈大小“

我能做些什么来修复它?

下面是代码(我不会用伪代码来解释它,因为我的问题不是关于函数不工作):

AI算法:

代码语言:javascript
复制
function minimaxAI(){
        const squares = history[history.length - 1].slice()
        let bestScore = -Infinity
        let bestMove = null
        // Perform minimax algorithm for each valid move and pick the best score
        const AI = (ourPlayer === "white") ? "black" : "white"
        const AIValidMoves = checkSquaresForSides()[(AI === "black") ? 0 : 1 ]
        console.log(AIValidMoves)
        for (var AIMove=0;AIMove<AIValidMoves.length;AIMove++){
            const crds = AIValidMoves[AIMove].turned
            crds.unshift(AIValidMoves[AIMove].coordinates)
            const newBoard = handleMove(crds, squares)
            const score = minimax(newBoard,5,(AI === "black") ? false : true)
            if (score > bestScore) {
                bestScore = score
                bestMove = crds
            }
        }
        const handling = bestMove
        const upcomingAI = handleMove(handling)
        setHistory(upcomingAI)
        setStepNumber(upcomingAI.length - 1)
    }

Minimax算法:

代码语言:javascript
复制
function minimax(board, depth, isMaximizing){
        const AI = (ourPlayer === "white") ? "BLACK" : "WHITE"
        let result = setWinnerAndTurn(true)
        // Retrieve the filled squares of the current board
        let currentStones = 0
        history[history.length - 1].slice().map((row, y) =>
            row.map((square, x) =>
                currentStones += (square) ? 1 : 0
            )
        );
        let newStones = 0
        // Retrieve the filled squares of the updated board
        board.map((row, y) =>
            row.map((square, x) =>
                newStones += (square) ? 1 : 0
            )
        );
        console.log(currentStones)
        console.log(newStones)
        if (result.winner || newStones - currentStones === depth) {
            // Get the evaluated version of the last move
            let score = (result.winner === AI) ? Infinity : -Infinity
            return score
        }
        if (isMaximizing){
            // Play the optimal move for the opposing player
            let bestScore = -Infinity
            let bestMove = null
            const selection = (AI === "BLACK") ? 0 : 1 
            const AIValidMoves = checkSquaresForSides()[selection]
            console.log(AIValidMoves)
            for (var AIMove=0;AIMove<AIValidMoves.length;AIMove++){
                const crds = AIValidMoves[AIMove].turned
                crds.unshift(AIValidMoves[AIMove].coordinates)
                const newBoard = handleMove(crds, board)
                const score = minimax(newBoard,10,(selection === 0) ? false : true)
                if (score > bestScore) {
                    bestScore = score
                    bestMove = crds
                }
            }
            return bestScore
        } else {
            // Play the optimal move for the maximizing player
            let bestScore = Infinity
            let bestMove = null
            const selection = (AI === "BLACK") ? 1 : 0 
            const AIValidMoves = checkSquaresForSides()[selection]
            console.log(AIValidMoves)
            for (var AIMove=0;AIMove<AIValidMoves.length;AIMove++){
                const crds = AIValidMoves[AIMove].turned
                crds.unshift(AIValidMoves[AIMove].coordinates)
                const newBoard = handleMove(crds, board)
                const score = minimax(newBoard,10,(selection === 1) ? false : true)
                if (score < bestScore) {
                    bestScore = score
                    bestMove = crds
                }
            }
            return bestScore
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-07 19:25:47

顺便说一下,我在1小时前解决了这个问题(现在发布)。我忘记了在朝向树根的每一步都减小深度值。刚刚更改了这些部分:

第一个:

代码语言:javascript
复制
if (result.winner) {
    // Get the evaluated version of the last move
    let score = (result.winner === AI) ? Infinity : -Infinity
    return score
} else if (depth === 0) {
    return 0
}

第二个:

代码语言:javascript
复制
const score = minimax(newBoard,depth - 1,(selection === 1) ? false : true)

否则,函数将陷入无限移动。

此外,您还可以删除以下内容:

代码语言:javascript
复制
let currentStones = 0
history[history.length - 1].slice().map((row, y) =>
    row.map((square, x) =>
    currentStones += (square) ? 1 : 0
    )
);
let newStones = 0
// Retrieve the filled squares of the updated board
board.map((row, y) =>
    row.map((square, x) =>
        newStones += (square) ? 1 : 0
    )
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67432880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档