【问题标题】:Rock Paper Scissors game issue剪刀石头布游戏问题
【发布时间】:2021-07-11 14:37:38
【问题描述】:

我构建了一个非常基本的石头剪刀布游戏,但似乎游戏无法正确捕获 userSelection 或输出正确的结果...

我花了数小时研究和调整我的代码的各个方面,但无法弄清楚 - 我猜我太绿了。

我不想要一个完全不同的版本来说明如何做到这一点 - 我想修复这个版本并了解它为什么目前不起作用:)

https://codepen.io/anna_whiskey/pen/XWRjQXV

    const gameInputs = ['rock', 'paper', 'scissors'];
    let computerSel;
    let round1Answer;
    let userSelection;


    
    function game() {
    function humanPlay() {

      document.getElementById("btn1").addEventListener("click", () => {
                userSelection = "rock";
            
            })
      document.getElementById("btn2").addEventListener("click", () => {
                userSelection = "paper";
                
            })
      document.getElementById("btn3").addEventListener("click", () => {
                userSelection = "scissors";
                
            })
            
        }

        

        function computerPlay() {
            computerSel = Math.floor(Math.random() * gameInputs.length);
            round1Answer = (gameInputs[computerSel]);
            console.log((gameInputs[computerSel]));
            humanPlay();
        }
        computerPlay();

        document.getElementById("outcome").textContent = `You: ${userSelection} Computer: ${round1Answer}`

        function playRound(round1Answer, userSelection) {

            if (userSelection === 'rock' && round1Answer === 'scissors') {
                alert('You WIN!');
            } else if (userSelection === 'rock' && round1Answer === 'rock') {
                alert('It/s a tie!');
            } else if (userSelection === 'paper' && round1Answer === 'rock') {
                alert('You WIN!');
            } else if (userSelection === 'paper' && round1Answer === 'paper') {
                alert('It/s a tie!');
            } else if (userSelection === 'scissors' && round1Answer === 'paper') {
                alert('You WIN!');
            } else if (userSelection === 'scissors' && round1Answer === 'scissors') {
                alert('It/s a tie!');
            } else {
                alert('You LOSE!');
            }
        }
        playRound(round1Answer, userSelection);
    } 

<!DOCTYPE html>
<html>
 <head>
<meta charset="utf-8">
</head>
<title>Rock Paper Scissors</title>
<body>
<div class="bg"></div>


<button id="btn1" onclick="game()">Rock</button>
<button id="btn2" onclick="game()">Paper</button>
<button id="btn3" onclick="game()">Scissors</button>

<div id="outcome"></div>


<link rel="stylesheet" href="rps.css">
<script type="text/javascript" src="rpsv2.js"></script>

</body>
</html>        

【问题讨论】:

  • 我不知道 Node 的任何属性是否被点击。 document.getElementById("btn1").clicked 这不是有效的语法。顺便说一句,在这一行中比较document.getElementById("btn1").clicked = true,您为此节点分配了一个新属性到true,所以每次第一个条件都变得真实,这就是为什么你每次都得到摇滚。

标签: javascript function onclick


【解决方案1】:

您应该尝试为人工输入添加事件侦听器

const gameInputs = ['rock', 'paper', 'scissors'];
let computerSel;
let round1Answer;
let userSelection;


function game() {

    function humanPlay() {

        document.getElementById("btn1").addEventListener("click", () => {
            userSelection = "rock";
            console.log(`User Selection: ${userSelection}`)
        })
        document.getElementById("btn2").addEventListener("click", () => {
            userSelection = "paper";
            console.log(`User Selection: ${userSelection}`)
        })
        document.getElementById("btn3").addEventListener("click", () => {
            userSelection = "scissor";
            console.log(`User Selection: ${userSelection}`)
        })
    }

    function computerPlay() {

        computerSel = Math.floor(Math.random() * 3);
        round1Answer = (gameInputs[computerSel]);
        // console.log(`Round ans is ${round1Answer}`)

    }

    function playRound(round1Answer, userSelection) {

        if (userSelection == 'rock' && round1Answer == 'scissors') {
            alert('You WIN!');
        } 
        else if (userSelection == 'paper' && round1Answer == 'rock') {
            alert('You WIN!');
        } 
        else if (userSelection == 'scissors' && round1Answer == 'paper') {
            alert('You WIN!');
        } 
        else if (userSelection == 'rock' && round1Answer == 'rock') {
            alert('It/s a tie!');
        } 
        else if (userSelection == 'paper' && round1Answer == 'paper') {
            alert('It/s a tie!');
        } 
        else if (userSelection == 'scissors' && round1Answer == 'scissors') {
            alert('It/s a tie!');
        } 
        else {
            alert('You LOSE!');
        }
    }
    humanPlay();
    computerPlay();
    playRound(round1Answer, userSelection);
}

用这个我成功地记录了石头和剪刀

【讨论】:

  • 谢谢你哦!由于某种原因,我无法让此代码为我运行 - 你确定它适合你吗?
  • codepen.io/avicdr/pen/eYWdwYm 检查它是否有效
  • 啊,太好了,现在可以了,谢谢! (我的 html 错了..) - 但是它没有将 userSelection 记录到控制台,你知道为什么吗?
  • 第一次尝试时不会记录
  • 它将从第二次点击开始记录
【解决方案2】:

您可以通过以下脚本简单地完成任务 JS

const gameInputs = ['rock', 'paper', 'scissors'];
let computerSel;
let round1Answer;
let userSelection;


function game (humanChoosed) {
  
  userSelection = gameInputs[humanChoosed];

  function computerPlay() {
    computerSel = Math.floor(Math.random() * gameInputs.length);
    round1Answer = (gameInputs[computerSel]);
  }
computerPlay();

  document.querySelector('h1').textContent = `Human: ${userSelection} Computer: ${round1Answer}`;

  function playRound (round1Answer, userSelection) {
 
    if (userSelection === 'rock' && round1Answer === 'scissors') {
        alert ('You WIN!');
    } else if (userSelection === 'rock' && round1Answer === 'rock') {
        alert ('It/s a tie!');
    } else if (userSelection === 'paper' && round1Answer === 'rock') {
        alert ('You WIN!');
    } else if (userSelection === 'paper' && round1Answer === 'paper') {
        alert ('It/s a tie!');
    } else if (userSelection === 'scissors' && round1Answer === 'paper') {
        alert ('You WIN!');
    } else if (userSelection === 'scissors' && round1Answer === 'scissors') {
        alert ('It/s a tie!');
    } else {
       alert ('You LOSE!');
    }
  } 
  playRound (round1Answer, userSelection);
}

HTML

<body>
  <div class="bg"></div>

  <button id="btn1" onclick="game(0)">Rock</button> 
  <button id="btn2" onclick="game(1)">Paper</button>  
  <button id="btn3" onclick="game(2)">Scissors</button> 

  <h1>Hello</h1>
  <link rel="stylesheet" href="rps.css">
  <script type="text/javascript" src="rps.js"></script>
</body>

【讨论】:

  • 非常感谢 Bin Rohan - 这行得通,但我很想知道为什么我的版本不工作(或者如果可能的话,如何让它工作):)
  • ALSO - 我不明白计算机如何确定用户在上面的示例中选择了哪个按钮??
  • 那些按钮调用函数game()并将索引号传递给你的这个数组const gameInputs = ['rock', 'paper', 'scissors'];。然后在您的game 函数中,它从userSelection = gameInputs[humanChoosed]; 这一行检索给定索引的值。
  • @Anna-Delta,回答你的第一条评论,首先在 JS 中没有任何链接 document.getElementById("btn1").clicked 。其次,在这一行 document.getElementById("btn1").clicked = true 中,您正在创建新属性 clicked 并将 true 分配为值。
  • 如果您想要与您的解决方案类似,那么您可以做到最好:if (document.getElementById("btn1").textContent.toLowerCase() === 'rock') { userSelection = 'rock'; console.log(userSelection); }
猜你喜欢
  • 1970-01-01
  • 2016-01-16
  • 2022-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多