记分牌不会更新 -- 使用 JavaScript 的 RPS 游戏

scoreboard will not update -- RPS game using javascript

本文关键字:JavaScript RPS 游戏 使用 更新 记分牌      更新时间:2023-09-26

我是Javascript的新手,我正在创建一个RPS游戏。出于某种原因,它只是在我播放剪刀按钮时记录点。我很确定我正在使用不应该关闭的东西,但我找不到在哪里。任何帮助将不胜感激。

这是我的javascript代码:

var bot = 0;
var you = 0; 
document.getElementById('rock').onclick = playRock;

function playRock() {
   var humanChoice = 'rock';
   var compChoice = cChoice();
   console.log(compChoice);
   compare(humanChoice, computerChoice);
   };

 function compare(humanChoice2, compChoice2) {
   if (humanChoice2 == compChoice2) {
    console.log("tie")
   } else if (humanChoice2 == "rock"){
    if (compChoice2 == 'scissors') {
        you = you + 1;
        document.getElementById("humanScore").innerHTML = you;
    }
    else {
        bot = bot + 1;
        document.getElementById("computerScore").innerHTML = bot;
    }
}
}
 function cChoice() {
computerChoice = Math.random();
    if (computerChoice <= .33) {
        computerChoice = 'rock';
    } else if (computerChoice <= .66) {
        computerChoice = 'paper';
    } else {
        computerChoice = 'scissors';
    }
    return computerChoice;
}
 document.getElementById('paper').onclick = playPaper;
  function playPaper() {
  var humanChoice = 'paper';
  var compChoice = cChoice();
  console.log(compChoice);
  compare(humanChoice, computerChoice);
};
 function compare(humanChoice2, compChoice2) {
  if (humanChoice2 == compChoice2) {
    console.log("tie")
  } else if (humanChoice2 == "paper"){
    if (compChoice2 == 'rock') {
        you = you + 1;
        document.getElementById("humanScore").innerHTML = you;
    }
    else {
        bot = bot + 1;
        document.getElementById("computerScore").innerHTML = bot;
    }
}
}
 function cChoice() {
computerChoice = Math.random();
    if (computerChoice <= .33) {
        computerChoice = 'rock';
    } else if (computerChoice <= .66) {
        computerChoice = 'paper';
    } else {
        computerChoice = 'scissors';
    }
    return computerChoice;
}
document.getElementById('scissors').onclick = playScissors;
function playScissors() {
var humanChoice = 'scissors';
var compChoice = cChoice();
console.log(compChoice);
compare(humanChoice, computerChoice);
};
function compare(humanChoice2, compChoice2) {
if (humanChoice2 == compChoice2) {
    console.log('tie')
} else if (humanChoice2 == 'scissors'){
    if (compChoice2 == 'paper') {
        you = you + 1;
        document.getElementById('humanScore').innerHTML = you;
    }
    else {
        bot = bot + 1;
        document.getElementById('computerScore').innerHTML = bot;
    }
}
}
function cChoice() {
  computerChoice = Math.random();
    if (computerChoice <= .33) {
        computerChoice = 'rock';
    } else if (computerChoice <= .66) {
        computerChoice = 'paper';
    } else {
        computerChoice = 'scissors';
    }
    return computerChoice;
}

如何拥有三个具有相同签名的函数?compare(humanChoice2, compChoice2)

仅使用最后一个定义。尝试以不同的方式命名每个函数。

function a()
{
 document.getElementById("demo").innerHTML = "1";
}
function a()
{
 document.getElementById("demo").innerHTML = "2";
}
function a()
{
 document.getElementById("demo").innerHTML = "3";
}
a();

这会将演示的 innerHTML 设置为 3