Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为react游戏获取cpu v cpu函数_Javascript_Reactjs_Button_Onclick - Fatal编程技术网

Javascript 如何为react游戏获取cpu v cpu函数

Javascript 如何为react游戏获取cpu v cpu函数,javascript,reactjs,button,onclick,Javascript,Reactjs,Button,Onclick,我已经在react应用程序中构建了一个简单的石头、布、剪刀游戏,我正在尝试创建一个按钮,当点击它时,cpu可以自己玩(cpu v cpu)。当前,用户使用以下功能播放cpu: handleMove(playerMove) { var cpuMove = this.getCPUMove(), //get the current state of the cpuMove function result = this.scoreGame(playerMove, cpuMove) //get

我已经在react应用程序中构建了一个简单的石头、布、剪刀游戏,我正在尝试创建一个按钮,当点击它时,cpu可以自己玩(cpu v cpu)。当前,用户使用以下功能播放cpu:

handleMove(playerMove) {
  var cpuMove = this.getCPUMove(), //get the current state of the cpuMove function
    result = this.scoreGame(playerMove, cpuMove) //get the current result from the playerMove and cpuMove functions

  console.log('Player chose: ' + playerMove + '. CPU chose: ' + cpuMove + '.')
  console.log('The winner is ' + (result === 1 ? 'player.' : result === 2 ? 'CPU.' : 'draw.'))

  this.setState((prev,props) => ({
    games: [...prev.games, { player: playerMove, cpu: cpuMove, winner: result }]
  }))
}
为了让cpu自动播放,我尝试了以下按钮命令:

<button type="button" onClick={ this.handleMove()}><span>Computer v Computer</span></button>
计算机v计算机
但游戏最终显示错误:


我对学习反应相当陌生。完整代码保存在中。

问题在于渲染函数,它应该是这样的:

<button type="button" onClick={() => this.handleMove(this.getCPUMove()) }><span>Computer v Computer</span></button>
this.handleMove(this.getCPUMove())}>computerv Computer

这样,cpu每次都会丢失。您知道一种区分cpu1和cpu2的方法吗?只要对控制台日志语句进行几次编辑,这种方法就非常有效。非常感谢。