gpt4 book ai didi

javascript - 永远是平局!剪刀石头布游戏

转载 作者:行者123 更新时间:2023-12-02 16:03:56 24 4
gpt4 key购买 nike

这是我的第一个游戏和石头剪刀布游戏的代码,并经过 CodeAcademy 的一些调整。我被困了两个小时试图找出为什么它总是导致平局。

这是我的 HTML

<!DOCTYPE  html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Rock Paper Scissors</title>
</head>

<body>
<form>
Rock
<input type="checkbox" id="rock">
Paper
<input type="checkbox" id="paper">
Scissors
<input type="checkbox" id="scissors">
<button onclick="startGame()">Start<button>
</form>

<script src="userInt.js"></script>
</body>

</html>

Javascript代码:

function userChoice(){
var rock = document.getElementById("rock").checked;
var scissors = document.getElementById("scissors").checked;
var paper = document.getElementById("paper").checked;

}
function computerChoice(){
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";

} else if(computerChoice <= 0.67) {
computerChoice = "paper";

} else {
computerChoice = "scissors";
}

}
function compare(userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";

}
if (userChoice ==="rock"){

if(computerChoice ==="scissors"){
return "rock wins";
}else{
return "paper wins";
}
}
if(userChoice ==="paper"){

if (computerChoice ==="scissors"){
return "paper wins"
}else{
return "scissors wins"
}
}

if(userChoice ==="scissors"){

if (computerChoice ==="rock"){
return "rock wins"
}else{
return "scissors wins"
}
}
}

function startGame(){
var getUserChoice = userChoice();
var getComputerChoice = computerChoice();
var endGame = compare(getUserChoice, getComputerChoice);
alert(endGame)
}

感谢支持!

最佳答案

您的代码中有两个问题:

  1. userChoice 不返回任何内容(未定义)
  2. computerChoice 不返回任何内容(未定义)
<小时/>
function userChoice() {
var rock = document.getElementById("rock").checked;
var scissors = document.getElementById("scissors").checked;
var paper = document.getElementById("paper").checked;

if (rock) {
return 'rock';
}

if (scissors) {
return 'scissors';
}

if (paper) {
return 'paper';
}

}

function computerChoice() {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";

} else if (computerChoice <= 0.67) {
computerChoice = "paper";

} else {
computerChoice = "scissors";
}

return computerChoice;

}

function compare(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";

}
if (userChoice === "rock") {

if (computerChoice === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
}
if (userChoice === "paper") {

if (computerChoice === "scissors") {
return "paper wins"
} else {
return "scissors wins"
}
}

if (userChoice === "scissors") {

if (computerChoice === "rock") {
return "rock wins"
} else {
return "scissors wins"
}
}
}

function startGame() {
var getUserChoice = userChoice();
console.log(getUserChoice);
var getComputerChoice = computerChoice();
console.log(getComputerChoice);
var endGame = compare(getUserChoice, getComputerChoice);
alert(endGame);
}

Working demo on JSFiddle.

关于javascript - 永远是平局!剪刀石头布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951517/

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