控制台版石头剪刀布游戏出现'game is not defined'错误的解决求助
控制台版石头剪刀布游戏出现'game is not defined'错误的解决求助
各位大佬好,我正在做一个控制台版的石头剪刀布游戏,但每次测试的时候都遇到一个错误:
index.html:14 Uncaught ReferenceError: game is not defined
at HTMLButtonElement.onclick (index.html:14:32)
我已经尝试过一些修复了——一开始我没定义choices数组,后来我在调用computerChoice函数前声明了这个数组并赋值了'rock','paper','scissors',但问题还是没解决。
我的JavaScript代码(main.js)
//this will play the game //play five rounds //console based let choices = ["rock", "paper", "scissors"]; let winners = []; function game() { for (let i = 1; i <= 5; i++) { playRound(i); } document.querySelector("button").textContent = "Play New Game"; logWins(); } function playRound(round) { const playerSelection = playerChoice(); const computerSelection = computerChoice(); const winner = checkWinner(playerSelection, computerSelection); winners.push(winner); logRound(playerSelection, computerSelection, winner, round); } function playerChoice() { //get input from the player let input = prompt("Type Rock, Paper, or Scissors"); while (input == null) { input = prompt("Type Rock, Paper or Scissors"); } } // this allows no input to show up if misspelled // a word, or wrote anything in the input prompt input = input.toLowerCase(); let check = validateInput(input); console.log(input); return input; //get random input from the comp function computerChoice() { return choices[Math.floor(Math.random() * choices.length)]; } function validateInput(choice) { if (choices.includes(choice)) { return true; } else { return false; } } function checkWinner(choiceP, choiceC) { if (choiceP === choiceC) { return "Tie"; } else if ( (choiceP === "rock" && choiceC === "scissors") || (choiceP === "paper" && choiceC === "rock") || (choiceP === "scissors" && choiceC === "paper") ) { return "Player"; } else { return "Computer"; } } function logWins() { let playerWins = winners.filter((item) => item == "Player").length; let computerWins = winners.filter((item) => item == "Computer").length; let ties = winners.filter((items) => item == "Ties").length; console.log("Results:"); console.log("Player Wins", playerWins); console.log("Computer Wins", computerWins); console.log("Tie", ties); } function logRound(playerChoice, computerChoice, winner, round) { console.log("Round:", round); console.log("Player Chose:", playerChoice); console.log("Computer Chose:", computerChoice); console.log(winner, "Won the Round!"); console.log("--------------------------"); } game();
我的HTML代码(index.html)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <link rel="stylesheet" href="styles.css" /> </head> <body> <div class="flex-container"> <h1>Rock Paper Scissors</h1> <button onclick="game()">Play New Game</button> </div> <p> Hi! This game is played within the console. <br /> Instructions to play this game on <br /> Windows/Linux <br /> press <strong>CTRL</strong> <em>+</em> <strong>SHIFT</strong> <em>+</em> <strong>J</strong> <br /> On Mac <br /> press <strong>Option</strong> <em>+</em> <strong>J</strong> <br /> This will open the console so you can start playing! </p> <script src="main.js"></script> </body> </html>
我注意到错误提示指向index.html,也查了相关资料但没找到解决办法。另外我也在考虑要不要用Python来做这个游戏,可能会更容易成功?有没有大佬能帮我看看这个JavaScript版本的问题出在哪呀?
备注:内容来源于stack exchange,提问作者user20759768
相关产品推荐
相关产品推荐

