在JavaScript的岩石纸剪刀领带盒

时间:2014-06-01 15:34:08

标签: javascript

我目前正在学习CodeAcademy JS课程,他们告诉我尝试实施重试 如果用户的选择和计算机的选择是相同的。 首先,请允许我为我的不良缩进道歉,因为我仍在学习如何正确识别我的代码。 看一下if(choice1 === choice2)。根据它,我已经写了我执行重试的方式,但它失败了。如果有一个关系,我会很高兴收到关于如何创建重试案例的帮助选择? 提前谢谢!

var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
    confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
    userChoice = prompt("Do you choose rock, paper or scissors?");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
console.log("You Chose:  "+userChoice);
console.log("The Computer Chose:  "+computerChoice);
var compare = function (choice1,choice2){
    if (choice1 === choice2) {
        userChoice = prompt("Choose again");
        computerChoice = Math.random();
        if (computerChoice < 0.34) {
            computerChoice = "rock";
        } else if(computerChoice <= 0.67) {
            computerChoice = "paper";
        } else {
            computerChoice = "scissors";
        }
        compare(userChoice,computerChoice);
    }

    if (choice1 === "rock"){
        if  (choice2 === "paper"){
            return "Paper wins";
        }
        if (choice2 === "scissors") {
            return "Rock wins";
        }
    }

    if (choice1 === "paper"){
        if (choice2 === "scissors"){
            return "Scissors wins!";
        }
        if (choice2 === "rock"){
            return "Paper wins!";
        }
    }

    if (choice1 === "scissors"){
        if (choice2 === "rock"){
            return "Rock wins!";
        }
        if (choice2 === "paper"){
            return "Scissors wins!";
        }
    }
};
compare(userChoice,computerChoice);

2 个答案:

答案 0 :(得分:3)

你走了:

function rockpaperscissors() {
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
    confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
    userChoice = prompt("Do you choose rock, paper or scissors?");
  }
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  }
  else if(computerChoice <= 0.67) {
    computerChoice = "paper";
  }
  else {
    computerChoice = "scissors";
  }
  console.log("You Chose:  "+userChoice);
  console.log("The Computer Chose:  "+computerChoice);
  var choice1 = userChoice;
  var choice2 = computerChoice;
  if (choice1 === choice2){
    confirm('Sorry, but there was a tie. You and the computer are equals. Let\'s try again anyways.');
    return rockpaperscissors();
  }

  if (choice1 === "rock"){
    if (choice2 === "paper"){
      return "Paper wins";
    }
    if (choice2 === "scissors") {
      return "Rock wins";
    }
  }
  if (choice1 === "paper"){
    if (choice2 === "scissors"){
      return "Scissors wins!";
    }
    if (choice2 === "rock"){
      return "Paper wins!";
    }
  }
  if (choice1 === "scissors"){
    if (choice2 === "rock"){
      return "Rock wins!";
    }
    if (choice2 === "paper"){
      return "Scissors wins!";
    }
  }
}
confirm(rockpaperscissors());

现在,解释一下我做了什么:

  • 现在一切都在rockpaperscissors函数中。这样您就可以从内部调用它而无需重复代码。
  • 用两个空格重新缩进所有内容。说真的,缩进很重要。
  • 如果有平局,那么你会告诉你有关系,然后重新启动。
  • 一旦达到了无关系,它将confirm结果。

另外,因为我觉得它是一个非常精致的版本,它使用了许多复杂的JS:

confirm

答案 1 :(得分:1)

我不太清楚您的程序是如何运行的,但您可以做的是将代码分解为不同的功能。例如,有一个函数只是为了处理移动,另一个是处理游戏逻辑(得分,寻找犯规等等)。

如果你有一个可以使计算机移动的功能,你只需使用相同的逻辑来说if (choice === computerMove)但是,我不会将computerMove与用户输入的任何内容进行比较,因为你永远不会知道如果用户会拼错摇滚或什么的。你可以让这个'游戏逻辑'函数返回一个可以判断是否有胜利者的值,如果两个玩家做出同样的动作(犯规),或者转向它。

如果你真的在为代码片段而烦恼,请告诉我,我会创建一个代码片段。