gpt4 book ai didi

javascript - 单击按钮元素时如何更新属性?

转载 作者:行者123 更新时间:2023-12-04 08:16:30 26 4
gpt4 key购买 nike

我在这里遇到了一个问题,我正在尝试创建这个石头剪刀布游戏,但在选择按钮供用户选择时遇到了问题。我无法获得更新 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com