-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
138 lines (112 loc) · 3.93 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const btns = document.querySelectorAll('[data-id]')
const pointElement = document.querySelector('[data-js="point"]')
const fisrtStep = document.querySelector('[data-js="fisrtStep"]')
const secondStep = document.querySelector('[data-js="secondStep"]')
const containerBtnComputer = document.querySelector('[data-js="containerBtnComputer"]')
const containerBtnUser = document.querySelector('[data-js="containerBtnUser"]')
const containerToBackHome = document.querySelector('[data-js="toBackHome"]')
const message = document.querySelector('[data-js="message"]')
const btnPlayAgain = document.querySelector('[data-js="btnPlayAgain"]')
const options = [ 'paper', 'scissors', 'rock' ]
let point = 0
function draw() {
message.innerHTML = 'Deu empate'
toggleShowOptionsToBack()
containerBtnUser.classList.remove('active')
containerBtnComputer.classList.remove('active')
btnPlayAgain.classList.remove('lose')
}
function userIsWinner() {
message.innerHTML = 'Você ganhou'
btnPlayAgain.classList.remove('lose')
containerBtnUser.classList.add('active')
containerBtnComputer.classList.remove('active')
point++
updatePoint()
toggleShowOptionsToBack()
}
function computerIsWinner() {
message.innerHTML = 'Você perdeu'
btnPlayAgain.classList.add('lose')
containerBtnUser.classList.remove('active')
containerBtnComputer.classList.add('active')
point > 0 && point--
updatePoint()
toggleShowOptionsToBack()
}
function toggleShowOptionsToBack() {
containerToBackHome.classList.toggle('show')
}
function createNewButton( tagName, className, option ) {
const element = document.createElement(tagName)
element.classList.add( className )
const img = document.createElement('img')
img.src = `/images/icon-${options[option]}.svg`
element.appendChild(img)
return element
}
function appendButtonUser(option) {
const buttonUser = createNewButton('button', `btn${options[option]}`, option)
containerBtnUser.replaceChildren(buttonUser)
}
function appendButtonComputer(option) {
const buttonComputer = createNewButton('button', `btn${options[option]}`, option)
containerBtnComputer.replaceChildren(buttonComputer)
}
function showWhoWin( userOptions, computerOptions, userWon ) {
fisrtStep.classList.remove('show')
secondStep.classList.add('show')
appendButtonUser(userOptions)
setTimeout(() => {
appendButtonComputer(computerOptions)
if ( userWon === 'empate' ) return draw()
userWon ? userIsWinner() : computerIsWinner()
}, 500);
}
function updatePoint() {
pointElement.innerHTML = point
}
function verifyWhoWin( userOptions, computerOptions ) {
if ( userOptions === 0 ) { // papel
if ( computerOptions === 0 ) {
return 'empate'
} else if ( computerOptions === 1 ) {
return false
} else {
return true
}
}
if ( userOptions === 1 ) { // tesoura
if ( computerOptions === 0 ) {
return true
} else if ( computerOptions === 1 ) {
return 'empate'
} else {
return false
}
} else { // pedra
if ( computerOptions === 0 ) {
return false
} else if ( computerOptions === 1 ) {
return true
} else {
return 'empate'
}
}
}
const getUserOptions = ( button ) => Number(button.getAttribute('data-id'))
function handleClick () {
const userOptions = getUserOptions( this )
const computerOptions = Math.floor( Math.random() * 3 )
const userWon = verifyWhoWin( userOptions, computerOptions )
showWhoWin( userOptions, computerOptions, userWon )
}
btns.forEach( btn => {
btn.addEventListener('click', handleClick)
})
btnPlayAgain.addEventListener('click', () => {
fisrtStep.classList.add('show')
secondStep.classList.remove('show')
toggleShowOptionsToBack()
containerBtnComputer.innerHTML = ''
})