gpt4 book ai didi

javascript - 总分添加错误的 javascript

转载 作者:行者123 更新时间:2023-11-27 23:10:54 24 4
gpt4 key购买 nike

我是菜鸟,所以提前感谢您的帮助......

当我测试我的“石头、剪刀、布”JavaScript 游戏时,“玩家与计算机”的分数似乎遍布 map 。最终得分总是导致玩家或计算机以 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();

Click here to see it mess up the scores!

预计到达时间:click to see picture for reference

最佳答案

在 getWinner() 下还需要一个选项

else if(playerMove == "scissors" && computerMove == "rock") { 
winner = "computer";
}

关于javascript - 总分添加错误的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36192336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com