-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandy.js
More file actions
262 lines (244 loc) · 7.81 KB
/
Copy pathcandy.js
File metadata and controls
262 lines (244 loc) · 7.81 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
var candies = ["Blue", "Orange", "Green", "Yellow", "Red", "Purple"];
var board = [];
var row = 9;
var column = 9;
var score = 0;
var highestScore = localStorage.getItem("highestScore") || 0;
var gameDuration = 60; // Game duration in seconds
var gameTimeLeft = gameDuration; // Initialize game time left
var gameStarted = false; // Track if the game has started
var gameOver = false; // Track if the game is over
var currTile;
var otherTile;
let quitButton;
let scoreDisplay;
window.onload = function () {
scoreDisplay = document.getElementById("score");
startGame();
startTimer();
//1/10th of a second
window.setInterval(function () {
if (gameStarted && !gameOver) {
crushCandy();
}
}, 100);
quitButton = document.getElementById('quitButton');
quitButton.addEventListener('click', () => {
const confirmQuit = confirm('Are you sure you want to quit?');
if (confirmQuit) {
window.location.href = 'Front.html';
}
});
}
function startTimer() {
var timer = setInterval(function () {
if (gameTimeLeft <= 0) {
clearInterval(timer);
endGame();
} else {
gameTimeLeft--;
document.getElementById("time").innerText = gameTimeLeft;
}
}, 1000);
}
function randomCandy() {
return candies[Math.floor(Math.random() * candies.length)];
}
function startGame() {
// Initialize board with random candies
for (let r = 0; r < row; r++) {
let row = [];
for (let c = 0; c < column; c++) {
let tile = document.createElement("img");
tile.id = r.toString() + "-" + c.toString();
tile.src = "./Images/" + randomCandy() + ".png";
tile.addEventListener("dragstart", dragStart);
tile.addEventListener("dragover", dragOver);
tile.addEventListener("dragenter", dragEnter);
tile.addEventListener("dragleave", dragLeave);
tile.addEventListener("dragend", dragEnd);
tile.addEventListener("drop", dragDrop);
tile.addEventListener("dragend", dragEnd);
document.getElementById("board").append(tile);
row.push(tile);
}
board.push(row);
}
// Shuffle candies until there are no initial matches
while (checkValid()) {
shuffleBoard();
}
console.log(board);
}
function shuffleBoard() {
for (let r = 0; r < row; r++) {
for (let c = 0; c < column; c++) {
board[r][c].src = "./Images/" + randomCandy() + ".png";
}
}
}
function dragStart() {
if (gameOver) return;
//this refers to tile that was clicked on for dragging
currTile = this;
}
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
}
function dragLeave() {
}
function dragDrop() {
if (gameOver) return;
//this refers to the target tile that was dropped on
otherTile = this;
}
function dragEnd() {
if (currTile.src.includes("blank") || otherTile.src.includes("blank")) {
return;
}
let currCoords = currTile.id.split("-");//id="0-0" -> ["0","0"]
let r = parseInt(currCoords[0]);
let c = parseInt(currCoords[1]);
let otherCoords = otherTile.id.split("-");
let r2 = parseInt(otherCoords[0]);
let c2 = parseInt(otherCoords[1]);
let moveLeft = c2 == c - 1 && r == r2;
let moveRight = c2 == c + 1 && r == r2;
let moveUp = r2 == r - 1 && c == c2;
let moveDown = r2 == r + 1 && c == c2;
let isAdjacent = moveLeft || moveRight || moveUp || moveDown;
if (isAdjacent) {
let currImg = currTile.src;
let otherImg = otherTile.src;
currTile.src = otherImg;
otherTile.src = currImg;
let validMove = checkValid();
if (!validMove) {
let currImg = currTile.src;
let otherImg = otherTile.src;
currTile.src = otherImg;
otherTile.src = currImg;
} else {
gameStarted = true; // Game has started after the first move
}
}
}
function crushCandy() {
if (!gameOver) {
crushThree();
document.getElementById("score").innerText = score;
}
}
function crushThree() {
let candiesToRemove = []; // Store the candies to remove
for (let r = 0; r < row; r++) {
for (let c = 0; c < column - 2; c++) {
let candy1 = board[r][c];
let candy2 = board[r][c + 1];
let candy3 = board[r][c + 2];
if (candy1.src == candy2.src && candy2.src == candy3.src && !candy1.src.includes("blank")) {
candiesToRemove.push(candy1, candy2, candy3);
}
}
}
for (let c = 0; c < column; c++) {
for (let r = 0; r < row - 2; r++) {
let candy1 = board[r][c];
let candy2 = board[r + 1][c];
let candy3 = board[r + 2][c];
if (candy1.src == candy2.src && candy2.src == candy3.src && !candy1.src.includes("blank")) {
candiesToRemove.push(candy1, candy2, candy3);
}
}
}
// Remove matched candies and increase score
if (candiesToRemove.length > 0) {
score += candiesToRemove.length / 3; // Increase score for each set of matched candies
for (let candy of candiesToRemove) {
candy.src = "blank.png";
}
// Slide candies down
slideCandy();
// Generate new candies to fill empty spaces
generateCandy();
}
if (score > highestScore) {
highestScore = score;
localStorage.setItem("highestScore", highestScore);
console.log("New High Score:", highestScore); // Add this line
document.getElementById("highScore").innerText = highestScore;
}
// Update the score display
document.getElementById("score").innerText = score;
}
function slideCandy() {
for (let c = 0; c < column; c++) {
let ind = row - 1;
for (let r = row - 1; r >= 0; r--) {
if (!board[r][c].src.includes("blank")) {
board[ind][c].src = board[r][c].src;
ind -= 1;
}
}
for (let r = ind; r >= 0; r--) {
board[r][c].src = "blank.png";
}
}
}
function checkValid() {
for (let r = 0; r < row; r++) {
for (let c = 0; c < column - 2; c++) {
let candy1 = board[r][c];
let candy2 = board[r][c + 1];
let candy3 = board[r][c + 2];
if (candy1.src == candy2.src && candy2.src == candy3.src && !candy1.src.includes("blank")) {
return true;
}
}
}
for (let c = 0; c < column; c++) {
for (let r = 0; r < row - 2; r++) {
let candy1 = board[r][c];
let candy2 = board[r + 1][c];
let candy3 = board[r + 2][c];
if (candy1.src == candy2.src && candy2.src == candy3.src && !candy1.src.includes("blank")) {
return true;
}
}
}
return false;
}
function slideCandy() {
for (let c = 0; c < column; c++) {
let ind = row - 1;
for (let r = row - 1; r >= 0; r--) {
if (!board[r][c].src.includes("blank")) {
board[ind][c].src = board[r][c].src;
ind -= 1;
}
}
for (let r = ind; r >= 0; r--) {
board[r][c].src = "blank.png";
}
}
// Fill the top row with new candies
for (let c = 0; c < column; c++) {
if (board[0][c].src.endsWith("blank.png")) {
board[0][c].src = "./Images/" + randomCandy() + ".png";
}
}
}
function generateCandy() {
for (let c = 0; c < column; c++) {
if (board[0][c].src.endsWith("blank.png")) {
board[0][c].src = "./Images/" + randomCandy() + ".png";
}
}
}
function endGame() {
gameOver = true;
alert("Game Over! Your score: " + score);
}