显示岩石剪刀游戏的结果 - JavaScript
我正在用JavaScript创建岩石剪刀游戏。我目前在一系列对象中有选择。我要做的是当用户选择自己的选择时,它将显示他们的选择和计算机的选择,但也将显示获奖者。我有3个用于岩石,纸和剪刀的PNG图像文件。
我的代码带有关于我要完成的工作的评论
const selectionButtons = document.querySelectorAll('[data-selection]')
const finalColumn = document.querySelector('[data-final-column]')
const SELECTIONS = [{
name: 'rock',
icon: document.querySelector('#uRock'),
beats: 'scissors'
},
{
name: 'paper',
icon: document.querySelector('#uPaper'),
beats: 'rock'
},
{
name: 'scissors',
icon: document.querySelector('#uScissors'),
beats: 'scissors'
}
]
selectionButtons.forEach(selectionButton => {
selectionButton.addEventListener('click', e =>{
const selectionName = selectionButton.dataset.selection
const selection = SELECTIONS.find(selection => selection.name === selectionName)
makeSelection(selection)
})
})
function makeSelection(selection) {
const computerSelection = randomSelection();
const yourWinner = isWinner(selection, computerSelection)
const computerWinner = isWinner(computerSelection, selection)
addSelectionReult(computerSelection, computerWinner)
addSelectionReult(selection, yourWinner)
}
function addSelectionReult(selection, winner){
const div = document.createElement('div')
div.innerHTML =
// add icon from selection to div child
div.classList.add('result-selection')
if(winner) div.classList.add('winner')
if(computerWinner) //add computerLost class to icon img element
if(yourWinner) //add playerWinner class to icon img element
finalColumn.after(div)
}
function isWinner(selection, computerSelection){
return selection.beats === computerSelection.name
}
function randomSelection() {
const random = Math.floor(Math.random() * SELECTIONS.length)
return SELECTIONS[random]
}
html应该看起来像
<!-- <div class="result-selection">
<img class= "winner" src="/noun-fist-477918.png" alt="">
</div>
<div class="result-selection">
<img class="computerLost" src="/noun-scissors-477919.png" alt="">
</div> -->
我想做的
- 用户选择他们的选择。 (选择是选择
- 。 (我的课程将与获胜者与失败者区分开来)
- 如果计算机是获胜者,则正确的图标将拥有冠军班,左图将具有PlayerLost类。球员是获胜者的相反。
我是否必须创建一个是Div的孩子然后附加孩子的变量?
还是只使用语句使用InnerHTML?
完成此操作的最佳方法是什么?
I am creating a rock paper scissors game with JavaScript. I currently have the selections in an array of objects. What I am trying to do is when the user selects their choice it will display their choice and the computer's choice, but will also show the winner. I have 3 png image files for rock, paper, and scissors.
My Code with comments on what I am trying to accomplish
const selectionButtons = document.querySelectorAll('[data-selection]')
const finalColumn = document.querySelector('[data-final-column]')
const SELECTIONS = [{
name: 'rock',
icon: document.querySelector('#uRock'),
beats: 'scissors'
},
{
name: 'paper',
icon: document.querySelector('#uPaper'),
beats: 'rock'
},
{
name: 'scissors',
icon: document.querySelector('#uScissors'),
beats: 'scissors'
}
]
selectionButtons.forEach(selectionButton => {
selectionButton.addEventListener('click', e =>{
const selectionName = selectionButton.dataset.selection
const selection = SELECTIONS.find(selection => selection.name === selectionName)
makeSelection(selection)
})
})
function makeSelection(selection) {
const computerSelection = randomSelection();
const yourWinner = isWinner(selection, computerSelection)
const computerWinner = isWinner(computerSelection, selection)
addSelectionReult(computerSelection, computerWinner)
addSelectionReult(selection, yourWinner)
}
function addSelectionReult(selection, winner){
const div = document.createElement('div')
div.innerHTML =
// add icon from selection to div child
div.classList.add('result-selection')
if(winner) div.classList.add('winner')
if(computerWinner) //add computerLost class to icon img element
if(yourWinner) //add playerWinner class to icon img element
finalColumn.after(div)
}
function isWinner(selection, computerSelection){
return selection.beats === computerSelection.name
}
function randomSelection() {
const random = Math.floor(Math.random() * SELECTIONS.length)
return SELECTIONS[random]
}
HTML of What it is supposed to look like
<!-- <div class="result-selection">
<img class= "winner" src="/noun-fist-477918.png" alt="">
</div>
<div class="result-selection">
<img class="computerLost" src="/noun-scissors-477919.png" alt="">
</div> -->
What I am trying to do
- User selects their choice. (the choice is selection.icon)
- In the div that is created with JavaScript it will display the choices and the winner. (I have classes made up that will distinguish the winner vs loser)
- If the computer is the winner the right icon will have the winner class and the left icon will have the playerLost class. Opposite for player being the winner.
Would I have to create a variable that is a child of the div and then append the child?
Or just use if statements and use innerHTML?
What is the best way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)