-
Notifications
You must be signed in to change notification settings - Fork 0
/
Universe.pde
613 lines (465 loc) · 13 KB
/
Universe.pde
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/**
* This class represents the game board with all the handling of the internal logic.
*/
public class Universe
{
/**
* Corners of the game board in the window.
*/
int x, y, height, width;
/**
* Number of rows of bubbles.
*/
int rows;
/**
* Number of columns of bubbles.
*/
int cols;
/**
* Bubbles storage.
*/
Bubble[][] bubbles;
/**
* Amount of shots fired by the user.
*/
int shots;
/**
* Amount of bubbles exploded by the user.
*/
int exploded;
/**
* The location of the cursor (selection) controled
* by keyboard arrows and on mouse over.
*/
Location cursor;
/**
* Flag that shows the hint mode.
*/
boolean hintMode;
/**
* Flag that shows if the game has ended.
*/
boolean gameOver;
/**
* Constructs a new Universe.
*
* @Param rows Number of rows of bubbles.
* @Param cols Number of cols of bubbles.
* @Param x X coord. of the upper-left corner of the board in the window.
* @Param y Y coord. of the upper-left corder of the board in the window.
* @Param width Width of the board in the window.
* @Param height Height of the board in the window.
*/
public Universe(int rows, int cols, int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.rows = rows;
this.cols = cols;
this.reset();
}
/**
* Sets the game to initial conditions.
*/
public void reset()
{
this.shots = 0;
this.exploded = 0;
this.hintMode = false;
this.gameOver = false;
this.bubbles = new Bubble[rows][cols];
this.fillWithRandomCoins();
this.cursor = new Location(0, 0);
}
/**
* Check if the game is in hint mode or not.
*
* @Return true if it is in hint mode, false otherwise.
*/
public boolean isHintMode()
{
return this.hintMode;
}
/**
* Check if the game has ended.
*
* @Return true if the game is finished, false otherwise.
*/
public boolean isGameOver()
{
return this.gameOver;
}
/**
* Fill the board with random coins.
*/
private void fillWithRandomCoins()
{
for(int r=0; r<rows; r++)
for(int c=0; c<cols; c++)
{
// There are 9 different types of coins defined in the game.
int type = int(random(1, 9 +1));
bubbles[r][c] = new Bubble(type, this.width / this.cols, this.height / this.rows);
}
}
/**
* Draw the game board in the window.
*/
public void draw()
{
for(int r=0; r<rows; r++)
for(int c=0; c<cols; c++)
{
// Determine if the current place is selected by the cursor and draw it.
boolean isCursor = (this.cursor.row == r && this.cursor.col == c);
if(isCursor)
{
fill(color(255, 245, 111));
stroke(color(255, 245, 111));
rect(this.cursor.col * this.width / this.cols + this.x, this.cursor.row * this.height / this.rows + this.y, this.width / this.cols, this.height / this.rows);
}
// If there is a bubble in the current place.
if(bubbles[r][c] != null)
{
// Draw the bubble.
bubbles[r][c].draw(c * this.width / this.cols + this.x, r * this.height / this.rows + this.y);
}
}
}
/**
* Check if a hit made on the window is in the game board or not.
*
* @Param x X coord. of the hit.
* @Param y Y coord. of the hit.
* @Return The Location object with the row/column information of the hit if it was in, null otherwise.
*/
public Location isHitIn(int x, int y)
{
// Check if the hit was in the board.
if (x >= this.x &&
y >= this.y &&
x <= this.x + this.width &&
y <= this.y + this.height)
{
int col = (x - this.x) / (this.width / this.cols);
int row = (y - this.y) / (this.height / this.rows);
return new Location(row, col);
}
// Otherwise.
return null;
}
/**
* Remove the selection of all the bubbles of the board.
*/
private void cleanAllBubbleSelections()
{
for(int r=0; r<rows; r++)
for(int c=0; c<cols; c++)
if(bubbles[r][c] != null)
bubbles[r][c].setSelection(false);
}
/**
* Explode the currently selected bubbles of the board.
*
* @Return The count of bubbles exploded.
*/
private int explodeSelectedBubbles()
{
int count = 0;
for(int r=0; r<rows; r++)
for(int c=0; c<cols; c++)
if(bubbles[r][c] != null)
if(bubbles[r][c].isSelected())
{
count ++;
bubbles[r][c] = null; // Explode!
}
return count;
}
/**
* Make the bubbles fall when they have empty spaces below after an explosion.
*/
private void makeExplodedBubblesFall()
{
for(int c=0; c<cols; c++)
{
int emptySpace = -1;
for(int r=rows-1; r>=0; r--)
{
// Find an empty space.
if(bubbles[r][c] == null)
emptySpace = r;
// Find a non empty space.
if(emptySpace != -1 &&
bubbles[r][c] != null)
{
// Move the bubble to compact the column.
bubbles[emptySpace][c] = bubbles[r][c];
bubbles[r][c] = null;
r = min(emptySpace + 2, rows);
emptySpace = -1;
}
}
}
}
/**
* Check if a column of the board is empty of bubbles.
*
* @Param column The index of the column to check.
* @Return true if the column is empty, false otherwise.
*/
private boolean isColumnEmpty(int column)
{
for(int r=rows-1; r>=0; r--)
{
if(bubbles[r][column] != null)
return false;
}
return true;
}
/**
* Move the bubbles from one column to another.
*
* @Param from The index of the source column.
* @Param to The index of the target column.
*/
private void moveBubblesColumn(int from, int to)
{
for(int r=0; r<rows; r++)
{
bubbles[r][to] = bubbles[r][from];
bubbles[r][from] = null;
}
}
/**
* Move horizontally the columns of bubbles to compact the board
* when there are empty columns after an explosion.
*/
private void collapseEmptyColumns()
{
int emptyCol = -1;
for(int c=0; c<this.cols; c++)
{
// Find an empty column.
if(this.isColumnEmpty(c))
{
if(emptyCol == -1) // undefined
emptyCol = c;
}
else
{
// Find a non empty column.
if(emptyCol != -1) // defined
{
// Move the bubbles from the non empty column to fill the gap.
this.moveBubblesColumn(c, emptyCol);
c = emptyCol;
emptyCol = -1;
}
}
}
}
/**
* Select the adjacent bubbles of the same type around a specific bubble.
*
* This method is call recusively to backtrack the surrounding bubbles of
* the same type in the four valid directions.
*
* @Param row The index of the row of the initial bubble.
* @Param col The index of the column of the initial bubble.
* @Param type The type of the initial bubble.
* @Return The number of selected bubbles. Zero if the location was invalid
* and 1 if the bubble has not surrounding bubbles of the same type.
*/
private int makeBubbleSelection(int row, int col, int type)
{
// Check the position of the target bubble.
if(!isValidLocation(row, col))
return 0;
// Check that there IS a bubble in this place.
if(bubbles[row][col] == null)
return 0;
// Check that it is not already selected to avoid infinite loops.
if(bubbles[row][col].isSelected())
return 0;
// Check that it has the correct type.
if(bubbles[row][col].getType() != type)
return 0;
// Prepare the direction arrays.
int[] kr = {
0, 1, 0, -1
};
int[] kc = {
1, 0, -1, 0
};
// All seems to be fine, select the current bubble.
bubbles[row][col].setSelection(true);
int count = 1;
// Call recusively to check its neighbours in the valid directions.
for(int i=0; i<4; i++)
{
int nr = row + kr[i];
int nc = col + kc[i];
count = count + this.makeBubbleSelection(nr, nc, type);
}
return count;
}
/**
* Check is a location is valid in the game board.
*
* @Param row Index of the row of the location to test.
* @Param col Index of the column of the location to test.
* @Return true if the location is valid in the board, false otherwise.
*/
private boolean isValidLocation(int row, int col)
{
if(row < 0 || row >= this.rows ||
col < 0 || col >= this.cols)
return false;
return true;
}
/**
* Make a hit in the board by the user.
*
* This method is call indistinctly of the source: keyboard or mouse.
* Also this method adds to the shot/exploded counters according with
* the results of the hit.
*
* @Param row Index of the row of the hit.
* @Param col Index of the column of the hit.
* @Return true if the hit was successful, false otherwise.
*/
public boolean hit(int row, int col)
{
// Check if the location of the hit is valid.
if(!isValidLocation(row, col))
return false;
// Check if there IS a bubble in that location.
if(bubbles[row][col] == null)
return false;
// Check the board is NOT in hint mode.
if(this.isHintMode())
return false;
// If the bubble was already selected: its explode time!
if(bubbles[row][col].isSelected())
{
// This is really a shot.
this.shots ++;
// Explode the selected bubbles.
this.exploded += this.explodeSelectedBubbles();
// Compact the board vertically.
this.makeExplodedBubblesFall();
// Compact the board horizontally.
this.collapseEmptyColumns();
// Check if there are more moves or if the game has ended.
if(this.toggleHints(false) == 0)
this.gameOver = true;
}
else // The bubble is not selected, so its just selection time!
{
// Clear any previous selection of the bubbles.
this.cleanAllBubbleSelections();
// Make the selection based on the current bubble.
int count = this.makeBubbleSelection(row, col, bubbles[row][col].getType());
// If there was only one selection means that there are not surrounding
// friends of the bubble, undo the selection to avoid only-one-selected-bubble.
if(count == 1)
bubbles[row][col].setSelection(false);
}
return true;
}
/**
* Handle the keyboard events related to the game board.
*
* - SPACE: hit the currently selected bubble.
* - Arrow keys: move the cursor.
*
* @Param key The key code pressed in the keyboard.
*/
public void handleCursor(int key)
{
switch(keyCode)
{
case KeyEvent.VK_SPACE:
this.hit(cursor.row, cursor.col);
break;
case UP:
this.moveCursor(-1, 0);
break;
case DOWN:
this.moveCursor(1, 0);
break;
case LEFT:
this.moveCursor(0, -1);
break;
case RIGHT:
this.moveCursor(0, 1);
break;
}
}
/**
* Move the cursor the amount of rows/columns specified.
*
* @Param rows Number of rows to move the cursor.
* @Param cols Number of columns to move the cursor.
* @Return true if the cursor was successfully moved, false otherwise.
*/
private boolean moveCursor(int rows, int cols)
{
// Check if the new location of the cursor is valid.
if(this.isValidLocation(cursor.row + rows, cursor.col + cols))
{
// Move the cursor.
this.cursor = new Location(cursor.row + rows, cursor.col + cols);
return true;
}
return false;
}
/**
* Toggle the hint mode.
*
* In hint mode the board shows the available moves but it does not
* receive shots.
*
* @Param show true If the moves will be shown to the user, false to handle it internally.
* @Return The number of bubbles that can be exploded.
*/
public int toggleHints(boolean show)
{
// Change the hint modes flag.
this.hintMode = !this.hintMode;
// If setting the hint mode off.
if(!this.hintMode)
{
// Clear any bubble selection done previously.
this.cleanAllBubbleSelections();
return 0;
}
int total = 0;
// Check each position of the board.
for(int c=0; c<cols; c++)
{
for(int r=0; r<rows; r++)
{
int count = 0;
// Check the selection of the bubbles (ignore empty spaces).
if(bubbles[r][c] != null)
count = this.makeBubbleSelection(r, c, bubbles[r][c].getType());
// Avoid bubbles with no surrounding similars.
if(count == 1)
{
bubbles[r][c].setSelection(false);
}
else
total += count;
}
}
// If its for internal handling, remove the hint mode inmediate.
if(!show)
this.toggleHints(false);
return total;
}
} // End of Universe class.
//////////////////////////////////////////////////