提问人:Korekollum 提问时间:9/3/2023 最后编辑:Korekollum 更新时间:9/4/2023 访问量:50
playerSelection() 没有返回,但它传递了一个字符串值?[石头剪刀布游戏]
playerSelection() is not returning but its getting passed a string value? [Rock paper scissors game]
问:
您好,我目前正在重温 html/css/javascript 中的石头剪刀布游戏。现在我正在尝试制作一个相应的网站,您可以在其中单击该按钮,它会以这种方式记录您的playerChoice。之后,它将开始游戏并打印获胜者、回合等。。似乎playerSelection函数无法正常工作。playerChoice = this.id 似乎工作正常,但 playerSelection 函数没有控制台记录任何内容。我还想知道我是否需要更改任何内容才能将字符串从函数中取出并进行比较(参见 playRound 函数)。如果您碰巧对此有所了解,请告诉我:)
这是我的HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script defer src="script.js" type="text/javascript"></script>
<title>Rock, Paper, Scissors</title>
</head>
<body>
<h1>Rock, Paper, Scissors</h1>
<p>Play against the computer. First to 5 points wins the game!</p>
<h2>Select a Move:</h2>
<div id="Buttons">
<button id="ROCK">ROCK</button>
<button id="PAPER">PAPER</button>
<button id="SCISSORS">SCISSORS</button>
</div>
<p id="roundTxt"></p>
<h2>Scores:</h2>
<ul id="scoresBox">
<h3>Player: </h3>
<div id="SCORES_HUMAN">0</div>
<h3>Computer: </h3>
<div id="SCORES_COMPUTER">0</div>
</ul>
</body>
</html>
这是我的javascript:
document.getElementById("ROCK").onclick = playerSelection;
document.getElementById("PAPER").onclick = playerSelection;
document.getElementById("SCISSORS").onclick = playerSelection;
function getComputerChoice (){
//select Rock Paper or Scissors at random
//return the choice
const PICK = ["Rock", "Paper", "Scissors"];
const random = Math.floor(Math.random() * PICK.length);
return (PICK[random]);
}
function playerSelection() {
//prompt user for their choice
//return the choice
//let playerChoice = prompt("Rock, Paper, or Scissors?");
playerChoice = this.id;
return playerChoice;
}
console.log(playerSelection());
function game() {
//Scoreboard
let SCORES_HUMAN = 0;
let SCORES_COMPUTER = 0;
//for(let i = 0; i<5; i++) {
playRound(playerSelection(), getComputerChoice());
console.log(playRound(playerSelection(), getComputerChoice()));
//}
if(SCORES_HUMAN > SCORES_COMPUTER) {
console.log("You Win!");
} else if(SCORES_HUMAN < SCORES_COMPUTER) {
console.log("You Lose!");
}
console.log("HUMAN: "+ SCORES_HUMAN +" COMPUTER: "+ SCORES_COMPUTER);
function playRound(playerSelection, getComputerChoice) {
//compare playerSelection to getComputerChoice
//return the outcome
if (playerSelection === "ROCK" && getComputerChoice === "Scissors") {
SCORES_HUMAN++;
return "You Win! Rock beats Scissors";
} else if (playerSelection === "ROCK" && getComputerChoice === "Paper") {
SCORES_COMPUTER++;
return "You Lose! Paper beats Rock";
} else if (playerSelection === "PAPER" && getComputerChoice === "Rock") {
SCORES_HUMAN++;
return "You Win! Paper beats Rock";
} else if (playerSelection === "PAPER" && getComputerChoice === "Scissors") {
SCORES_COMPUTER++;
return "You Lose! Scissors beats Paper";
} else if (playerSelection === "SCISSORS" && getComputerChoice === "Paper") {
SCORES_HUMAN++;
return "You Win! Scissors beats Paper";
} else if (playerSelection === "SCISSORS" && getComputerChoice === "Rock") {
SCORES_COMPUTER++;
return "You Lose! Rock beats Scissors";
} else {
return "It's a tie!";
}
}
}
game();
这是我的css:
body {
font-family: Verdana, Geneva, Tahoma, sans-serif;
margin-left: 50px;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
p {
font-size: 18px;
}
button {
border: .3rem solid rgb(255, 0, 0);
border-radius: .5rem;
margin: .75rem;
font-size: 1rem;
font-weight: bold;
padding: .75rem .5rem;
width: 6rem;
text-align: center;
color: white;
background: rgb(89, 89, 89);
text-shadow: 0 0 .5rem rgb(229, 229, 229);
}
button:hover {
cursor: pointer;
}
button:active {
background-color: rgba(92, 92, 92, 0.208);
}
.Buttones {
flex: display;
align-self: center;
}
我感谢我得到的任何帮助,并感谢您阅读:)
单击岩石按钮(或纸/剪刀按钮)会将字符串“rock”传递到游戏回合功能中,游戏将正确进行。
答:
因为 playerSelection 函数中的 'this' 指向的是函数,而不是事件。 将事件参数传递给 playerSelection 函数
function playerSelection(e) { ... }
代码存在许多问题。
- 调用两次:
playRound()
playRound(playerSelection(), getComputerChoice());
console.log(playRound(playerSelection(), getComputerChoice()));
我知道你试图看看在做什么,但实际上这是在每个“回合”执行两次。您已经在 中返回了文本语句,因此playRound()
playRound()
- 将您的 s 更改为 s,然后调用 或
return
console.log()
playRound(playerSelection(), getComputerChoice())
- 删除不在 中的调用,并保持原样。
playRound()
console.log()
playRound()
正如上面 Ahmed 所提到的,您传递的不是单击事件,而是对函数本身的引用。您需要收听按下按钮时发出的事件,以便正确处理玩家的选择
一个小问题,但您在 html 文档中提到第一个获得 5 的玩家获胜,但是您只允许使用您的(注释掉的)for 循环进行 5 轮比赛。相反,请使用如下所示的 while 循环:
while (SCORES_HUMAN < 5 and SCORES_COMPUTER < 5){
playRound(playerSelection(), getComputerChoice());
}
编辑更新以包含 JS 代码的工作示例。没有对 HTML 或 CSS 的相关更改。
function getComputerChoice (){
//select Rock Paper or Scissors at random
//return the choice
const PICK = ["Rock", "Paper", "Scissors"];
const random = Math.floor(Math.random() * PICK.length);
return (PICK[random]);
}
let SCORES_HUMAN = 0;
let SCORES_COMPUTER = 0;
const SCORES_HUMAN_TEXT = document.getElementById("SCORES_HUMAN");
const SCORES_COMPUTER_TEXT = document.getElementById("SCORES_COMPUTER");
const ROCK = document.getElementById("ROCK");
const PAPER = document.getElementById("PAPER");
const SCISSORS = document.getElementById("SCISSORS");
function updateScores() {
SCORES_HUMAN_TEXT.innerHTML = SCORES_HUMAN
SCORES_COMPUTER_TEXT.innerHTML = SCORES_COMPUTER
}
ROCK.addEventListener("click", function (e){
playRound ("ROCK");
});
PAPER.addEventListener("click", function (e){
playRound("PAPER");
});
SCISSORS.addEventListener("click", function (e){
playRound("SCISSORS");
});
function game() {
//Scoreboard
/*
while (SCORES_HUMAN < 5 && SCORES_COMPUTER < 5){
playRound(playerSelection(), getComputerChoice());
}
console.log(playRound(playerSelection(), getComputerChoice()));
*/
if(SCORES_HUMAN > SCORES_COMPUTER) {
console.log("You Win!");
} else if(SCORES_HUMAN < SCORES_COMPUTER) {
console.log("You Lose!");
}
console.log("HUMAN: "+ SCORES_HUMAN +" COMPUTER: "+ SCORES_COMPUTER);
}
function playRound(playerSelection) {
computerChoice = getComputerChoice()
//compare playerSelection to getComputerChoice
//return the outcome
if (playerSelection === "ROCK" && computerChoice === "Scissors") {
SCORES_HUMAN++;
// return "You Win! Rock beats Scissors";
} else if (playerSelection === "ROCK" && computerChoice === "Paper") {
SCORES_COMPUTER++;
// return "You Lose! Paper beats Rock";
} else if (playerSelection === "PAPER" && computerChoice === "Rock") {
SCORES_HUMAN++;
// return "You Win! Paper beats Rock";
} else if (playerSelection === "PAPER" && computerChoice === "Scissors") {
SCORES_COMPUTER++;
// return "You Lose! Scissors beats Paper";
} else if (playerSelection === "SCISSORS" && computerChoice === "Paper") {
SCORES_HUMAN++;
// return "You Win! Scissors beats Paper";
} else if (playerSelection === "SCISSORS" && computerChoice === "Rock") {
SCORES_COMPUTER++;
// return "You Lose! Rock beats Scissors";
} else {
// return "It's a tie!";
}
updateScores()
}
//game();
评论
code
codepen
评论
game()
playRound()
console.log()
playerSelection()
console.log()