提问者:小点点

石头剪刀布和一个神秘的响应错误


我目前正在开发我的石头剪刀布游戏。具体来说,我正在开发< code>eventListeners并确保当有人点击“石头”按钮时,会触发一个函数,让计算机随机选择石头、布或剪刀,然后在一个容器内创建一个新段落,并附带一个连接响应,如“你赢了,布打败了石头”。除了一个之外,我所有的回答都有效。

let results=playerWin rockWin是由于某种原因而无法工作的唯一响应。它应该说“你赢了,石头打败剪刀”。相反,它会回去说这是一场平局。我有<code>console.log</code>ged,看看这是否在没有附加<code>eventlistener</code<的情况下发生。由于某种原因,它可以工作,但当我希望它在触发时打印时,它就是不工作。你能帮我找到我缺少的东西吗?如果问题不在我的代码范围内,我肯定可以提供更多的代码。我只是不想用无关的代码阻塞我的帖子。

const options = ["rock", "paper", "scissors"];
const rock = options[0];
const paper = options[1];
const scissors = options[2];
const rockWin = "Rock beats scissors.";
const paperWin = "Paper beats rock.";
const scissorsWin = "Scissors beats paper";
const tie = "It was a tie";
const playerWin = "You won! ";
const computerWin = "You lose! ";
const oops = "Please try again";
let computerPoints = 0;
let playerPoints = 0;

//eventListeners to html

const btn = document.querySelectorAll('btn');
const rockbtn = document.querySelector('#rockbtn');
const paperbtn = document.querySelector('#paperbtn');
const scissorsbtn = document.querySelector('#scissorsbtn');
const resultContainer = document.querySelector('#resultContainer')
const resultPara = document.querySelector('#resultPara')
//Computer's choice

function computerPlay(){
let compChoice = options[Math.floor(Math.random() * options.length)];
return compChoice;
}


//One round of the game
    function playRound(playerSelection, computerSelection){
        if (playerSelection === computerSelection){
            let results = tie;
            resultPara.textContent = results;
            console.log(playerSelection,playerPoints);
            console.log(computerSelection, computerPoints);
            return tie;
        }

        else if(playerSelection === rock && computerSelection === paper){
            computerPoints = computerPoints + 1;
            let results = computerWin + paperWin;
            resultPara.textContent = results;
            console.log(playerSelection,playerPoints);
            console.log(computerSelection,computerPoints);
            return computerWin + paperWin;
        }

        else if(playerSelection === rock && computerSelection === scissors){
            playerPoints = playerPoints + 1;
            let results = playerWin + rockWin;
            console.log(playerSelection,playerPoints);
            console.log(computerSelection,computerPoints);
            return playerWin + rockWin;

再往下,我有我的事件侦听器。

paperbtn.addEventListener('click', paperPlay);
scissorsbtn.addEventListener('click', scissorsPlay);

function rockPlay(){
    playRound(rock, computerPlay());
}


function paperPlay(){
    playRound(paper, computerPlay());
}
function scissorsPlay(){
    playRound(scissors, computerPlay());
}



共1个答案

匿名用户

我不知道为什么它说这是平局。您确实忘记为玩家选择===纸张放置连击,但我认为这不是您指定的问题的来源。在包含的事件侦听器中,您忘记了一个用于摇滚的事件侦听器,它应该是rockbtn.addEventListener('click', rockPlay)。我不知道这些是否有助于解决您的问题,但它们是我在查看您提供的内容后注意到的事情。下面我有你的 if/else if 链,你用它来确定结果。我为玩家添加了连击===纸,并放了我能想到的任何其他笔记。我希望这在某种程度上有所帮助!

if (playerSelection === computerSelection){
            let results = tie;
            resultPara.textContent = results;
            console.log(playerSelection,playerPoints);
            console.log(computerSelection, computerPoints);
            return tie;
        }

        else if(playerSelection === rock && computerSelection === paper){
            computerPoints += 1;
            let results = computerWin + paperWin;
            resultPara.textContent = results;
            console.log(playerSelection,playerPoints);
            console.log(computerSelection,computerPoints);
            return results; // use results variable because you can
        }

        else if(playerSelection === rock && computerSelection === scissors){
            playerPoints += 1; // I recommend using the += operator for this
            let results = playerWin + rockWin;
            console.log(playerSelection,playerPoints);
            console.log(computerSelection,computerPoints);
            return results;

        // adding the combos where playerSelection == paper
        
        else if(playerSelection === paper && computerSelection === rock){
            playerPoints += 1; 
            let results = playerWin + paperWin;
            console.log(playerSelection,playerPoints);
            console.log(computerSelection,computerPoints);
            return results;

        else if(playerSelection === paper && computerSelection === scissors){
            computerPoints += 1;
            let results = computerWin + rockWin;
            console.log(playerSelection,playerPoints);
            console.log(computerSelection,computerPoints);
            return results;