-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
178 lines (154 loc) · 4.99 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
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
const mask = document.querySelector('.mask')
const resetBtn = document.querySelector('.js-reset')
const container = document.querySelector('.container')
// Sounds
const winSound = new Audio('./sounds/win.mp3')
const initSound = new Audio('./sounds/init.mp3')
const errorSound = new Audio('./sounds/error.mp3')
const unknownSound = new Audio('./sounds/unknown.mp3')
// Game Data
let lastClickCount = 0
let lastClickIsBlue = false
// Play Sound Function
const playSound = (action = 'init') => {
if(action === 'error'){
errorSound.play()
} else if(action === 'unknown'){
unknownSound.play()
} else if(action === 'win'){
winSound.play()
} else { initSound.play() }
}
// on click Function
const onClick = (e) => {
let element = e.target
let id = element.getAttribute('data-id')
console.log(lastClickCount);
if(lastClickCount === 9) {
finishGame()
return
};
if(element.className !== 'box') {
playSound('error')
return
}
if(lastClickIsBlue){
element.classList.add('yellow')
} else {
element.classList.add('blue')
}
checkGameStatus(id)
lastClickCount++
lastClickIsBlue = !lastClickIsBlue
}
// Check Game Status
const checkGameStatus = (id) => {
if(lastClickCount < 4) return;
let boxes = document.querySelectorAll('.container .box')
let currentColor = lastClickIsBlue ? 'yellow' : 'blue'
let currentColorItems = []
boxes.forEach(box => {
if(box.classList.contains(currentColor)){
currentColorItems.push(parseInt(box.getAttribute('data-id')))
}
})
if(isWinner(currentColorItems)){
finishGame(true , !lastClickIsBlue , `${currentColor} won ${lastClickIsBlue ? '😋': '😝'}`)
}
}
// Check Win
const isWinner = (nodes = []) => {
if( (nodes.includes(1) && nodes.includes(2) && nodes.includes(3))
|| (nodes.includes(4) && nodes.includes(5) && nodes.includes(6))
|| (nodes.includes(7) && nodes.includes(8) && nodes.includes(9))
|| (nodes.includes(1) && nodes.includes(4) && nodes.includes(7))
|| (nodes.includes(2) && nodes.includes(5) && nodes.includes(8))
|| (nodes.includes(3) && nodes.includes(6) && nodes.includes(9))
|| (nodes.includes(1) && nodes.includes(5) && nodes.includes(9))
|| (nodes.includes(3) && nodes.includes(5) && nodes.includes(7))
) return true
return false
}
// Finish Game Function
const finishGame = (hasWinner = false , winnerIsBlue = false , message = 'Game Is Finished 😩') => {
let playBtn = document.createElement('button')
let maskText = document.createElement('p')
if(hasWinner){
let winner = document.createElement('div')
winner.className = 'winner'
mask.appendChild(winner)
if(winnerIsBlue){
winner.classList.add('bg-blue')
} else {
winner.classList.add('bg-yellow')
}
playSound('win')
} else {
playSound('unknown')
}
playBtn.className = 'play-btn'
playBtn.innerHTML = 'Play Now'
maskText.innerHTML = message
mask.appendChild(maskText)
mask.appendChild(playBtn)
mask.classList.add('is-show')
playBtn.addEventListener('click' , initGame)
}
// Init Game Function
const initGame = () => {
winSound.pause()
errorSound.pause()
unknownSound.pause()
winSound.currentTime = 0
errorSound.currentTime = 0
unknownSound.currentTime = 0
lastClickCount = 0
mask.innerHTML = ''
lastClickIsBlue = false
container.innerHTML = ''
mask.classList.remove('is-show')
for(let index = 0; index < 9; index ++){
let box = document.createElement('div')
box.className = 'box'
box.innerHTML = index + 1
box.setAttribute('data-id' , index + 1)
box.addEventListener('click' , onClick)
container.appendChild(box)
}
}
// Reset Game
resetBtn.addEventListener('click' , () => {
initGame()
playSound()
})
// Start Game
initGame()
// Init Shortcuts
document.addEventListener("keydown", event => {
if(event.which === 27) initGame()
if(event.which === 49 || event.which === 97){ downKey(1) }
if(event.which === 50 || event.which === 98){ downKey(2) }
if(event.which === 51 || event.which === 99){ downKey(3) }
if(event.which === 52 || event.which === 100){ downKey(4) }
if(event.which === 53 || event.which === 101){ downKey(5) }
if(event.which === 54 || event.which === 102){ downKey(6) }
if(event.which === 55 || event.which === 103){ downKey(7) }
if(event.which === 56 || event.which === 104){ downKey(8) }
if(event.which === 57 || event.which === 105){ downKey(9) }
})
const downKey = keyNumber => {
let btn = document.querySelector(`.box[data-id="${keyNumber}"]`)
btn.click()
}
console.log(`%c
Ramin Rezaei \n
https://raminrezaei.ir
\n`, 'color:#999')
/*
______
| __ \ (_)
| |__) |__ _ _ __ ___ _ _ __
| _ // _` | '_ ` _ \| | '_ \
| | \ \ (_| | | | | | | | | | |
|_| \_\__,_|_| |_| |_|_|_| |_|
*/