我需要添加什么,所以我的JavaScript可以按照我想要的方式工作?

时间:2016-08-05 12:51:35

标签: javascript browser alert

我开始学习 JavaScript ,这是我构建的第一款游戏:Rock,Paper,Scissors。 (播放器与计算机)现在,一切正常,但我无法弄清楚如何 这个

当结果为平局时,游戏再次开始询问我是否想再次播放,而无需刷新浏览器。

不要用jQuery回答,我正在努力学习JavaScript! :)

这是我的代码:

// declare variables
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

// prevent the user from choosing a different answer
if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
  userChoice = prompt("Your answer is not acceptable! Please choose again!");
}

// pick a random answer from computer
if (computerChoice < 0.34) {
  computerChoice = "rock";
} else if (computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
}
alert("Computer: " + computerChoice);

// check who wins
var compare = function(choice1, choice2) {
  if (choice1 === choice2) {
    alert("The result is a tie!");
  } else if (choice1 === "rock") {
    if (choice2 === "scissors") {
      alert("rock wins");
    } else {
      alert("paper wins");
    }
  } else if (choice1 === "paper") {
    if (choice2 === "rock") {
      alert("paper wins");
    } else {
      alert("scissors win");
    }
  }
}

// call the function
compare(userChoice, computerChoice);

2 个答案:

答案 0 :(得分:3)

你基本上要做的是将游戏包装在一个更大的上下文循环中,它本身只是检查游戏的终止条件。

在结构上,让我们假设您的整个游戏都包含在一个函数中:

function playGame() {
    // all the code you have now
}

然后循环将具有这样的结构:

while (true) {
    playGame();

    if (!confirm("Would you like to play again?")) {
        break;
    }
}

当然,您可以通过确认获得更多信息,或者使用值来控制循环而不是使用break的无限循环。但这个概念是一样的。整个循环将继续重复并再次播放&#34;直到某些条件存在才会导致它停止。

所以在你的情况下,你想检查它是否是一个平局,是吗?结构上是这样的:

while (true) {
    playGame();

    if (!gameIsTie()) {
        break;
    }
}

或:

var winner = 0;

while (winner == 0) {
    playGame();
    winner = determineWinner();
}

其中你将实施确定游戏是否平局所需的功能,或者是否有胜利者等。

答案 1 :(得分:1)

如果是平局,你可以把所有功能都放在功能和呼叫中。

&#13;
&#13;
(function game(){
// declare variables
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

// prevent the user from choosing a different answer
if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
  userChoice = prompt("Your answer is not acceptable! Please choose again!");
}

// pick a random answer from computer
if (computerChoice < 0.34) {
  computerChoice = "rock";
} else if (computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
}
alert("Computer: " + computerChoice);

// check who wins
var compare = function(choice1, choice2) {
  if (choice1 === choice2) {
    alert("The result is a tie!");
    var newGame = prompt("Play again?");
    if(newGame === 'yes'){
      game();
    }
  } else if (choice1 === "rock") {
    if (choice2 === "scissors") {
      alert("rock wins");
    } else {
      alert("paper wins");
    }
  } else if (choice1 === "paper") {
    if (choice2 === "rock") {
      alert("paper wins");
    } else {
      alert("scissors win");
    }
  }
}

// call the function
compare(userChoice, computerChoice);
})()
&#13;
&#13;
&#13;