我正在做剪刀石头布游戏。我对javascript很陌生,所以我不太了解。我已经创建了一个函数,当用户进行选择时可以显示计算机的选择。我接下来要做的是创建一个函数,该函数将用户做出的选择与cpu做出的选择进行比较,然后返回结果,无论是赢还是输。
我试图运行一个if语句来检查它们是否相等,但是在做出选择时我无法弄清楚如何检查两个图像是否相等。
let userRock = document.querySelector('.rock')
let userPaper = document.querySelector('.paper')
let userScissors = document.querySelector('.scissors')
const cpuScissors = document.querySelector('.cpu-scissors')
const cpuPaper = document.querySelector('.cpu-paper')
const cpuRock = document.querySelector('.cpu-rock')
let result = document.querySelector('.result')
let currentItem;
userPaper.addEventListener('click', start)
userScissors.addEventListener('click', cpuChoice)
userRock.addEventListener('click', cpuChoice)
function start() {
cpuChoice()
getWinner()
}
// Computer Choice
function cpuChoice() {
const rand = Math.random()
if (currentItem) {
currentItem.style.display = 'none'
}
if (rand < .34) {
cpuPaper.style.display = 'inline-block'
currentItem = cpuPaper;
} else if (rand >= .67) {
cpuRock.style.display = 'inline-block'
currentItem = cpuRock;
} else {
cpuScissors.style.display = 'inline-block'
currentItem = cpuScissors;
}
}
// Get Winner
function getWinner() {
}
<div class="main-container">
<div class="score">
<p>You:0</p>
<p>Computer:0</p>
</div>
<div class="user-choice">
<img class="rock" src="icons/rock.png">
<img class="paper" src="icons/paper.png">
<img class="scissors" src="icons/scissors.png">
</div>
<div class="cpu-result">
<img class="cpu-rock" src="icons/rock.png">
<img class="cpu-paper" src="icons/paper.png">
<img class="cpu-scissors" src="icons/scissors.png">
</div>
<div class="result"></div>
答案 0 :(得分:0)
您的方法在以下方面是错误的:
-您让玩家选择;
-您让计算机选择;
-您更新视图(隐藏/显示图像);
-然后您尝试比较显示的图像以确定谁赢了。
正确的逻辑是:
-让玩家选择;
-让计算机选择;
-比较两个结果以确定谁赢了;
-更新视图(隐藏/显示图像)。
所显示的内容只是脚本脚本幕后发生情况的反映。
我在10分钟内完成了一个剪刀石头布小游戏。寻找获奖者的方法可能更聪明,但是您会明白的。
const items = ["rock", "paper", "scissors"],
issues = ["Draw", "You win", "Computer wins"];
let playerChoice = "",
compChoice = "";
$("button").click(function() {
const $this = $(this);
$("button").css("outline", "none");
$this.css("outline", "blue solid 2px");
playerChoice = $this.data("val");
computerChooses();
})
const computerChooses = () => {
const rand = Math.floor(Math.random() * 3); // 0,1 or 2
compChoice = items[rand]; // "rock", "paper" or "scissors"
$(".compChoice img").hide();
$(".compChoice").show();
$("#img" + rand).show();
findWinner()
}
const findWinner = () => {
let issue;
if (playerChoice === compChoice) {
issue = 0;
} else if (playerChoice === "rock") {
issue = compChoice === "scissors" ? 1 : 2;
} else if (playerChoice === "paper") {
issue = compChoice === "scissors" ? 2 : 1;
} else { // player chose scissors
issue = compChoice === "rock" ? 2 : 1;
}
$(".result").text(issues[issue]) // "Draw", "You win" or "Computer wins"
}
button {
cursor: pointer;
}
img {
width: 32px;
height: 32px;
}
.compChoice {
display: none;
}
.compChoice img {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Choose :</div>
<button data-val="rock">
<img src="https://image.flaticon.com/icons/svg/838/838023.svg"/>
</button>
<button data-val="paper">
<img src="https://img.icons8.com/metro/1600/sheet-of-paper.png"/>
</button>
<button data-val="scissors">
<img src="https://png.icons8.com/scissors/win8/1600"/></button>
<br/>
<br/>
<div class="compChoice">
<div>Computer picks :</div><img id="img0" src="https://image.flaticon.com/icons/svg/838/838023.svg" /><img id="img1" src="https://img.icons8.com/metro/1600/sheet-of-paper.png" /><img id="img2" src="https://png.icons8.com/scissors/win8/1600" />
</div>
<br/>
<div class="result"></div>