-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathboard.js
363 lines (321 loc) · 8.74 KB
/
board.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Copyright (c) 2017 MIT 6.813/6.831 course staff, all rights reserved.
* Redistribution of original or derived work requires permission of course staff.
*/
/**
* Board represents the state of the candy-board. A candy-board is a square
* array of squares. The candy-board can be any size.
*
* Each square of the candy-board contains exactly one candy.
* Each square is identified by its row and column, numbered from 0 to
* size-1. Square [0,0] is in the upper-left corner of the candy-board.
* Rows are numbered downward, and columns are numbered to the right.
* The candy type on each square is random.
*
* Candies are mutable: a candy can be added, removed, and moved.
* (The size of a board is immutable, however.)
*
* The board broadcasts four event types: "add",
* "remove", "move", and "scoreUpdate".
*/
var Board = function(size)
{
// A unique ID for each candy.
var candyCounter = 0;
// Score, one point per candy crushed.
this.score = 0;
// boardSize is number of squares on one side of candy-board
this.boardSize = size;
// square is a two dimensional array representating the candyboard
// square[row][col] is the candy in that square, or null if square is empty
this.square = new Array(this.boardSize);
// make an empty candyboard
for (var i = 0; i <= this.boardSize; i++)
{
this.square[i] = [];
}
/*
* Returns true/false depending on whether row and column
* identify a valid square on the board.
*/
this.isValidLocation = function(row, col)
{
return (row >= 0 && col >= 0 &&
row <= this.boardSize && col <= this.boardSize &&
row == Math.round(row) && col == Math.round(col));
}
/*
* Returns true/false depending on whether the
* square at [row,col] is empty (does not contain a candy).
*/
this.isEmptyLocation = function(row, col)
{
if (this.getCandyAt(row, col)) {
return false;
}
return true;
}
////////////////////////////////////////////////
// Public methods
//
/*
* Perform an a valid move automatically on the board. Flips the
* appropriate candies, but does not crush the candies.
*/
this.doAutoMove = function() {
var move = rules.getRandomValidMove();
var toCandy = board.getCandyInDirection(move.candy, move.direction);
this.flipCandies(move.candy,toCandy);
}
/*
* Returns the number of squares on each side of the board
*/
this.getSize = function()
{
return this.boardSize;
}
/**
* Get the candy found on the square at [row,column], or null
* if the square is empty. Requires row,column < size.
*/
this.getCandyAt = function(row, col)
{
if (this.isValidLocation(row,col))
{
return this.square[row][col];
}
}
/**
* Get location of candy (row and column) if it's found on this
* board, or null if not found.
*/
this.getLocationOf = function(candy){
return {row:candy.row, col:candy.col};
}
/**
* Get a list of all candies on the board, in no particular order.
*/
this.getAllCandies = function(){
var results = [];
for (var r in this.square) {
for (var c in this.square[r]) {
if (this.square[r][c]) {
results.push(this.square[r][c]);
}
}
}
return results;
}
/*
* Add a new candy to the board. Requires candies to be not currently
* on the board, and (row,col) must designate a valid empty square.
*
* The optional spawnRow, spawnCol indicate where the candy
* was "spawned" the moment before it moved to row, col. This location,
* which may be off the board, is added to the 'add' event and
* can be used to animate new candies that are coming in from offscreen.
*/
this.add = function(candy, row, col, spawnRow, spawnCol)
{
if (this.isEmptyLocation(row, col))
{
var details = {
candy: candy,
toRow: row,
toCol: col,
fromRow: spawnRow,
fromCol: spawnCol
};
candy.row = row;
candy.col = col;
this.square[row][col] = candy;
$(this).triggerHandler("add", details);
}
else
{
console.log("add already found a candy at " + row + "," + col);
}
}
/**
* Move a candy from its current square to another square.
* Requires candy to be already found on this board, and (toRow,toCol)
* must denote a valid empty square.
*/
this.moveTo = function(candy, toRow, toCol)
{
if (this.isEmptyLocation(toRow,toCol))
{
var details = {
candy:candy,
toRow:toRow,
toCol:toCol,
fromRow:candy.row,
fromCol:candy.col};
delete this.square[candy.row][candy.col];
this.square[toRow][toCol] = candy;
candy.row = toRow;
candy.col = toCol;
$(this).triggerHandler("move", details);
}
}
/**
* Remove a candy from this board.
* Requires candy to be found on this board.
*/
this.remove = function(candy)
{
var details = {
candy: candy,
fromRow: candy.row,
fromCol: candy.col
};
delete this.square[candy.row][candy.col];
candy.row = candy.col = null;
$(this).triggerHandler("remove", details);
}
/**
* Remove a candy at a given location from this board.
* Requires candy to be found on this board.
*/
this.removeAt = function(row, col)
{
if (this.isEmptyLocation(row, col))
{
console.log("removeAt found no candy at " + row + "," + col);
}
else
{
this.remove(this.square[row][col]);
}
}
/**
* Remove all candies from board.
*/
this.clear = function() {
for (var r in this.square)
{
for (var c in this.square[r])
{
if (this.square[r][c])
{
this.removeAt(r, c);
}
}
}
}
////////////////////////////////////////////////
// Utilities
//
/*
Adds a candy of specified color to row, col.
*/
this.addCandy = function(color, row, col, spawnRow, spawnCol)
{
var candy = new Candy(color, candyCounter++);
this.add(candy, row, col, spawnRow, spawnCol);
}
/**
* Adds a candy of random color at row, col.
*/
this.addRandomCandy = function(row, col, spawnRow, spawnCol)
{
var random_color = Math.floor(Math.random() * Candy.colors.length);
var candy = new Candy(Candy.colors[random_color], candyCounter++);
this.add(candy, row, col, spawnRow, spawnCol);
}
/*
Returns the candy immediately in the direction specified by direction
['up', 'down', 'left', 'right'] from the candy passed as fromCandy
*/
this.getCandyInDirection = function(fromCandy, direction)
{
switch(direction)
{
case "up": {
return this.getCandyAt(fromCandy.row-1, fromCandy.col);
}
case "down": {
return this.getCandyAt(fromCandy.row+1, fromCandy.col);
}
case "left": {
return this.getCandyAt(fromCandy.row, fromCandy.col-1);
}
case "right": {
return this.getCandyAt(fromCandy.row, fromCandy.col+1);
}
}
}
/* Flip candy1 with candy2 in one step, firing two move events.
* Does not verify the validity of the flip. Does not crush candies
* produced by flip. */
this.flipCandies = function(candy1, candy2)
{
// Swap the two candies simultaneously.
var details1 = {
candy: candy1,
toRow: candy2.row,
toCol: candy2.col,
fromRow: candy1.row,
fromCol: candy1.col
};
var details2 = {
candy: candy2,
toRow: candy1.row,
toCol: candy1.col,
fromRow: candy2.row,
fromCol: candy2.col
};
candy1.row = details1.toRow;
candy1.col = details1.toCol;
this.square[details1.toRow][details1.toCol] = candy1;
candy2.row = details2.toRow;
candy2.col = details2.toCol;
this.square[details2.toRow][details2.toCol] = candy2;
// Trigger two move events.
$(this).triggerHandler("move", details1);
$(this).triggerHandler("move", details2);
}
/*
* Resets the score
*/
this.resetScore = function() {
this.score = 0;
$(this).triggerHandler("scoreUpdate", [{score: 0}]);
}
/*
* Adds some score.
*/
this.incrementScore = function(candy, row, col) {
this.score += 1;
$(this).triggerHandler("scoreUpdate", [{
score: this.score,
candy: candy,
row: row,
col: col
}]);
}
/*
* Gets the current score
*/
this.getScore = function() {
return this.score;
}
/**
* Get a string representation for the board as a multiline matrix.
*/
this.toString = function()
{
var result = "";
for (var r = 0; r < this.boardSize; ++r) {
for (var c = 0; c < this.boardSize; ++c) {
var candy = this.square[r][c];
if (candy) {
result += candy.toString().charAt(0) + " ";
}else {
result += "_ ";
}
}
result += "<br/>";
}
return result.toString();
}
}