我创建了一个非常基本的剪刀石头布射击游戏,您可以在其中使用 JavaScript 函数与计算机对战。
我有一个非常基本的 HTML 框架,其中有一个按钮。我试图在按下时启动提示(游戏)。
提示按我想要的方式工作,但一旦我打开窗口它就会启动脚本。
我试图仅在按下 html 上的按钮时启动脚本。
当我按下按钮时,控制台出现错误
getPlayerChoice 未在 HTMLButtonElement.onclick 中定义
这是我的代码:
let playerWinCount = 0;
let computerWinCount = 0;
let roundCount = 0;
function playRound() {
function computerPlay() {
let arr = ["Rock", "Paper", "Scissors"];
let compChoice = arr[Math.floor(Math.random() * arr.length)];
return compChoice;
}
//function getPlayerChoice() {
var str = prompt("What is your selection for this round?");
if (!str) {
return;
}
let newStr = str.toLowerCase();
let capStr = newStr[0].toUpperCase() + newStr.slice(1);
if (capStr !== "Rock" && capStr!== "Paper" && capStr !== "Scissors") {
return;
} else {
return capStr;
}
}
//
let playerSelection = getPlayerChoice();
let computerSelection = computerPlay();
if (playerSelection === computerSelection) {
alert("Try again... You both chose " + playerSelection + "!");
return;
} else {
if (playerSelection === "Rock") {
if (computerSelection === "Scissors") {
alert("You win! Rock beats Scissors!");
playerWinCount++;
roundCount++;
console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);
return;
} else if (computerSelection === "Paper") {
alert("You lose! Paper beats Rock!");
computerWinCount++;
roundCount++;
console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);
return;
}
} else if (playerSelection === "Paper") {
if (computerSelection === "Rock") {
alert("You win! Paper beats Rock!");
playerWinCount++;
roundCount++;
console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);
return;
}
我注释掉了我的提示功能,因为每当程序运行时,它都会启动无法关闭的提示。
如果没有输入,我需要一种关闭提示窗口的方法。
慕标琳琳
相关分类