0

我正在创建一个石头剪刀布游戏(我故意省略了代码的某些部分,只是为了让事情更容易理解),并且我希望能够在按下一次按钮后继续游戏。现在游戏的设置方式,一旦你按下按钮,游戏就会告诉你是赢了还是输了。此时,如果您再次尝试按下该按钮,则不会发生任何事情。我一直在尝试几种不同的方法,但我似乎无法找到一种方法来做到这一点。如果有人可以帮助我在第一次单击后再次单击该按钮,则游戏能够继续进行,计算机选择新的选择,依此类推,将不胜感激。谢谢你。

const choices = ["rock", "paper", "scissors"];
const computersChoice = choices[Math.floor(Math.random() * choices.length)];
const rockButton = document.getElementById("rockbtn");
const updateText = document.getElementById('textField')
let yourChoice
let status
let statusComp


//function for when user clicks rock button
function userRock() {
    rockButton.addEventListener ("click", function() {
        yourChoice = choices[0]  
        execute()
  });
}

function execute(){
    checker()
    computersTurn()
}

// checks to see if user made a choice
function checker(){
    if (yourChoice === choices[0]) {
        status ='rock'
    }
  }  

// computer chooses
function computersTurn() {
        statusComp = computersChoice
        //logs check to make sure the program is running correctly
        console.log(status)
        console.log(statusComp)
        if (status === statusComp) {
            updateText.innerText = 'It\s a tie'
        } else if (status === 'rock' && statusComp === 'paper'){
            updateText.innerText = 'You lose... Computer chose paper'
        } else if (status === 'rock' && statusComp === 'scissors'){
            updateText.innerText = 'You win... Computer chose scissors'
        }
  }


function startGame(){
    userRock()

}

startGame()
4

2 回答 2

2

问题是您只获得了一次computerChoice。您应该在每次点击时获得计算机选择。

const choices = ["rock", "paper", "scissors"];
let computersChoice ;
const rockButton = document.getElementById("rockbtn");
const updateText = document.getElementById('textField')
let yourChoice
let status
let statusComp


//function for when user clicks rock button
function userRock() {
    rockButton.addEventListener ("click", function() {
        yourChoice = choices[0];
        computersChoice = choices[Math.floor(Math.random() * choices.length)];
        execute()
  });
}

function execute(){
    checker()
    computersTurn()
}

// checks to see if user made a choice
function checker(){
    if (yourChoice === choices[0]) {
        status ='rock'
    }
  }  

// computer chooses
function computersTurn() {
        statusComp = computersChoice
        //logs check to make sure the program is running correctly
        console.log(status)
        console.log(statusComp)
        if (status === statusComp) {
            updateText.innerText = 'It\s a tie'
        } else if (status === 'rock' && statusComp === 'paper'){
            updateText.innerText = 'You lose... Computer chose paper'
        } else if (status === 'rock' && statusComp === 'scissors'){
            updateText.innerText = 'You win... Computer chose scissors'
        }
  }


function startGame(){
    userRock()

}

startGame()
<button id="rockbtn">Play</button>
<span id="textField"/></span>

于 2020-05-25T12:16:59.870 回答
1

这很简单。只需将您创建计算机选项的那一行移到您的computerTurns()函数中。这样计算机就不会一直使用相同的结果。其他一切都可以保持不变并且会起作用。

const choices = ["rock", "paper", "scissors"];
const rockButton = document.getElementById("rockbtn");
const updateText = document.getElementById('textField')
let computersChoice;
let yourChoice
let status
let statusComp


//function for when user clicks rock button
function userRock() {
    rockButton.addEventListener ("click", function() {
        yourChoice = choices[0]  
        execute()
  });
}

function execute(){
    checker()
    computersTurn()
}

// checks to see if user made a choice
function checker(){
    if (yourChoice === choices[0]) {
        status ='rock'
    }
  }  

// computer chooses
function computersTurn() {
        computersChoice = choices[Math.floor(Math.random() * choices.length)];
        statusComp = computersChoice
        //logs check to make sure the program is running correctly
        console.log(status)
        console.log(statusComp)
        if (status === statusComp) {
            updateText.innerText = 'It\s a tie'
        } else if (status === 'rock' && statusComp === 'paper'){
            updateText.innerText = 'You lose... Computer chose paper'
        } else if (status === 'rock' && statusComp === 'scissors'){
            updateText.innerText = 'You win... Computer chose scissors'
        }
  }


function startGame(){
    userRock()

}

startGame()
<button id="rockbtn">Rock</button>
<textarea id="textField"></textarea>

于 2020-05-25T12:20:25.140 回答