-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
215 lines (179 loc) · 6.53 KB
/
script.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const audio = document.querySelector('audio');
const body = document.querySelector('body');
const container = document.createElement('div');
container.id = "container";
body.appendChild(container);
let botaoReset = document.getElementById('botao_resetar')
let modalReset = document.getElementById('ganhou')
let winnerText = document.getElementById('ganhouTexto')
let acabou = false
const board = [
"BBBBBB",
"BBBBBB",
"BBBBBB",
"BBBBBB",
"BBBBBB",
"BBBBBB",
"BBBBBB"
];
let jogada = 'vermelho'
let horizontalCompare = 2 // Times needed to compare horizontally
let verticalCompare = 3 // Times needed to compare vertically
const criarBoard = () => {
container.style.display = 'flex'
for (let i = 0; i < board.length; i++) {
let coluna = document.createElement('div');
coluna.classList = "coluna"
coluna.dataset.index = i
container.appendChild(coluna);
for (let j = 0; j < board[i].length; j++) {
let linha = document.createElement('div');
linha.classList = "linha"
linha.dataset.index = j
coluna.appendChild(linha);
}
}
}
criarBoard()
function swapPlayer(event) {
audio.volume = 0.1
audio.play();
let torre = event.currentTarget.children
for (let i = 0; i < torre.length; i++) {
if (!torre[i].hasChildNodes()) {
if (jogada === 'vermelho' && acabou === false) {
let disco = document.createElement('div')
disco.classList.remove('disco-black')
disco.classList.add('disco-red')
torre[i].appendChild(disco)
jogada = 'preto'
break
} else if (jogada === 'preto' && acabou === false) {
let disco = document.createElement('div')
disco.classList.remove('disco-red')
disco.classList.add('disco-black')
torre[i].appendChild(disco)
jogada = 'vermelho'
break
}
}
}
verticalWin()
horizontalWin()
firstDiagonalWin()
secondDiagonalWin()
draw()
}
const getTowers = () => {
let array = document.querySelectorAll('.coluna')
for (let i = 0; i < array.length; i++) {
array[i].addEventListener("click", swapPlayer)
}
}
getTowers()
function winCompare(item, firstNextItem, secondNextItem, thirdNextItem) {
if (item !== undefined &&
firstNextItem !== undefined &&
secondNextItem !== undefined &&
thirdNextItem !== undefined) {
if (item.className === firstNextItem.className &&
item.className === secondNextItem.className &&
item.className === thirdNextItem.className) {
// Modal Display //
if (item.className === 'disco-red') {
acabou = true
winnerText.innerText = 'Petter Quill Ganhou!'
modalReset.classList.add('petterWin')
}
else if (item.className === 'disco-black') {
acabou = true
winnerText.innerText = 'Rocket Raccoon Ganhou!'
modalReset.classList.add('raccoonWin')
}
setTimeout(mostrarVitorioso, 700);
//setTimeout()
}
}
}
// Vertical Win Condition //
function verticalWin() {
for (let i = 0; i < container.children.length; i++) {
for (let j = 0; j <= horizontalCompare; j++) {
let item = container.children[i].children[j].children[0]
let firstNextItem = container.children[i].children[j + 1].children[0]
let secondNextItem = container.children[i].children[j + 2].children[0]
let thirdNextItem = container.children[i].children[j + 3].children[0]
winCompare(item, firstNextItem, secondNextItem, thirdNextItem)
}
}
}
// Horizontal Win Condition //
function horizontalWin() {
for (let i = 0; i <= verticalCompare; i++) {
for (let j = 0; j < container.children[i].children.length; j++) {
let item = container.children[i].children[j].children[0]
let firstNextItem = container.children[i + 1].children[j].children[0]
let secondNextItem = container.children[i + 2].children[j].children[0]
let thirdNextItem = container.children[i + 3].children[j].children[0]
winCompare(item, firstNextItem, secondNextItem, thirdNextItem)
}
}
}
// First Diagonal Win Condition //
function firstDiagonalWin() {
for (let i = 0; i <= verticalCompare; i++) {
for (let j = 0; j <= horizontalCompare; j++) {
let item = container.children[i].children[j].children[0]
let firstNextItem = container.children[i + 1].children[j + 1].children[0]
let secondNextItem = container.children[i + 2].children[j + 2].children[0]
let thirdNextItem = container.children[i + 3].children[j + 3].children[0]
winCompare(item, firstNextItem, secondNextItem, thirdNextItem)
}
}
}
// Second Diagonal Win Condition //
function secondDiagonalWin() {
for (let i = container.children.length - 1; i >= verticalCompare; i--) {
for (let j = 0; j <= horizontalCompare; j++) {
let item = container.children[i].children[j].children[0]
let firstNextItem = container.children[i - 1].children[j + 1].children[0]
let secondNextItem = container.children[i - 2].children[j + 2].children[0]
let thirdNextItem = container.children[i - 3].children[j + 3].children[0]
winCompare(item, firstNextItem, secondNextItem, thirdNextItem)
}
}
}
// Draw Condition //
function draw() {
let primeiraLinha = document.querySelectorAll('[data-index="5"]')
let contagem = 0
for (let i = 0; i < primeiraLinha.length; i++) {
if (i !== 5) {
if (primeiraLinha[i].hasChildNodes()) {
contagem += 1
}
}
}
// Modal Display //
if (contagem === 7) {
winnerText.innerText = 'Empatou!'
modalReset.classList.add('draw')
setTimeout(mostrarVitorioso, 700);
}
}
// Reset Game Function //
const gameReset = () => {
container.innerHTML = ''
modalReset.classList.remove('draw')
modalReset.classList.remove('petterWin')
modalReset.classList.remove('raccoonWin')
modalReset.style.display = 'none'
acabou = false
criarBoard()
getTowers()
}
const mostrarVitorioso = () => {
container.style.display = 'none'
modalReset.style.display = 'flex'
}
botaoReset.addEventListener("click", gameReset);