我是菜鸟,所以提前感谢你的帮助......
当我测试我的“摇滚,纸张,剪刀”javascript游戏时,“玩家与计算机”的分数似乎遍布地图。最终得分总是导致玩家或计算机获胜5,但每场比赛的日志显示得分以非常随机的增量递增和递减。
我做错了什么?
////////////////////////////////////////////////
/* Provided Code - Please Don't Edit */
////////////////////////////////////////////////
'use strict';
function getInput() {
console.log("Please choose either 'rock', 'paper', or 'scissors'.");
return prompt("Please choose either 'rock', 'paper', or 'scissors'.");
}
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 */
////////////////////////////////////////////////
function getPlayerMove(move) {
// Write an expression that operates on a variable called `move`
// If a `move` has a value, your expression should evaluate to that value.
// However, if `move` is not specified / is null, your expression should equal `getInput()`.
return move = getInput();
}
function getComputerMove(move) {
// Write an expression that operates on a variable called `move`
// If a `move` has a value, your expression should evaluate to that value.
// However, if `move` is not specified / is null, your expression should equal `randomPlay()`.
return move = randomPlay();
}
function getWinner(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";
}
else if(playerMove == "rock" && computerMove == "scissors") {
winner = "player";
}
else if(playerMove == "rock" && computerMove == "paper") {
winner = "computer";
}
else if(playerMove == "paper" && computerMove == "rock") {
winner = "player";
}
else if(playerMove == "paper" && computerMove == "scissors") {
winner = "computer";
}
else if(playerMove == "scissors" && computerMove == "paper") {
winner = "player";
}
return winner;
}
function playToFive() {
console.log("Let's play Rock, Paper, Scissors");
var playerWins = 0;
var computerWins = 0;
while (playerWins <= 5 && computerWins <= 5) {
var playerTurnMove = getPlayerMove();
var computerTurnMove = getComputerMove();
var winnerRound = getWinner(playerTurnMove, computerTurnMove);
console.log("Player's move is " + playerTurnMove + " Computer's move is " + computerTurnMove);
console.log("The winner this round is " + winnerRound);
if (winnerRound == 'player') {
playerWins += 1;
console.log("The score is Player: " + playerWins + " to " + computerWins + " :Computer");
if (playerWins == 5) {
console.log("Congratulations! You win!");
break;
}
}
else if (winnerRound == 'computer') {
computerWins += 1;
console.log("The score is Player: " + playerWins + " to " + computerWins + " :Computer");
if (computerWins == 5) {
console.log("Oh No! The computer won!");
break;
}
}
else if (winnerRound == 'tie' ){
console.log("The score is Player: " + playerWins + " to " + computerWins + " :Computer");
}
}
console.log("The final score is: ");
return "Player: " + playerWins + ", Computer: " + computerWins;
}
playToFive();
答案 0 :(得分:1)
在getWinner()
下需要另外一个选项else if(playerMove == "scissors" && computerMove == "rock") {
winner = "computer";
}
答案 1 :(得分:0)
除了剪刀(P1)与摇滚(CPU)的getWinner函数中缺少逻辑分支之外,您不会输出分数的结果。你将它作为一个字符串返回。只需将该字符串附加到当前控制台输出中,或将其全部显示在警报或其他内容中。
console.log("The final score is... " +
"Player: " + playerWins + ", " +
"Computer: " + computerWins);
你的getWinner函数也可以只返回一个int而不是一个字符串。 1 =玩家获胜,0 =领带,-1 = CPU获胜。