剪刀石头布项目Odin项目-按钮辅助

如何解决剪刀石头布项目Odin项目-按钮辅助

我已经尝试了好几个小时,以便在您单击页面上的任何按钮(石头,纸张,剪刀)时,它将岩石,纸张或剪刀的值传递给我的变量“ userChoice”。我对javascript还是很陌生,所以请记住我的代码可能非常错误。任何帮助/建议将不胜感激!

这是我的HTML代码

<html>
<head></head>
  <title>Rock,Paper,Scissors</title>
  <link rel="stylesheet" href="styles.css">
  <Meta charset="UTF-8"/>
</head>
<h1>Welcome to the Rock,Scissors Championship!</h1>
<body>

  <div class="selections">
    <div class="rock">
      <input type="image" id="rock" src="/home/johannesvw3/Desktop/web-projects/Rock/images/rock.png">
      <button class="text-rock">ROCK</button>
      <div class="paper">
        <input type="image" id="paper" src="/home/johannesvw3/Desktop/web-projects/Rock/images/paper.png">
        <button class="text-paper">PAPER</button>
        <div class="scissor">
          <input type="image" id="scissors" src="/home/johannesvw3/Desktop/web-projects/Rock/images/scissors.png">
          <button class="text-scissors">SCISSORS</button>
    </div>  
    <div class="scoreboard">scoreboard
      <div class="user">User:
      <div class="comp">Computer:
    </div>


  <script src="rock.js"></script>
</body>
</html>

这是我的Javascript:

//userChoice
const myButtonRock = document.querySelector('.text-rock');
const myButtonPaper = document.querySelector('.text-paper');
const myButtonScissors = document.querySelector('.text-scissors');

function userChoices() {
  let userChoice = ["rock","paper","scissors"];
  return userChoice;
}
  myButtonRock.onclick = function() {
    userChoice('rock');
  }

  myButtonPaper.onclick = function() {
    userChoice('paper');
  }

  myButtonScissors.onclick = function() {
    userChoice('scissors');
  }

  //Computer Choice
  function compChoice() {
    let compChoice = Math.floor(Math.random() * 3);
    if (compChoice == 0) {
      compChoice = "rock";
    } else if (compChoice == 1) {
      compChoice = "paper";
    } else {
      compChoice = "scissors";
    }
    return compChoice
  }

  let userscore = 0;
  let compscore = 0;
  let draws = 0;

  
  function playRound(userChoice,compChoice) {
  
  //Result Messages
    let userWinsRound = "You Win!"
    let compWinsRound = "You Lose :(";
    let draw = "It's a draw";
  
    //Rock Choice
    if (userChoice == "rock" && compChoice == "scissors") {
      alert("user score:" + " " + ++userscore + " " + userWinsRound)
    } else if (userChoice == "rock" && compChoice == "paper") {
      alert("computer score:" + " " + ++compscore + " " + compWinsRound)
    } else if (userChoice == "rock" && compChoice == "rock") {
      alert("draws:" + " " + ++draws + " " + draw)
    }
    //paper Choice
    if (userChoice == "paper" && compChoice == "rock") {
      alert("user score:" + " " + ++userscore + " " + userWinsRound)
    } else if (userChoice == "paper" && compChoice == "scissors") {
      alert("computer score:" + " " + ++compscore + " " + compWinsRound)
    } else if (userChoice == "paper" && compChoice == "paper") {
      alert("draws:" + " " + ++draws + " " + draw)
    }
    //scissors Choice
    if (userChoice == "scissors" && compChoice == "paper") {
      alert("user score:" + " " + ++userscore + " " + userWinsRound)
    } else if (userChoice == "scissors" && compChoice == "rock") {
      alert("computer score:" + " " + ++compscore + " " + compWinsRound)
    } else if (userChoice == "scissors" && compChoice == "scissors") {
      alert("draws:" + " " + ++draws + " " + compWinsRound)
    } else {
      return "Something Went Wrong";
    }
        
    }
    
      while (userscore < 5 && compscore < 5) {
        console.log(playRound(userChoices(),compChoice()));
      }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?