-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
368 lines (308 loc) · 10.8 KB
/
script.js
File metadata and controls
368 lines (308 loc) · 10.8 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'use strict'
console.log("Script is loaded successful")
/* -------------------------------------------------------------------------- */
// Main functions
/* -------------------------------------------------------------------------- */
/*------ Chose GameMode Single/Multi ------*/
//player function to create player with stats and proprieties
const Player = (name, isFirst) => {
this.name = name
this.PlayerScore = 0
this.isFirst = isFirst
this.sign = 'o';
//functions
(() => {
if (this.isFirst)
this.sign = 'x'
})()
const incrementSore = () => {
this.PlayerScore++;
}
const changeOrder = () => {
this.isFirst = !this.isFirst
}
return {sign, name, isFirst, PlayerScore, changeOrder, incrementSore}
}
/*------ func that render game ------*/
const gameplay = (() => {
const square = document.querySelector(".square")
//note: din a 5-a oara poate fi facuta primul castig
// variables
let steps = 0,
currentSign = '',
gameArr = [/*
'o', 'x', 'o',
'x', 'x', 'o',
'o', 'o', 'x'*/
'g', 'a', 'm',
'e', 's', 't',
'a', 'r', 't'
]
const reloadGame = () => {
console.log(`reloading...`)
const squares = document.querySelectorAll('.miniSquare')
//remove non-needed classes
squares.forEach(square => {
if (square.firstChild) {
square.classList.remove('clicked')
square.removeChild(square.firstChild)
}
})
//reload all variables
gameArr = ['g', 'a', 'm', 'e', 's', 't', 'a', 'r', 't']
steps = 0
currentSign = ''
}
const gameOver = () => {
console.log('Game Over')
alert('gameOver')
}
// check if player has won
const checkWinner = () => {
if (gameArr.length > 5) {
console.log(`checking...`)
let winCheck = 0,
sign,
gameArr2d = [],
gameArrCopy = [...gameArr]
while (gameArrCopy.length) gameArr2d.push(gameArrCopy.splice(0, 3));
const horizontalCheck = () => {
winCheck = 0
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
sign = gameArr2d[i][0]
if (sign === gameArr2d[i][j])
winCheck++
}
if (winCheck >= 3) {
return 1
} else winCheck = 0
}
}
const verticalCheck = () => {
winCheck = 0
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
sign = gameArr2d[0][i]
if (sign === gameArr2d[j][i])
winCheck++
}
if (winCheck >= 3) {
return 1
} else winCheck = 0
}
}
const mainDiagonalCheck = () => {
winCheck = 0
sign = gameArr2d[0][0]
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === j)
if (sign === gameArr2d[i][j])
winCheck++
}
}
if (winCheck >= 3) {
return 1
}
}
const secondaryDiagonalCheck = () => {
winCheck = 0
sign = gameArr2d[0][2]
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if ((i + j) === 2)
if (sign === gameArr2d[i][j])
winCheck++
}
}
if (winCheck >= 3) {
return 1
}
}
// checking for winner
if (horizontalCheck()
|| verticalCheck()
|| mainDiagonalCheck()
|| secondaryDiagonalCheck()) {
gameOver()
console.log(horizontalCheck(), verticalCheck(), mainDiagonalCheck(), secondaryDiagonalCheck())
}
}
}
//create DOM for X and O visualization
const createChild = (tag = 'div', className = '', id = '') => {
const child = document.createElement(tag)
if (className) {
child.setAttribute('class', className.toString())
}
if (className) {
child.setAttribute('id', id.toString())
}
return child
}
//change sign (x or o)
const changeSign = (clickedItem) => {
if (steps < 9) {
if (steps % 2 === 0)
currentSign = 'x'
else
currentSign = 'o'
clickedItem.appendChild(createChild('div', currentSign))
steps++
}
}
//find square order
const getSquareNumber = (clickedItem) => {
let number = clickedItem.classList[1].match(/\d+/)[0]
return parseInt(number) - 1
}
//insert signs into an array
const insertSquareToArr = (clickedItem) => {
let squareOrder = getSquareNumber(clickedItem)
for (let i = 0; i < 9; i++)
if (i === squareOrder) {
gameArr[i] = currentSign
}
}
//toggle class on click to add x or o
if (square)
square.addEventListener('click', (e) => {
if (e.target !== e.currentTarget && e.target.classList.contains('miniSquare')) {
const clickedItem = e.target
let clickedItemClass = e.target.classList[1]
if (clickedItem.classList[2] !== 'clicked') {
if (!clickedItemClass) {
clickedItemClass = e.target.parentNode.classList[1]
}
//change sign from x to o
changeSign(clickedItem)
//find square order
insertSquareToArr(clickedItem)
//add class clicked to miniSquare to prevent multiple signs in one square
clickedItem.classList.add('clicked')
// check winner
checkWinner(gameArr)
}
}
e.stopPropagation()
}, false)
return {/*checkWinner,*/ gameArr, reloadGame}
})()
const screenRender = (() => {
const choseMode = document.querySelectorAll('.choseMode')
const drawHash = document.querySelector('#drawHash')
const PlayersInfo = document.querySelector('.players')
//multiplayer button click action
choseMode[1].addEventListener('click', () => {
})
//one event listener for all buttons
const mainBoardFooter = document.querySelector('.main-board__footer')
if (mainBoardFooter)
mainBoardFooter.addEventListener('click', (e) => {
if (e.target.getAttribute('type') === 'button') {
const butt = e.target
//check if the button is multiplayer and add gsap animation
if (butt.classList.contains('multiPlayer')) {
//multiplayer switch animation
anim.multiplayer()
} else if (butt.classList.contains('singlePlayer')) {
} else if (butt.classList.contains('start')) {
//todo: to revert
// if (document.querySelectorAll('.playerName')[0].value.length !== 0 && document.querySelectorAll('.playerName')[0].value.length !== 0)
anim.start()
}
}
})
//add event listener to reload button
const reload = document.querySelector('.reload')
reload.addEventListener('click', () => {
gameplay.reloadGame()
})
// h1 click to reload page
const mainHeader = document.querySelector('.title')
mainHeader.addEventListener('click', () => {
rel()
console.log('hmm, works')
})
return {choseMode}
})()
const anim = (() => {
const multiplayer = () => {
const timeLine = gsap.timeline({defaults: {duration: .4}})
timeLine.to('#drawHash', {duration: .3, opacity: 0, x: '-25%', ease: 'back.in'})
.to('.players', {opacity: 1, ease: 'back.out'})
.from('.players', {x: '25%', ease: 'back.out'}, '-=.4')
.to('.choseMode', {
duration: .2, scale: 0, display: 'none', ease: 'back.in', x: (i, elem, buttons) => {
return i % 2 === 1 ? -100 : 100
}
}, '-=.6')
.fromTo('.start', {duration: 0, scale: 0}, {
display: 'flex',
scale: 1,
ease: 'back.out'
}, '-=.4')
}
const start = () => {
const tl = gsap.timeline({defaults: {duration: .45}})
tl.to('.start', {scale: 0, y: '-180'})
.to('.players__labels', {
scale: 0,
y: (i) => {
return i % 2 === 1 ? -40 : 30
}
}, '-=.5')
.to('.options', {duration: 0, display: 'none'})
.to('#game-board', {duration: 0, display: 'flex'})
.to('.square', {duration: 0, display: 'grid', scale: 0})
.to('.square', {scale: 1, ease: 'back.out'})
.to('.reload', {duration: .35, opacity: 1})
//todo: to change it(func)
document.querySelector('#game-board').style = 'align-items: center;\n'
}
return {multiplayer, start}
})()
// const choseMode = (() => {
// const gameBoard = document.getElementById('game-board')
// const single = document.getElementById('single')
// const multi = document.getElementById('multi')
//
// const clearBoard = () => {
// gameBoard.innerHTML = ''
// }
//
// if (multi)
// multi.addEventListener('click', () => {
//
// clearBoard()
//
// })
//
// return {multi, clearBoard}
// })()
// const squares = document.getElementsByClassName('miniSquare'),
// ToggleClass = () => {
// [].map.call(squares, function (elem) {
// elem.classList.remove('animCircle')
// }
// )
// this.classList.add('animCircle')
// }
// [].map.call(squares, function (elem) {
// elem.addEventListener('click', ToggleClass, false)
// })
//handy functions
const clear = () => {
console.clear();
}
const rel = () => {
location.reload();
}
// console.log('in 3 sec all will be deleted');
// setTimeout(function () {
// clear();
// }, 1000);
//test
const sandu = Player('sandu', true)
// gameplay.checkWinner(gameplay.gameArr)