-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
191 lines (146 loc) · 6.16 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
document.addEventListener('DOMContentLoaded',()=>{
const grid = document.querySelector('.grid')
const squares =[]
let score = 0
const displayScore = document.getElementById('score')
const width = 8
const candyColor = ['url(blue-candy.png)',
'url(green-candy.png)',
'url(orange-candy.png)',
'url(red-candy.png)',
'url(purple-candy.png)',
'url(yellow-candy.png)']
function createBoard(){
for(let i=0;i < width * width;i++){
const square = document.createElement('div')
square.setAttribute('draggable',true)
square.setAttribute('id',i)
let color = Math.floor(Math.random() * candyColor.length)
square.style.backgroundImage = candyColor[color]
grid.appendChild(square)
squares.push(square)
}
}
createBoard()
let colorBeingDragged
let colorBeingReplaced
let squareIDdragged
let squareIDreplaced
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
squareIDdragged = 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
squareIDreplaced = parseInt(this.id)
this.style.backgroundImage = colorBeingDragged
squares[colorBeingDragged].style.backgroundImage = colorBeingReplaced
}
function dragEnd() {
console.log(this.id,'dragend')
let validMoves = [squareIDdragged -1,squareIDdragged - width,squareIDdragged +1,squareIDdragged + width]
let validMove = validMoves.includes(squareIDreplaced)
if(colorBeingReplaced && validMove){
squareIDreplaced = null
} else if(colorBeingReplaced && !validMove){
squares[squareIDreplaced].style.backgroundImage = colorBeingReplaced
squares[squareIDdragged].style.backgroundImage = colorBeingDragged
} else squares[squareIDdragged].style.backgroundImage = colorBeingDragged
}
function moveDown(){
for(let 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()* candyColor.length)
squares[i].style.backgroundImage = candyColor[randomcolor]
}
}
}
}
function checkRowForThree() {
for(let i=0;i<61;i++){
let rowofThree = [i,i+1,i+2]
let decidedColor = squares[i].style.backgroundImage
let 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
displayScore.innerHTML = score
rowofThree.forEach(index=> squares[index].style.backgroundImage = '')
}
}
}
checkRowForThree()
function checkColumnForThree() {
for(let i=0;i<47;i++){
let columnofThree = [i,i+width,i+width*2]
let decidedColor = squares[i].style.backgroundImage
let isBlank = squares[i].style.backgroundImage === ''
if(columnofThree.every(index=> squares[index].style.backgroundImage === decidedColor && !isBlank)){
score += 3
displayScore.innerHTML = score
columnofThree.forEach(index=> squares[index].style.backgroundImage = '')
}
}
}
checkColumnForThree()
function checkRowForFour(){
for(let i=0;i<60;i++){
let rowofFour = [i,i+1,i+2,i+3]
let decidedColor = squares[i].style.backgroundImage
let 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
displayScore.innerHTML = score
rowofFour.forEach(index=>squares[index].style.backgroundImage= '')
}
}
}
checkRowForFour()
function checkColumnForFour(){
for(let i=0;i < 39;i++){
let columnofFour = [i,i+ width,i+width*2,i+width*3]
let decidedColor = squares[i].style.backgroundImage
let isBlank = squares[i].style.backgroundImage === ''
if(columnofFour.every(index=> squares[index].style.backgroundImage === decidedColor && !isBlank)){
score += 4
displayScore.innerHTML = score
columnofFour.forEach(index=> squares[index].style.backgroundImage = '')
}
}
}
checkColumnForFour()
window.setInterval(function(){
moveDown()
checkRowForFour()
checkColumnForFour()
checkRowForThree()
checkColumnForThree()
},100)
})