在.js中

时间:2018-04-12 16:10:53

标签: javascript html math

我正在努力制作一个降低得分的Rock-Paper-Scissor游戏。

得分' 0'到达我的脚本应显示一个警告框。说you loseyou win

我如何在JavaScript中执行此操作?

我也不希望代码进一步显示负值。

var cpu_score = 5;
var me_score = 5;

function stone() {
  var choice = ["stone", "paper", "scissor"];
  var num = Math.floor(Math.random() * 3);
  var cpu_choice = choice[num];

  document.getElementById("p1").innerHTML = "CPU choosed: " + cpu_choice;
  document.getElementById("p2").innerHTML = "YOU choosed: stone";

  if (cpu_choice == "paper") {
    document.getElementById("p3").innerHTML = "YOU LOSE! OH PITY GOD!";
    cpu_score--;
  }
  if (cpu_choice == "stone") {
    document.getElementById("p3").innerHTML = "Close LUCK! It's DRAW!";
  }
  if (cpu_choice == "scissor") {
    document.getElementById("p3").innerHTML = "WON THE A.I! GREAT";
    me_score--;
  }

  document.getElementById("s1").innerHTML = cpu_score;
  document.getElementById("s2").innerHTML = me_score;
}


function paper() {
  var choice = ["stone", "paper", "scissor"];
  var num = Math.floor(Math.random() * 3);
  var cpu_choice = choice[num];

  document.getElementById("p1").innerHTML = "CPU choosed: " + cpu_choice;
  document.getElementById("p2").innerHTML = "YOU choosed: paper";

  if (cpu_choice == "scissor") {
    document.getElementById("p3").innerHTML = "YOU LOSE! OH PITY GOD!";
    cpu_score--;
  }
  if (cpu_choice == "paper") {
    document.getElementById("p3").innerHTML = "Close LUCK! It's DRAW!";
  }
  if (cpu_choice == "stone") {
    document.getElementById("p3").innerHTML = "WON THE A.I! GREAT";
    me_score--;
  }

  document.getElementById("s1").innerHTML = cpu_score;
  document.getElementById("s2").innerHTML = me_score;
}



function scissor() {
  var choice = ["stone", "paper", "scissor"];
  var num = Math.floor(Math.random() * 3);
  var cpu_choice = choice[num];

  document.getElementById("p1").innerHTML = "CPU choosed: " + cpu_choice;
  document.getElementById("p2").innerHTML = "YOU choosed: scissor";

  if (cpu_choice == "stone") {
    document.getElementById("p3").innerHTML = "YOU LOSE! OH PITY GOD!";
    cpu_score--;
  }
  if (cpu_choice == "scissor") {
    document.getElementById("p3").innerHTML = "Close LUCK! It's DRAW!";
  }
  if (cpu_choice == "paper") {
    document.getElementById("p3").innerHTML = "WON THE A.I! GREAT";
    me_score--;
  }

  document.getElementById("s1").innerHTML = cpu_score;
  document.getElementById("s2").innerHTML = me_score;
}

3 个答案:

答案 0 :(得分:0)

尝试类似:

if(me_score <= 0){alert("put here the text you want in the alert box")}

当你的分数达到0或以下时,它应该会给你警告框。

答案 1 :(得分:0)

您可以使用一些简单的if语句和alert()函数来实现此目的:

if (cpu_score <= 0) {
    alert('you win')
}
if (me_score <= 0) {
    alert('you lose')
}

这个逻辑可以放在一个名为checkGameOver()的函数中,每次完成一轮就可以调用它。

答案 2 :(得分:0)

您可以将your choice传递给函数并继续基于该参数进行测试,而不需要三个基本相同的不同函数。

var cpu_score = 5;
var me_score = 5;

function round(myChoice) {
  var choice = ["stone", "paper", "scissor"];
  var num = Math.floor(Math.random() * 3);
  var cpu_choice = choice[num];

  document.getElementById("p1").innerHTML = "CPU choosed: " + cpu_choice;
  document.getElementById("p2").innerHTML = "YOU choosed: " + myChoice;

  if (cpu_choice === myChoice) {
    document.getElementById("p3").innerHTML = "Close LUCK! It's DRAW!";
  } else if (cpu_choice === 'stone') {
    if (myChoice === 'paper') {
      document.getElementById("p3").innerHTML = "YOU LOSE! OH PITY GOD!";
      cpu_score--;
    } else {
      document.getElementById("p3").innerHTML = "WON THE A.I! GREAT";
      me_score--;
    }
  } else if (cpu_choice === 'paper') {
    if (myChoice === 'scissor') {
      document.getElementById("p3").innerHTML = "YOU LOSE! OH PITY GOD!";
      cpu_score--;
    } else {
      document.getElementById("p3").innerHTML = "WON THE A.I! GREAT";
      me_score--;
    }
  } else if (cpu_choice === 'scissor'){
    if (myChoice === 'stone') {
      document.getElementById("p3").innerHTML = "YOU LOSE! OH PITY GOD!";
      cpu_score--;
    } else {
      document.getElementById("p3").innerHTML = "WON THE A.I! GREAT";
      me_score--;
    }
  }

  document.getElementById("s1").innerHTML = cpu_score;
  document.getElementById("s2").innerHTML = me_score;

  if (cpu_score === 0) {
    alert('You Win');
  }
  if (me_score === 0) {
    alert('You Lose');
  }
}

如果任何人的得分达到0,最后会显示该消息。