我在这里遇到一个问题,我正在尝试创建这个石头、剪刀、布游戏,但在供用户选择的按钮选择方面遇到问题。我没有运气获得更新playerTurn.currentPick属性的代码来更新玩家在按钮单击时选择的内容。我创建了一个名为 choiceBtn 的函数,它应该用用户选择的内容更新属性。正如你所知,我是编码新手,但任何帮助将不胜感激。下面是我的 JS 代码,如果有帮助的话我也会附上 HTML 代码。
let playerTurn = {
currentPick: null
};
let cpuTurn = {
currentPick: null
};
const playerChoice = ["Lapis", "Papyrus", "Scalpellus"];
playerTurn.currentPick = choiceBtn();
function computerChoice() {
const randomChoice = Math.floor(Math.random() * playerChoice.length);
cpuTurn.currentPick = playerChoice[randomChoice];
return cpuTurn.currentPick;
}
console.log(computerChoice());
function compareChoices(){
computerChoice();
if(playerTurn.currentPick === cpuTurn.currentPick){
resultScreen("It's a tie. Both the player and cpu chose " + playerTurn.currentPick);
} else if(playerTurn.currentPick === playerChoice[0]){
} if(cpuTurn.currentPick === playerChoice[2]){
resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
} else if(playerTurn.currentPick === playerChoice[1]){
if(cpuTurn.currentPick === playerChoice[0]){
resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
} else if(playerTurn.currentPick === playerChoice[2]){
if(cpuTurn.currentPick === playerChoice[1]){
resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
} else if(cpuTurn.currentPick === playerChoice[0]){
if(playerTurn.currentPick === playerChoice[2]){
resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
}
else if(cpuTurn.currentPick === playerChoice[1]){
if(playerTurn.currentPick === playerChoice[0]){
resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
}
else if(cpuTurn.currentPick === playerChoice[2]){
if(playerTurn.currentPick === playerChoice[1]){
resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
}
}
function resultScreen(display){
const displayText = document.createElement('p');
displayText.innerText = display;
document.body.appendChild(displayText);
}
function choiceBtn(){
var lapisBtn = document.getElementById("lapis").onclick = function(){
playerTurn.currentPick = playerChoice[0];
}
var papyrusBtn = document.getElementById("papyrus").onclick = function(){
papyrusBtn = playerChoice[1];
}
var scalpellusBtn = document.getElementById("scalpellus").onclick = function(){
scalpellusBtn = playerChoice[2];
}
}
compareChoices();
<!--HTML CODE-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lapis, Papyrus, Scalpelus</title>
</head>
<body>
Welcome to the game of Lapis, Papyrus, and Scalpelus!
<h2>Make your Choice Below</h2>
<button id="lapis">Lapis</button>
<button id="papyrus">Papyrus</button>
<button id="scalpellus">Scalpellus</button>
</body>
</html>
最佳答案
你走在正确的道路上。定义点击事件时存在几个问题。我从它们所在的函数中删除了它们,以便可以在运行开始时定义它们。将玩家选择移至当前选择变量也存在问题。你的布和剪刀正在被移动到变量名称。
该片段显示 onClick 事件正在运行,但在检查游戏结果方面似乎确实需要进一步的工作。谁赢并不总是正确的
let playerTurn = {
currentPick: null
};
let cpuTurn = {
currentPick: null
};
const playerChoice = ["Lapis", "Papyrus", "Scalpellus"];
// playerTurn.currentPick = choiceBtn();
function computerChoice() {
const randomChoice = Math.floor(Math.random() * playerChoice.length);
cpuTurn.currentPick = playerChoice[randomChoice];
return cpuTurn.currentPick;
}
console.log(computerChoice());
function compareChoices() {
computerChoice();
if (playerTurn.currentPick === cpuTurn.currentPick) {
resultScreen("It's a tie. Both the player and cpu chose " + playerTurn.currentPick);
} else if (playerTurn.currentPick === playerChoice[0]) {}
if (cpuTurn.currentPick === playerChoice[2]) {
resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
} else if (playerTurn.currentPick === playerChoice[1]) {
if (cpuTurn.currentPick === playerChoice[0]) {
resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
} else if (playerTurn.currentPick === playerChoice[2]) {
if (cpuTurn.currentPick === playerChoice[1]) {
resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
} else if (cpuTurn.currentPick === playerChoice[0]) {
if (playerTurn.currentPick === playerChoice[2]) {
resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
} else if (cpuTurn.currentPick === playerChoice[1]) {
if (playerTurn.currentPick === playerChoice[0]) {
resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
} else if (cpuTurn.currentPick === playerChoice[2]) {
if (playerTurn.currentPick === playerChoice[1]) {
resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
}
}
}
function resultScreen(display) {
const displayText = document.createElement('p');
displayText.innerText = display;
document.body.appendChild(displayText);
}
var lapisBtn = document.getElementById("lapis").onclick = function() {
playerTurn.currentPick = playerChoice[0];
compareChoices();
}
var papyrusBtn = document.getElementById("papyrus").onclick = function() {
playerTurn.currentPick = playerChoice[1];
compareChoices();
}
var scalpellusBtn = document.getElementById("scalpellus").onclick = function() {
playerTurn.currentPick = playerChoice[2];
compareChoices();
}
Welcome to the game of Lapis, Papyrus, and Scalpelus!
<h2>Make your Choice Below</h2>
<button id="lapis">Lapis</button>
<button id="papyrus">Papyrus</button>
<button id="scalpellus">Scalpellus</button>
关于javascript - 单击按钮元素时如何更新属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65676643/