我正在尝试在HTML的脚本标签中创建一个从头到五的Rock / Paper / Scissor游戏。据我所知,一切正常,但是当我运行它时,在控制台中我关闭的脚本标记处收到一条错误消息。
你们可以看看吗,让我知道:
1。)为什么我在结束脚本标签处收到错误消息,以及解决该问题并使代码按我希望的方式运行的我需要做什么?
2。)一般而言,您对我的代码有什么建议吗?我是自学成才,并试图尽可能多地吸收。
//Game parameters to start
var computerScore = 0
var playerScore = 0
//Game functions
function game() {
while (computerScore >= 5 && playerScore >= 5) {
//Computer Selection
function computerPlay() {
let playerSelection = prompt("Make your choice: rock, paper, or scissors!")
let computerSelection = Math.floor(Math.random() * 3)
//Player Selection
if (playerSelection.toLowerCase() == "rock") {
var playerChoice = 0;
} else if (playerSelection.toLowerCase() == "paper") {
var playerChoice = 1;
} else if (playerSelection.toLowerCase() == "scissors") {
var playerChoice = 2;
} else {
alert("Sorry, that word isn't recognized. Please select rock, paper, or scissors.")
}
//Rock
if (computerSelection == 0) {
var computerChoice = 0
//Paper
} else if (computerSelection == 1) {
var computerChoice = 1
//Scissors
} else if (computerSelection == 2) {
var computerChoice = 2
} else {}
//OUTCOMES
if (computerSelection == 0 && playerChoice == 0 || computerSelection == 1 && playerChoice == 1 || computerSelection == 2 && playerChoice == 2) {
alert("Tie game!")
} else if (computerSelection == 1 && playerChoice == 0) {
alert("Paper beats rock. You lose!")
} else if (computerSelection == 2 && playerChoice == 0) {
alert("Rock beats scissors. You win!")
} else if (computerSelection == 0 && playerChoice == 1) {
alert("Paper beats rock. You win!")
} else if (computerSelection == 0 && playerChoice == 2) {
alert("Rock beats scissors. You lose!")
} else if (computerSelection == 1 && playerChoice == 2) {
alert("Scissors beat paper. You win!")
} else if (computerSelection == 2 && playerChoice == 1) {
alert("Scissors beat paper. You lose!")
} else {}
//Increments
if (computerSelection > playerChoice) {
computerScore++
} else if (computerSelection < playerChoice) {
playerScore++
} else {}
if (computerScore == 5 && playerScore < 5) {
alert("Computer wins, " + computerScore + " to " + playerScore + "!")
} else if (playerScore == 5 && computerScore < 5) {
alert("Player wins, " + playerScore + " to " + computerScore + "!")
} else {}
}
game()
答案 0 :(得分:0)
所以这里有几件事,
首先,您的游戏功能没有右括号。
第二,您的游戏函数中的computerPlay函数永远不会被调用(它也没有右括号,但我还是建议将其删除)
第三,当computer / playerScore大于5时触发while条件(第12行),但在第7和8行上将它们设置为0。您可能希望它们小于或等于。
请参阅下面的固定版本。
<!DOCTYPE HTML >
<html>
<body>
<script>
//Game parameters to start
var computerScore = 0
var playerScore = 0
//Game functions
function game() {
while (computerScore <= 5 && playerScore <= 5) {
//Computer Selection
let playerSelection = prompt("Make your choice: rock, paper, or scissors!")
let computerSelection = Math.floor(Math.random() * 3)
//Player Selection
if (playerSelection.toLowerCase() == "rock") {
var playerChoice = 0;
} else if (playerSelection.toLowerCase() == "paper") {
var playerChoice = 1;
} else if (playerSelection.toLowerCase() == "scissors") {
var playerChoice = 2;
} else {
alert("Sorry, that word isn't recognized. Please select rock, paper, or scissors.")
}
//Rock
if (computerSelection == 0) {
var computerChoice = 0
//Paper
} else if (computerSelection == 1) {
var computerChoice = 1
//Scissors
} else if (computerSelection == 2) {
var computerChoice = 2
} else {}
//OUTCOMES
if (computerSelection == 0 && playerChoice == 0 || computerSelection == 1 && playerChoice == 1 || computerSelection == 2 && playerChoice == 2) {
alert("Tie game!")
} else if (computerSelection == 1 && playerChoice == 0) {
alert("Paper beats rock. You lose!")
} else if (computerSelection == 2 && playerChoice == 0) {
alert("Rock beats scissors. You win!")
} else if (computerSelection == 0 && playerChoice == 1) {
alert("Paper beats rock. You win!")
} else if (computerSelection == 0 && playerChoice == 2) {
alert("Rock beats scissors. You lose!")
} else if (computerSelection == 1 && playerChoice == 2) {
alert("Scissors beat paper. You win!")
} else if (computerSelection == 2 && playerChoice == 1) {
alert("Scissors beat paper. You lose!")
} else {}
//Increments
if (computerSelection > playerChoice) {
computerScore++
} else if (computerSelection < playerChoice) {
playerScore++
} else {}
if (computerScore == 5 && playerScore < 5) {
alert("Computer wins, " + computerScore + " to " + playerScore + "!")
} else if (playerScore == 5 && computerScore < 5) {
alert("Player wins, " + playerScore + " to " + computerScore + "!")
} else {}
}
}
game();
</script>
</body>
</html>