代码之家  ›  专栏  ›  技术社区  ›  JackJack

回复:javascript石头,纸,剪刀作业。我错过了什么?

  •  -1
  • JackJack  · 技术社区  · 8 年前

    作为我的课堂作业之一,我负责修复Real.IT网站上的损坏代码。 repl.it rock, paper scissors . 虽然这个话题已经提了很多次,但我找不到任何代码与此类似的线程。他们都很不一样。

    我知道主要问题是:

    1. 提示说明即使用户获胜,计算机也总是获胜。我添加了用户输入逻辑,以便返回用户获胜的结果。

    2. 游戏忽略“摇滚”作为用户输入。我添加了一个toLowerCase()方法来删除大写字符。

    3. 计算机总是忽略选择“岩石”。我将Math.ceil改为Math.floor,这样函数randomFrom还包括computerChoices数组的0索引。

    4. 正确地读取和存储用户输入(除了创建输入变量之外,我不知道如何使其工作)。我把这个记录下来了,但到目前为止还没有运气。

    这是原始代码:

    let computerChoices = ["rock", "paper", "scissors"];
    
    function randomFrom(array) {
    return array[(Math.ceil(Math.random()*3)) ];
    console.log(array);
    }
    
    
    function checkInput(input, computerChoices) {
      if (input == "quit") {
        return true;
      }
      
      let computerChoice = randomFrom(computerChoices);
      
    if(computerChoice === "rock" && input === "scissors"){
    alert("Computer wins!");
    return true;
    } else if (computerChoice === "scissors" && input === "paper"){
    alert("Computer wins!");
    return true;
    } else if (computerChoice === "paper" && input === "rock"){
    alert("Computer wins!");
    return true;
    }
    
    alert("Computer wins!");``
    return false
    }
    
    function start(gameOver, computerChoices) {
      while (!gameOver){
        let playerInput = '';
        prompt("Hi! Enter rock/paper/scissors to play, or quit to stop playing.");
    gameOver = checkInput(playerInput, computerChoices);
    playerInput = "paper";
    
    } 
    }
    
    let gameOver = false;
    
    start(gameOver, computerChoices)  

    我也在代码中添加了注释。我不太明白这里发生的一切。我知道这也是一个范围问题,但我只是没有看到它。我没有要求任何人帮我解决这个问题,但至少让我知道我是朝着正确的方向前进,还是我使代码比开始时更糟。如果你真的解决了它,请添加更多的评论。谢谢您!

    //Is gameOver defined correctly? I'm not sure. 
    let gameOver = false;
    let computerChoices = ["rock", "paper", "scissors"];
    let computerChoice = randomFrom(computerChoices);
    console.log(computerChoice);
    
    // //change Math.ceil to Math.floor in order to include the [0] index
    function randomFrom(array) {
    return array[(Math.floor(Math.random()*3))];
    }
    
    //Changed computerChoices to computerChoice
    function checkInput(input, computerChoice) {
      if (input === "quit") {
        return true;
      }
    
    //Changed computerChoices to computerChoice
    function startGame(gameOver, computerChoice) {
      while (!gameOver){
        let playerInput = '';
        //Added input in order to change user input to lowercase characters
        let input = playerInput.toLowerCase();
        prompt("Hi! Enter rock/paper/scissors to play, or quit to stop playing.");
    } 
      
    if(computerChoice === "rock" && input === "scissors"){
    alert("Computer wins!");
    return true;
    } else if (computerChoice === "scissors" && input === "paper"){
    alert("Computer wins!");
    return true;
    } else if (computerChoice === "paper" && input === "rock"){
    alert("Computer wins!");
    return true;
    //Added user outcomes since program results in the computer always winning
    } else if(computerChoice === "rock" && input === "paper"){
    alert("Player wins!");
    return true;
    } else if (computerChoice === "scissors" && input === "rock"){
    alert("Player wins!");
    return true;
    } else if (computerChoice === "paper" && input === "scissors"){
    alert("Computer wins!");
    return true;
    } else if (computerChoice === input) {
    alert("we have a draw");
    } else {
    console.log("error");
    }
    
    }
    //I'm calling the startGame() function in order to get it to run
    return startGame();
    }
    1 回复  |  直到 8 年前
        1
  •  0
  •   CodeF0x    8 年前

    这里有些东西:

    • 在另一个函数中定义的某些函数,因此不能调用它们
    • 我删除了所有那些不必要的参数和返回语句
    • 我定义了 input startGame() 使其在内部可访问的函数 checkInput() --通过 输入 作为一个参数也可以
    • 我还移除了无效和不必要的while循环 开始时间()
    • 开始时间() 在里面 检查输入() 所以没人叫它,所以比赛就没开始

    //Is gameOver defined correctly? I'm not sure. 
    let gameOver = false;
    let input;
    let computerChoices = ["rock", "paper", "scissors"];
    let computerChoice = randomFrom(computerChoices);
    console.log(computerChoice);
    
    // //change Math.ceil to Math.floor in order to include the [0] index
    function randomFrom(array) {
      return array[(Math.floor(Math.random() * 3))];
    }
    
    //Changed computerChoices to computerChoice
    function startGame() {
      //Added input in order to change user input to lowercase characters
      input = prompt("Hi! Enter rock/paper/scissors to play, or quit to stop playing.").toLowerCase();
      checkInput();
    }
    
    //Changed computerChoices to computerChoice
    function checkInput() {
      if (input === "quit") {
        return;
      }
    
    
      if (computerChoice === "rock" && input === "scissors") {
        alert("Computer wins!");
      } else if (computerChoice === "scissors" && input === "paper") {
        alert("Computer wins!");
      } else if (computerChoice === "paper" && input === "rock") {
        alert("Computer wins!");
        //Added user outcomes since program results in the computer always winning
      } else if (computerChoice === "rock" && input === "paper") {
        alert("Player wins!");
      } else if (computerChoice === "scissors" && input === "rock") {
        alert("Player wins!");
      } else if (computerChoice === "paper" && input === "scissors") {
        alert("Computer wins!");
      } else if (computerChoice === input) {
        alert("we have a draw");
      } else {
        console.log("error");
      }
    
    }
    //I'm calling the startGame() function in order to get it to run
    startGame();

    记住,如果你想比赛持续的时间超过一轮,你需要一个循环。

    其次,这里是一个工作示例,因此决定显示一些错误,我在测试堆栈片段以外的任何地方都没有遇到这些错误: http://jsfiddle.net/zvao25ny/1/