更多的石头剪刀

时间:2015-04-07 21:49:30

标签: javascript

我刚刚开始,我在构建RPS计划时遇到了问题。我应该遵循隐藏评论中提供的指示,我只是不明白我应该用2个函数getPlayerMove(move)和getComputerMove(move)做什么。

如果加载到index.html并通过浏览器中的控制台播放,我能够让程序正常工作但是它没有其他工作(例如,使用repl http://repl.it/hUa/8

// ////////////////////////////////////////////////
/*   Provided Code - Please Don't Edit   */
////////////////////////////////////////////////
'use strict';

playToFive();

function getInput() {
    console.log("Please choose either 'rock', 'paper', or 'scissors'.");
    return prompt();
}

function randomPlay() {
    var randomNumber = Math.random();
    if (randomNumber < 0.33) {
        return "rock";
    } else if (randomNumber < 0.66) {
        return "paper";
    } else {
        return "scissors";
    }
}
////////////////////////////////////////////////
/*           Write Your Code Below            */
////////////////////////////////////////////////

var playerMove;
var computerMove;

function getPlayerMove(move) {
    // Write an expression that operates on a variable called `move`
    var move = getInput();
    if (move === null) {
        getInput();
    } else {
        console.log("player chooses " + move);
        playerMove = move;
        getComputerMove();
    } 

}

function getComputerMove(move) {
    var move = randomPlay();
    if (move === null || 0) {
        randomPlay();
    } else {
        console.log("computer chooses " + move);
        computerMove = move;
        getWinner(playerMove, computerMove);
    }
}

var winner;
var getWinner = function(playerMove, computerMove) {
    //var winner;
    // Write code that will set winner to either 'player', 'computer', or 'tie' based on the values of playerMove and computerMove.
    // Assume that the only values playerMove and computerMove can have are 'rock', 'paper', and 'scissors'.
    // The rules of the game are that 'rock' beats 'scissors', 'scissors' beats 'paper', and 'paper' beats 'rock'.
    /* YOUR CODE HERE */
    if (playerMove === computerMove) {
        winner = "tie";
        console.log("Looks like we have a tie!");
        return winner;
    } 
    else if (playerMove === "rock") {
        if (computerMove === "paper") {
            winner = "computer";
        } else {
            winner = "player";
        }
    } else if (playerMove === "paper") {
        if (computerMove === "rock") {
            winner = "player";
        } else {
            winner = "computer";
        }
    } else if (playerMove === "scissors") {
        if (computerMove === "rock") {
            winner = "computer";
        } else {
            winner = "player";
        }
    } else {
        getInput();
    }
    console.log("Looks like the " + winner + " wins!");
    return winner;
}

function playToFive() {
    console.log("Let's play Rock, Paper, Scissors");
    var playerWins = 0;
    var computerWins = 0;
    for (var i=0; i<25; i++) {
        if (playerWins === 5 || computerWins === 5) {
            console.log("FINAL SCORE: Player Wins : " + playerWins + "  Computer Wins : " + computerWins);
            break;
        } 
        else 
        {
            if (winner === "player") 
            {
             playerWins ++;
             console.log("Player Wins : " + playerWins + "  Computer Wins : " + computerWins);
            } 
            else if (winner === "computer") 
            {
             computerWins ++;
             console.log("Player Wins : " + playerWins + "  Computer Wins : " + computerWins);
            }
        getPlayerMove();    

        }
    }
}

3 个答案:

答案 0 :(得分:0)

你在哪里定义getWinner()函数

var getWinner = function(playerMove, computerMove) { ... }

这将在浏览器中起作用,因为变量的默认范围是窗口对象。因此,在定义的上下文中声明的变量将分配给窗口对象。

当在这样的终端中运行时,我不确定默认范围是什么(或者是否有一个),因此在声明后该函数可能不存在。

对我有用的解决方案是将上述行更改为

function getWinner(playerMove, computerMove) { ... }

希望这适合你。我会尝试找到更多信息,并在我对发生的事情有更具体的解释时进行更新。

修改 阅读更多关于函数声明和函数表达式之间的区别 Declaration vs Expression

答案 1 :(得分:0)

您将在代码中设置变量getWinner,而不是在调用时设置变量{{1}}。在调用之前尝试移动它的声明。

答案 2 :(得分:0)

我已经在这里工作了,但现在只需要提示即可对这部分进行操作。

玩家的行动现在是硬编码的 - 你现在需要做的就是通过提示功能得到这个并且一切正常。

// ////////////////////////////////////////////////
/*   Provided Code - Please Don't Edit   */
/*   IT WORKS!!!          ///////////// */
//////////////////////////////////////////////////
'use strict';

rockPaperScissors();


function randomPlay() {
    var randomNumber = Math.random();
    if (randomNumber < 0.33) {
        return "rock";
    } else if (randomNumber < 0.66) {
        return "paper";
    } else {
        return "scissors";
    }
}
////////////////////////////////////////////////
/*           Write Your Code Below            */
////////////////////////////////////////////////

var playerMove;
var computerMove;
var winner;

function getComputerMove() {
    var move = randomPlay();
    if (move === null || 0) {
        randomPlay();
    } else {
        console.log("The computer chose " + move);
        computerMove = move;
    }
}

function getPlayerMove() {
    // Write an expression that operates on a variable called `move`
    //////////////////// The PLAYER move is hard coded in the line below this one///////////////////////////
    var move = 'scissors';
        console.log("The player chose " + move);
        playerMove = move;
}



function rockPaperScissors() {
    console.log("Let's play Rock, Paper, Scissors.");
    getComputerMove();
    getPlayerMove();   
    getWinner();
}

function getWinner() {
    var winner;
  if ((computerMove === 'rock') && (playerMove === 'rock')) {
     winner = "It's a tie!";
 } else if ((computerMove === 'scissors') && (playerMove === 'rock')) {
    winner = "Player. Congratulations you Win!!!";
} else if ((computerMove === 'paper') && (playerMove === 'rock')) {
     winner = "The computer. You lose bitch!";
  } else if ((computerMove === 'rock') && (playerMove === 'scissors')) {
     winner = "The computer. You lose bitch!";
 } else if ((computerMove === 'scissors') && (playerMove === 'scissors')) {
    winner = "It's a tie!";
} else if ((computerMove === 'paper') && (playerMove === 'scissors')) {
     winner = "The player. You win!";
  }else if ((computerMove === 'rock') && (playerMove === 'paper')) {
     winner = "The player. You win!";
 } else if ((computerMove === 'scissors') && (playerMove === 'paper')) {
    winner = "The computer. You lose bitch!";
} else if ((computerMove === 'paper') && (playerMove === 'paper')) {
     winner = "It's a tie!";
  }
console.log("The winner is: " + winner);
}

https://repl.it/BvcS/7