Javascript 试图在react中的render方法中调用两个AI函数,但它们';重新异步调用
我正在做一个有AI模式的棋盘游戏。现在我尝试调用AI的移动,但它是异步调用的。我不知道我能做什么。我试过很多东西;同步调用setTimeout,在onClick内调用它。。。你有什么解决办法吗 代码如下:Javascript 试图在react中的render方法中调用两个AI函数,但它们';重新异步调用,javascript,reactjs,asynchronous,render,Javascript,Reactjs,Asynchronous,Render,我正在做一个有AI模式的棋盘游戏。现在我尝试调用AI的移动,但它是异步调用的。我不知道我能做什么。我试过很多东西;同步调用setTimeout,在onClick内调用它。。。你有什么解决办法吗 代码如下: render() { // datas that will be rendered regularly come here const current = this.state.history[this.state.stepNumber] con
render() {
// datas that will be rendered regularly come here
const current = this.state.history[this.state.stepNumber]
const elemsPlayer = this.checkElementsAround(this.checkEmptySpaces())
if (this.state.ourPlayer !== (this.state.blackIsNext) ? 'black' : 'white') {
if (this.state.gameMode === "AI-Easy") {
setTimeout(()=>{
this.easyAI()
}, 1500)
} else if (this.state.gameMode === "AI-Hard") {
setTimeout(()=>{
this.minimaxAI()
}, 1500)
}
}
// Return the game scene here
return (
<div id="master-container">
<div id="gameboard-container">
<GameBoard squares={current} onClick={(row,col) => {
// Where player's move will be called
}}/>
</div>
</div>
)
}
render(){
//将定期呈现的数据在此显示
const current=this.state.history[this.state.stepNumber]
const elemsPlayer=this.checkElementsAround(this.checkEmptySpace())
如果(this.state.ourPlayer!==(this.state.blackIsNext)“‘黑色’:‘白色’){
如果(this.state.gameMode==“AI轻松”){
设置超时(()=>{
这个.easyAI()
}, 1500)
}否则如果(this.state.gameMode==“AI硬”){
设置超时(()=>{
这是minimaxAI()
}, 1500)
}
}
//在这里返回游戏场景
返回(
{
//玩家的移动将被调用的位置
}}/>
)
}
以下是easyAI函数(minimax one目前为空):
easyAI(){
const elemsAI=this.checkElementsAround(this.checkEmptySpace())
const validsAI=[]
for(设elAI=0;elAI{
this.setWinnerAndTurn(this.state.history)
//console.log(this.state.history)
})
}
}
如果为了找到一个解决方案,你必须看看其他的东西,请一定要让我知道
提前感谢:)请提供您的this.easyAI()和this.minimaxAI()函数sminimaxai目前为空,我将立即放置另一个函数。您可以检查easyAI函数。我看到了,函数似乎非常简单。但无法解决您的主要问题。问题是,当游戏开始时,人工智能并不等待轮到它,而是自己玩
easyAI(){
const elemsAI = this.checkElementsAround(this.checkEmptySpaces())
const validsAI = []
for (let elAI=0;elAI<elemsAI.length;elAI++) {
const turningAI = this.checkTurningStones(elemsAI[elAI].directions, this.state.blackIsNext)
//console.log(turningAI)
if (turningAI.length !== 0) {
validsAI.push(elemsAI[elAI])
}
}
// ValidsAI returns an object {emptySquares: coordinates of the empty square, turningStones: coordinates of the stones that will be turned upside down if emptySquares is played}
//Check if AI has a move to make
if (validsAI.length !== 0) {
//Perform the move
var elementToTurnAI = null
try{
elementToTurnAI = validsAI[Math.floor(Math.random()*validsAI.length)]
} catch {
console.log(null)
}
//console.log(elementToTurnAI)
const turningAI = this.checkTurningStones(elementToTurnAI.directions, this.state.blackIsNext)
const selfAI = elementToTurnAI.coordinates
//console.log(selfAI)
//console.log(turningAI)
turningAI.unshift([selfAI[0],selfAI[1]])
// Turn the stones
const upcomingAI = this.handleMove(turningAI)
// Update the state
this.setState({
history: upcomingAI,
stepNumber: upcomingAI.length - 1
},() => {
this.setWinnerAndTurn(this.state.history)
//console.log(this.state.history)
})
}
}