-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhanoi.js
148 lines (116 loc) · 3.75 KB
/
hanoi.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
139
140
141
142
143
144
145
146
147
148
const main = document.getElementById('main')
const button = document.getElementById('button')
const criadiv =(elemento, classe, pai, id) => {
let criaelemento = document.createElement(elemento)
criaelemento.classList.add(classe)
criaelemento.id = id
return pai.appendChild(criaelemento)
}
/* */
criadiv('div', 'box-clicavel', main,'start')
const start = document.getElementById('start')
criadiv('div', 'pilar', start)
criadiv('div', 'disco', start, 'grande')
const discogrande = document.getElementById('grande')
criadiv('div', 'disco', start, 'medio')
const discomedio = document.getElementById('medio')
criadiv('div', 'disco', start, 'pequeno')
const discopequeno = document.getElementById('pequeno')
/* */
criadiv('div', 'box-clicavel', main,'offset')
const offset = document.getElementById('offset')
criadiv('div', 'pilar', offset)
criadiv('div', 'box-clicavel', main,'end')
const end = document.getElementById('end')
criadiv('div', 'pilar', end)
/* */
let count = 0
const header = document.getElementById('header')
criadiv('span', 'hidden', header,'jogadas')
const jogadas = document.getElementById('jogadas')
const mao = document.getElementById('mao')
const headerContainer = document.querySelector('.header-container')
const messageBox = document.getElementById('mensagem-erro')
/**/
let estadomouse = null
const pegadisco = (e) => {
if(e.currentTarget.childElementCount > 1){
estadomouse = e.currentTarget.lastElementChild
mao.innerText =''
headerContainer.classList.remove('hidden')
messageBox.innerText =''
mao.appendChild(estadomouse)
}
else{
messageBox.innerText = 'Você deve selecionar um disco'
}
}
/**/
const soltadisco = (discoasersolto, destino) => {
if(destino.childElementCount === 1){
destino.appendChild(discoasersolto)
count++
jogadas.innerText = `Jogadas: ${count}`
estadomouse = null
messageBox.innerText =''
}
else if(discoasersolto.clientWidth < destino.lastElementChild.clientWidth){
destino.appendChild(discoasersolto)
count++
jogadas.innerText = `Jogadas: ${count}`
estadomouse = null
messageBox.innerText =''
}
else{
messageBox.innerText = 'Você só pode soltar este disco sobre um disco menor'
}
jogadas.classList.remove('hidden')
victoria()
}
/* */
const movimentacao = (e) => {
if(estadomouse === null){
pegadisco(e)
}
else if(estadomouse !== null) {
let target = e.currentTarget
soltadisco(estadomouse, target)
}
mao.classList.remove('hidden')
headerContainer.classList.remove('hidden')
return estadomouse
}
/* */
criadiv('section', 'flex', document.body, 'container')
const container = document.getElementById('container')
criadiv('div','hidden', container, 'resultado')
const resultado = document.getElementById('resultado')
/* */
const victoria = () => {
let output = false
if(end.childElementCount === 4) {
output = true
resultado.innerText = "YOU WIN!"
container.classList.remove('hidden')
resultado.classList.remove('hidden')
}
}
start.addEventListener('click', movimentacao)
offset.addEventListener('click', movimentacao)
end.addEventListener('click', movimentacao)
/* */
const resetgame = () => {
start.appendChild(discogrande)
start.appendChild(discomedio)
start.appendChild(discopequeno)
resultado.classList.add('hidden')
headerContainer.classList.add('hidden')
jogadas.classList.add('hidden')
estadomouse = null
count = 0
return jogadas.innerText = count
}
button.addEventListener('click', resetgame)
criadiv('footer', 'a', document.body, 'footer')
const footer = document.getElementById('footer')
footer.innerText = "Desenvolvido por Igor & Rodolfo"