vca*_*ble 1 javascript function
我已经尝试了一段时间,但没有骰子。我有一个叫做的函数gameScore(),该函数应该能够跟踪玩家和计算机的得分以及平局。我将分数定义为全局变量,因此从函数内部更新分数应该没有问题。
我的问题是,即使我在控制台上看到输出信息,说谁将赢得比赛,但分数不会更新,并且游戏会无限期进行。非常感谢您的帮助,下面是我的代码。
let playerScore = 0;
let computerScore = 0;
let draws = 0;
//Computer choice
function computerPlay() {
let random = Math.random();
if (random <= 0.3333) {
return "paper";
} else if (random >= 0.6666) {
return "rock";
} else {
return "scissors";
}
}
//Plays one round of RPS
function playRound(playerChoice, computerSelection) {
if (playerChoice === computerSelection) {
return draw;
} else if (playerChoice === "rock" && computerSelection === "scissors") {
return playerWinRound;
} else if (playerChoice === "paper" && computerSelection === "rock") {
return playerWinRound;
} else if (playerChoice === "scissors" && computerSelection === "paper") {
return playerWinRound;
} else {
return computerWinRound;
}
}
//Specifies round win/game win messages
let playerWinRound = "Player wins this round!"
let computerWinRound = "Computer wins this round!"
let draw = "Draw!"
let playerWin = "Player wins the game! Congratulations!"
let computerWin = "Computer wins the game! Congratulations!"
//For loop that plays multiple rounds
for (let i = 0; i < 1000; i++) {
let playerChoice = prompt("Rock, paper, or scissors?").toLowerCase();
const computerSelection = computerPlay();
let roundResult = playRound(playerChoice, computerSelection);
console.log(roundResult);
gameScore(roundResult);
console.log("Your score is " + playerScore);
console.log("The computer's score is " + computerScore);
if (playerScore === 5 || computerScore === 5) {
break;
}
}
//Keeps score and prints out correct messages based on score
function gameScore() {
let result = playRound()
if (result === playerWinRound) {
playerScore++;
} else if (result === draw) {
draws++;
} else {
computerScore++;
}
if (playerScore === 5) {
console.log(playerWin);
return;
}
if (computerScore === 5) {
console.log(computerWin);
return;
}
}Run Code Online (Sandbox Code Playgroud)
你已经完成的该轮的结果,你把它传递给gameScore同gameScore(roundResult);,然而,你不实际使用这样的说法,但你创造另一番:
function gameScore() {
let result = playRound()
Run Code Online (Sandbox Code Playgroud)
相反,只需将结果传递给:
function gameScore(result) {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47 次 |
| 最近记录: |