-
Notifications
You must be signed in to change notification settings - Fork 0
/
candy.js
204 lines (168 loc) · 5.5 KB
/
candy.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
var candies = ["Blue", "Orange", "Green", "Yellow", "Red", "Purple"];
var board = [];
var rows = 9;
var columns = 9;
var score = 0;
var currTile;
var otherTile;
window.onload = function() {
startGame();
//1/10th of a second
window.setInterval(function(){
crushCandy();
slideCandy();
generateCandy();
}, 100);
}
function randomCandy() {
return candies[Math.floor(Math.random() * candies.length)]; //0 - 5.99
}
function startGame() {
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < columns; c++) {
// <img id="0-0" src="./images/Red.png">
let tile = document.createElement("img");
tile.id = r.toString() + "-" + c.toString();
tile.src = "./images/" + randomCandy() + ".png";
//DRAG FUNCTIONALITY
tile.addEventListener("dragstart", dragStart); //click on a candy, initialize drag process
tile.addEventListener("dragover", dragOver); //clicking on candy, moving mouse to drag the candy
tile.addEventListener("dragenter", dragEnter); //dragging candy onto another candy
tile.addEventListener("dragleave", dragLeave); //leave candy over another candy
tile.addEventListener("drop", dragDrop); //dropping a candy over another candy
tile.addEventListener("dragend", dragEnd); //after drag process completed, we swap candies
document.getElementById("board").append(tile);
row.push(tile);
}
board.push(row);
}
console.log(board);
}
function dragStart() {
//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() {
//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;
}
}
}
function crushCandy() {
//crushFive();
//crushFour();
crushThree();
document.getElementById("score").innerText = score;
}
function crushThree() {
//check rows
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns-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")) {
candy1.src = "./images/blank.png";
candy2.src = "./images/blank.png";
candy3.src = "./images/blank.png";
score += 30;
}
}
}
//check columns
for (let c = 0; c < columns; c++) {
for (let r = 0; r < rows-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")) {
candy1.src = "./images/blank.png";
candy2.src = "./images/blank.png";
candy3.src = "./images/blank.png";
score += 30;
}
}
}
}
function checkValid() {
//check rows
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns-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;
}
}
}
//check columns
for (let c = 0; c < columns; c++) {
for (let r = 0; r < rows-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 < columns; c++) {
let ind = rows - 1;
for (let r = columns-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 = "./images/blank.png";
}
}
}
function generateCandy() {
for (let c = 0; c < columns; c++) {
if (board[0][c].src.includes("blank")) {
board[0][c].src = "./images/" + randomCandy() + ".png";
}
}
}