1
+ < script >
1
2
document.addEventListener("DOMContentLoaded", () => {
2
3
candyCrushGame ( ) ;
3
4
} );
4
5
5
6
function candyCrushGame() {
6
- // DOM Elements
7
7
const grid = document . querySelector ( ".grid" ) ;
8
8
const scoreDisplay = document . getElementById ( "score" ) ;
9
9
const timerDisplay = document . getElementById ( "timer" ) ;
@@ -12,7 +12,6 @@ function candyCrushGame() {
12
12
const timedButton = document . getElementById ( "timedMode" ) ;
13
13
const changeModeButton = document . getElementById ( "changeMode" ) ;
14
14
15
- // Game State Variables
16
15
const width = 8 ;
17
16
const squares = [ ] ;
18
17
let score = 0 ;
@@ -30,10 +29,9 @@ function candyCrushGame() {
30
29
"url(https://raw.githubusercontent.com/arpit456jain/Amazing-Js-Projects/master/Candy%20Crush/utils/purple-candy.png)" ,
31
30
] ;
32
31
33
- // Create the Game Board
34
32
function createBoard ( ) {
35
- grid . innerHTML = "" ; // Clear existing grid
36
- squares . length = 0 ; // Clear squares array
33
+ grid . innerHTML = "" ;
34
+ squares . length = 0 ;
37
35
for ( let i = 0 ; i < width * width ; i ++ ) {
38
36
const square = document . createElement ( "div" ) ;
39
37
square . setAttribute ( "draggable" , true ) ;
@@ -43,73 +41,66 @@ function candyCrushGame() {
43
41
grid . appendChild ( square ) ;
44
42
squares . push ( square ) ;
45
43
}
46
-
47
- // Drag + Touch Event Listeners
44
+ // Desktop drag
45
+ squares . forEach ( square => square . addEventListener ( "dragstart" , dragStart ) ) ;
46
+ squares . forEach ( square => square . addEventListener ( "dragend" , dragEnd ) ) ;
47
+ squares . forEach ( square => square . addEventListener ( "dragover" , dragOver ) ) ;
48
+ squares . forEach ( square => square . addEventListener ( "dragenter" , dragEnter ) ) ;
49
+ squares . forEach ( square => square . addEventListener ( "dragleave" , dragLeave ) ) ;
50
+ squares . forEach ( square => square . addEventListener ( "drop" , dragDrop ) ) ;
51
+
52
+ // Mobile touch
53
+ let touchStartX = 0 , touchStartY = 0 ;
48
54
squares . forEach ( square => {
49
- // PC Drag Events
50
- square . addEventListener ( "dragstart" , dragStart ) ;
51
- square . addEventListener ( "dragend" , dragEnd ) ;
52
- square . addEventListener ( "dragover" , dragOver ) ;
53
- square . addEventListener ( "dragenter" , dragEnter ) ;
54
- square . addEventListener ( "dragleave" , dragLeave ) ;
55
- square . addEventListener ( "drop" , dragDrop ) ;
56
-
57
- // Mobile Touch Events
58
- square . addEventListener ( "touchstart" , function ( e ) {
59
- e . preventDefault ( ) ;
60
- colorBeingDragged = this . style . backgroundImage ;
61
- squareIdBeingDragged = parseInt ( this . id ) ;
55
+ square . addEventListener ( "touchstart" , e => {
56
+ touchStartX = e . touches [ 0 ] . clientX ;
57
+ touchStartY = e . touches [ 0 ] . clientY ;
58
+ colorBeingDragged = square . style . backgroundImage ;
59
+ squareIdBeingDragged = parseInt ( square . id ) ;
62
60
} ) ;
63
61
64
- square . addEventListener ( "touchmove" , function ( e ) {
65
- e . preventDefault ( ) ;
66
- const touch = e . touches [ 0 ] ;
67
- const target = document . elementFromPoint ( touch . clientX , touch . clientY ) ;
68
- if ( target && target . id ) {
69
- squareIdBeingReplaced = parseInt ( target . id ) ;
70
- colorBeingReplaced = target . style . backgroundImage ;
62
+ square . addEventListener ( "touchend" , e => {
63
+ let touchEndX = e . changedTouches [ 0 ] . clientX ;
64
+ let touchEndY = e . changedTouches [ 0 ] . clientY ;
65
+
66
+ let dx = touchEndX - touchStartX ;
67
+ let dy = touchEndY - touchStartY ;
68
+
69
+ if ( Math . abs ( dx ) > Math . abs ( dy ) ) {
70
+ // Horizontal
71
+ if ( dx > 0 ) squareIdBeingReplaced = squareIdBeingDragged + 1 ;
72
+ else squareIdBeingReplaced = squareIdBeingDragged - 1 ;
73
+ } else {
74
+ // Vertical
75
+ if ( dy > 0 ) squareIdBeingReplaced = squareIdBeingDragged + width ;
76
+ else squareIdBeingReplaced = squareIdBeingDragged - width ;
71
77
}
72
- } ) ;
73
78
74
- square . addEventListener ( "touchend" , function ( ) {
75
- if ( squareIdBeingReplaced != null ) {
79
+ if ( squareIdBeingReplaced >= 0 && squareIdBeingReplaced < width * width ) {
80
+ colorBeingReplaced = squares [ squareIdBeingReplaced ] . style . backgroundImage ;
76
81
squares [ squareIdBeingReplaced ] . style . backgroundImage = colorBeingDragged ;
77
82
squares [ squareIdBeingDragged ] . style . backgroundImage = colorBeingReplaced ;
78
- dragEnd ( ) ;
79
83
}
80
84
} ) ;
81
85
} ) ;
82
86
}
83
87
84
- // Drag and Drop Functions
85
88
let colorBeingDragged, colorBeingReplaced, squareIdBeingDragged, squareIdBeingReplaced;
86
89
87
90
function dragStart() {
88
91
colorBeingDragged = this . style . backgroundImage ;
89
92
squareIdBeingDragged = parseInt ( this . id ) ;
90
93
}
91
-
92
- function dragOver ( e ) {
93
- e . preventDefault ( ) ;
94
- }
95
-
96
- function dragEnter ( e ) {
97
- e . preventDefault ( ) ;
98
- }
99
-
100
- function dragLeave ( ) {
101
- // No action needed
102
- }
103
-
94
+ function dragOver(e) { e . preventDefault ( ) ; }
95
+ function dragEnter(e) { e . preventDefault ( ) ; }
96
+ function dragLeave() { }
104
97
function dragDrop() {
105
98
colorBeingReplaced = this . style . backgroundImage ;
106
99
squareIdBeingReplaced = parseInt ( this . id ) ;
107
100
this . style . backgroundImage = colorBeingDragged ;
108
101
squares [ squareIdBeingDragged ] . style . backgroundImage = colorBeingReplaced ;
109
102
}
110
-
111
103
function dragEnd() {
112
- // Define valid moves
113
104
let validMoves = [
114
105
squareIdBeingDragged - 1 ,
115
106
squareIdBeingDragged - width ,
@@ -128,7 +119,6 @@ function candyCrushGame() {
128
119
}
129
120
}
130
121
131
- // Move Candies Down
132
122
function moveIntoSquareBelow ( ) {
133
123
for ( let i = 0 ; i < width ; i ++ ) {
134
124
if ( squares [ i ] . style . backgroundImage === "" ) {
@@ -144,13 +134,12 @@ function candyCrushGame() {
144
134
}
145
135
}
146
136
147
- // Check for Matches
148
137
function checkRowForFour ( ) {
149
138
for ( let i = 0 ; i < 60 ; i ++ ) {
150
139
if ( i % width >= width - 3 ) continue ;
151
140
let rowOfFour = [ i , i + 1 , i + 2 , i + 3 ] ;
152
141
let decidedColor = squares [ i ] . style . backgroundImage ;
153
- const isBlank = decidedColor === "" ;
142
+ const isBlank = squares [ i ] . style . backgroundImage === "" ;
154
143
if ( rowOfFour . every ( index => squares [ index ] . style . backgroundImage === decidedColor && ! isBlank ) ) {
155
144
score += 4 ;
156
145
scoreDisplay . innerHTML = score ;
@@ -163,7 +152,7 @@ function candyCrushGame() {
163
152
for ( let i = 0 ; i < 40 ; i ++ ) {
164
153
let columnOfFour = [ i , i + width , i + 2 * width , i + 3 * width ] ;
165
154
let decidedColor = squares [ i ] . style . backgroundImage ;
166
- const isBlank = decidedColor === "" ;
155
+ const isBlank = squares [ i ] . style . backgroundImage === "" ;
167
156
if ( columnOfFour . every ( index => squares [ index ] . style . backgroundImage === decidedColor && ! isBlank ) ) {
168
157
score += 4 ;
169
158
scoreDisplay . innerHTML = score ;
@@ -177,7 +166,7 @@ function candyCrushGame() {
177
166
if ( i % width >= width - 2 ) continue ;
178
167
let rowOfThree = [ i , i + 1 , i + 2 ] ;
179
168
let decidedColor = squares [ i ] . style . backgroundImage ;
180
- const isBlank = decidedColor === "" ;
169
+ const isBlank = squares [ i ] . style . backgroundImage === "" ;
181
170
if ( rowOfThree . every ( index => squares [ index ] . style . backgroundImage === decidedColor && ! isBlank ) ) {
182
171
score += 3 ;
183
172
scoreDisplay . innerHTML = score ;
@@ -190,7 +179,7 @@ function candyCrushGame() {
190
179
for ( let i = 0 ; i < 48 ; i ++ ) {
191
180
let columnOfThree = [ i , i + width , i + 2 * width ] ;
192
181
let decidedColor = squares [ i ] . style . backgroundImage ;
193
- const isBlank = decidedColor === "" ;
182
+ const isBlank = squares [ i ] . style . backgroundImage === "" ;
194
183
if ( columnOfThree . every ( index => squares [ index ] . style . backgroundImage === decidedColor && ! isBlank ) ) {
195
184
score += 3 ;
196
185
scoreDisplay . innerHTML = score ;
@@ -199,7 +188,6 @@ function candyCrushGame() {
199
188
}
200
189
}
201
190
202
- // Game Loop
203
191
function gameLoop ( ) {
204
192
checkRowForFour ( ) ;
205
193
checkColumnForFour ( ) ;
@@ -208,7 +196,6 @@ function candyCrushGame() {
208
196
moveIntoSquareBelow ( ) ;
209
197
}
210
198
211
- // Start the Game
212
199
function startGame ( mode ) {
213
200
currentMode = mode ;
214
201
modeSelection . style . display = "none" ;
@@ -253,9 +240,7 @@ function candyCrushGame() {
253
240
254
241
function changeMode ( ) {
255
242
clearInterval ( gameInterval ) ;
256
- if ( currentMode === "timed" ) {
257
- clearInterval ( timerInterval ) ;
258
- }
243
+ if ( currentMode === "timed" ) clearInterval ( timerInterval ) ;
259
244
grid . style . display = "none" ;
260
245
scoreDisplay . parentElement . style . display = "none" ;
261
246
modeSelection . style . display = "flex" ;
@@ -265,3 +250,4 @@ function candyCrushGame() {
265
250
timedButton . addEventListener ( "click ", ( ) => startGame ( "timed ") ) ;
266
251
changeModeButton . addEventListener ( "click ", changeMode ) ;
267
252
}
253
+ </script >
0 commit comments