我正在制作一个具有多个回合的石头剪刀布游戏,但是我在存储玩家的胜利次数时遇到了问题。我做错了什么?



`我正在制作一个多回合的石头剪刀游戏,但我在存储玩家的胜利次数时遇到了问题。我做错了什么?

// THIS FUNCTION DECIDES IF THE COMPUTER CHOOSES ROCK, PAPER OR SCISSOS 
function getComputerChoice () {
let rand = Math.floor(Math.random() * 10);
if (rand <= 3) {
return "Rock"
} else if (rand <= 6) {
return "Paper"
} else {
return "Scissors"
}}
// TESTING PLAYROUND FUNCTION 
function playRound(playerSelection, computerSelection) { 
const loseMessage = "You lose! Paper beats Rock";
const winMessage = "You win! Rock beats Scissors";
const drawMessage = "Draw. You and the computer chose Rock"
if (computerSelection === "Paper" && playerSelection === "Rock") {
alert (loseMessage);
return loseMessage 
} else if (computerSelection === "Rock" && playerSelection === "Rock") {
alert(drawMessage);
return drawMessage 
} else if (computerSelection === "Scissors" && playerSelection === "Rock") {
alert(winMessage);
return winMessage 
} else {
alert("Something went wrong")
}
}
let playerScore = 0;

function updatePlayerScore1() { 
let playRoundResults = playRound();
if (playRoundResults === "You win! Rock beats Scissors") {
playerScore += 1;
}
else {
playerScore += 0  
}
return playerScore;
}
playRound(prompt("Rock, Paper or Scissors?", "Rock"), getComputerChoice());
alert (updatePlayerScore1());

`

我原以为updatePlayerScore1函数会存储玩家获胜的次数并提醒它。

我认为您不会运行updatePlayerScore()。它应该在你的警报之外运行。相反,将玩家分数放入警报(playerScore)中

当u声明function playRound(playerSelection, computerSelection)时在这里,你将通过两个论点,但在

function updatePlayerScore1() { 
let playRoundResults = playRound();
***
}

你两个都没通过。你可以将你的代码更改为这个,然后尝试

function updatePlayerScore1() {
let playRoundResults = playRound('Rock', getComputerChoice());
***
}

我认为u应该使用var而不是let

使用let不会更新您的分数,它仍然会在updatePlayerScore1函数中更改,但不会在全局范围中更改

您应该在updatePlayerScore1函数中运行playRound功能,如

function updatePlayerScore1() { 
let playRoundResults = playRound(prompt("Rock, Paper or Scissors?", "Rock"), getComputerChoice());
if (playRoundResults === "You win! Rock beats Scissors") {
playerScore += 1;
}
else {
playerScore += 0  
}
return playerScore;

}

let playRoundResults = playRound();这一行产生了错误,函数playRound()需要两个参数,但您没有提供任何参数。

解决这一行,你就会得到答案。如果您找不到解决方案,请将您的代码与以下代码进行比较。

function updatePlayerScore1(message) { 
let playRoundResults = message;
if (playRoundResults === "You win! Rock beats Scissors") {
playerScore += 1;
}
else {
playerScore += 0  ;
}
return playerScore;
}
let message = playRound(prompt("Rock, Paper or Scissors?", "Rock"), getComputerChoice());
alert (updatePlayerScore1());

有多种平局、胜利和失败的组合。(例如,你可以用剪刀挑石头获胜,也可以用石头挑纸获胜)。为了让代码正常工作,您必须考虑所有这些可能性。(确切地说,总共9个)。我建议你创建玩家动态获得的信息,尤其是如果你继续创建具有更多可能结果的游戏,知道如何做到这一点。

我写的第一个函数就是这样做的。。。它采用2个选择作为自变量。根据这些,它可以判断这是一场平局、胜利还是失败。然后在适当的消息中插入选择(工作量要小得多,尤其是如果你添加了可能的结果)

对于您的更新PlayerScore,您只关心它是否是一场胜利。所以你CCD_ 4结果消息;你赢了"你松了";或";绘制";。然后你可以检查一下它是否写着";获胜";如果它真的给你的分数加1,如果不是什么都不做。

function results (yourPick, pcPick) {
let winningCombinations = {rock: "scissors", paper: "rock", scissors: "paper"}
if (yourPick === pcPick) return `Draw! bot of you picked ${yourPick}`
if (pcPick === winningCombinations[yourPick]) return `You win! ${yourPick} beats ${pcPick}`
return `You lose! ${yourPick} loses to ${pcPick}`
}
let playerScore = 0
function updatePlayerScore (result) {
let basicResult = result.split("!")[0]
if (basicResult === "You win") playerScore++
}

最新更新