-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
213 lines (176 loc) · 6.38 KB
/
app.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
const grid = document.querySelector('.grid')
const scoreDisplay= document.getElementById('score')
const width = 8
const squares = []
let score = 0
scoreDisplay.innerHTML = score
const candyColors = [
'url(images/fotor_red.png)',
'url(images/fotor_yellow.png)',
'url(images/fotor_orange.png)',
'url(images/fotor_purple.png)',
'url(images/fotor_green.png)',
'url(images/fotor_blue.png)'
]
// Create Board
function createBoard() {
for (let i = 0; i < width * width; i++) {
const square = document.createElement('div')
square.setAttribute('draggable', true)
square.setAttribute('id', i)
let randomColor = Math.floor(Math.random() * candyColors.length)
square.style.backgroundImage = candyColors[randomColor]
grid.appendChild(square)
squares.push(square)
}
}
createBoard()
// Drag the candies
let colorBeingDragged
let colorBeingReplaced
let squareIdBeingDragged
let squareIdBeingReplaced
squares.forEach(square => square.addEventListener('dragstart', dragStart))
squares.forEach(square => square.addEventListener('dragend', dragEnd))
squares.forEach(square => square.addEventListener('dragover', dragOver))
squares.forEach(square => square.addEventListener('dragenter', dragEnter))
squares.forEach(square => square.addEventListener('dragleave', dragLeave))
squares.forEach(square => square.addEventListener('drop', dragDrop))
function dragStart() {
colorBeingDragged = this.style.backgroundImage
console.log(colorBeingDragged)
squareIdBeingDragged = parseInt(this.id)
console.log(this.id, 'dragstart')
}
function dragOver(e) {
e.preventDefault()
console.log(this.id, 'dragover')
}
function dragEnter(e) {
e.preventDefault()
console.log(this.id, 'dragenter')
}
function dragLeave() {
console.log(this.id, 'dragleave')
}
function dragDrop() {
console.log(this.id, 'dragdrop')
colorBeingReplaced = this.style.backgroundImage
squareIdBeingReplaced = parseInt(this.id)
this.style.backgroundImage = colorBeingDragged
squares[squareIdBeingDragged].style.backgroundImage = colorBeingReplaced
}
function dragEnd() {
console.log(this.id, 'dragend')
// What is a valid move?
let validMoves = [
squareIdBeingDragged -1,
squareIdBeingDragged -width,
squareIdBeingDragged +1,
squareIdBeingDragged +width,
]
let validMove = validMoves.includes(squareIdBeingReplaced)
if (squareIdBeingDragged && validMove) {
squareIdBeingReplaced = null
}
else if (squareIdBeingReplaced && !validMove) {
squares[squareIdBeingReplaced].style.backgroundImage = colorBeingReplaced
squares[squareIdBeingDragged].style.backgroundImage = colorBeingDragged
}
else squares[squareIdBeingDragged].style.backgroundImage = colorBeingDragged
}
// drop candies once some have been cleared
function moveDown() {
for (i = 0; i < 55; i++) {
if (squares[i + width].style.backgroundImage === '') {
squares[i + width].style.backgroundImage = squares[i].style.backgroundImage
squares[i].style.backgroundImage = ''
const firstRow = [0, 1, 2, 3, 4, 5, 6, 7]
const isFirstRow = firstRow.includes(i)
if (isFirstRow && squares[i].style.backgroundImage === '') {
let randomColor = Math.floor(Math.random() * candyColors.length)
squares[i].style.backgroundImage = candyColors[randomColor]
}
}
}
}
// Checking for matches
// Check for row of Four
function checkRowForFour() {
for (i = 0; i < 60; i++) {
let rowOfFour = [i, i + 1, i + 2, i + 3]
let decidedColor = squares[i].style.backgroundImage
const isBlank = squares[i].style.backgroundImage === ''
const notValid = [5, 6, 7, 13, 14, 15, 21, 22, 23, 29, 30, 31, 37, 38, 39, 45, 46, 47, 53, 54, 55]
if (notValid.includes(i)) continue
if (rowOfFour.every(index =>
squares[index].style.backgroundImage === decidedColor && !isBlank)) {
score += 4
scoreDisplay.innerHTML = score
rowOfFour.forEach(index => {
squares[index].style.backgroundImage = ''
})
}
}
}
checkRowForFour()
// Check Column For Four
function checkColumnForFour() {
for (i = 0; i < 47; i++) {
let columnOfFour = [i, i + width, i + (width * 2), i + (width * 3)]
let decidedColor = squares[i].style.backgroundImage
const isBlank = squares[i].style.backgroundImage === ''
if (columnOfFour.every(index =>
squares[index].style.backgroundImage === decidedColor && !isBlank)) {
score += 4
scoreDisplay.innerHTML = score
columnOfFour.forEach(index => {
squares[index].style.backgroundImage = ''
})
}
}
}
checkColumnForFour()
// Check for row of Three
function checkRowForThree() {
for (i = 0; i < 61; i++) {
let rowOfThree = [i, i + 1, i + 2]
let decidedColor = squares[i].style.backgroundImage
const isBlank = squares[i].style.backgroundImage === ''
const notValid = [6, 7, 14, 15, 22, 23, 30, 31, 38, 39, 46, 47, 54, 55]
if (notValid.includes(i)) continue
if (rowOfThree.every(index =>
squares[index].style.backgroundImage === decidedColor && !isBlank)) {
score += 3
scoreDisplay.innerHTML = score
rowOfThree.forEach(index => {
squares[index].style.backgroundImage = ''
})
}
}
}
checkRowForThree()
// Check Column For Three
function checkColumnForThree() {
for (i = 0; i < 47; i++) {
let columnOfThree = [i, i + width, i + (width * 2)]
let decidedColor = squares[i].style.backgroundImage
const isBlank = squares[i].style.backgroundImage === ''
if (columnOfThree.every(index =>
squares[index].style.backgroundImage === decidedColor && !isBlank)) {
score += 3
scoreDisplay.innerHTML = score
columnOfThree.forEach(index => {
squares[index].style.backgroundImage = ''
})
}
}
}
checkColumnForThree()
window.setInterval(function() {
moveDown()
checkRowForFour()
checkColumnForFour()
checkRowForThree()
checkColumnForThree()
}, 100)