-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCandyBoard.cpp
1643 lines (1555 loc) · 66 KB
/
CandyBoard.cpp
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef BASIC_LIB
#define BASIC_LIB
#include <SDL.h>
#include <SDL_image.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <time.h>
#include <vector>
#include <queue>
#endif // BASIC_LIB
#ifndef CANDY_BOARD_H
#define CANDY_BOARD_H
#include "CandyBoard.h"
#endif // CANDY_BOARD_H
#ifndef HEADER_H
#define HEADER_H 1
#include "Headers.h"
#endif // HEADER_H
#ifndef ANIMATION_H
#define ANIMATION_H 1
#include "Animation.h"
#endif // ANIMATION_H
CandyBoard::CandyBoard(int rows, int columns):
LEAST_POSSIBLE_MOVES(5)
{
srand(time(0));
candies.assign(rows, std::vector<Candy>());
for(int i = 0; i < rows; i++)
candies[i].assign(columns, Candy());
mRows = rows;
mColumns = columns;
mWidth = mRows * Candy::CandyWidth;
mHeight = mColumns * Candy::CandyHeight;
// 20 is for that it does not touch the bottom
mX = std::min(SCREEN_WIDTH / 3, SCREEN_WIDTH - mWidth - 20);
mY = SCREEN_HEIGHT - mHeight - 20;
last_candy_choosen = {-1, -1};
multiplier = 1;
mCascadeCounter = 0;
generateRandomBoard();
printf("Candy board constructed.\n");
}
CandyBoard::~CandyBoard()
{
boardTexture.free();
}
void CandyBoard::handleEvent( SDL_Event* e )
{
if(e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP || e->type == SDL_MOUSEMOTION)
{
//Get mouse position
int x, y;
int mouse_state = SDL_GetMouseState( &x, &y );
// printf("Mouse in X:%d Y:%d and mX %d mY %d\n", x, y, mX, mY);
// determine which candy it is
if(x >= mX && x <= mX + mWidth && y >= mY && y <= mY + mHeight)
{
switch( e->type )
{
case SDL_MOUSEBUTTONDOWN:
{
// first candy to touch
if(last_candy_choosen.x == -1 && last_candy_choosen.y == -1)
{
last_candy_choosen = {x, y};
printf("Last choose: X %d Y %d\n", x, y);
}
break;
}
case SDL_MOUSEBUTTONUP:
printf("Choose X %d y %d\n", x, y);
// if outside the board
if(x < mX || y < mY || x > mX + mWidth || y > mY + mHeight)
{
last_candy_choosen = {-1, -1};
ANIMATION_MODE = false;
break;
}
// choose a candy
if(last_candy_choosen.x == x && last_candy_choosen.y == y)
{
SDL_RenderClear(gRenderer);
renderFrame();
displayCandyBoard(gRenderer);
// set render draw color to Orange
SDL_SetRenderDrawColor(gRenderer, 255, 165, 0, 255);
SDL_Rect rect = {mX + getPositionX(x) * Candy::CandyWidth,
mY + getPositionY(y) * Candy::CandyHeight,
Candy::CandyWidth,
Candy::CandyHeight
};
SDL_RenderDrawRect(gRenderer, &rect);
SDL_RenderPresent(gRenderer);
ANIMATION_MODE = true;
break;
}
if(last_candy_choosen.x != -1 && last_candy_choosen.y != -1 && x != last_candy_choosen.x && y != last_candy_choosen.y)
{
if(abs(getPositionX(last_candy_choosen.x) - getPositionX(x)) + abs(getPositionY(last_candy_choosen.y) - getPositionY(y)) >= 2
&& abs(getPositionX(last_candy_choosen.x) - getPositionX(x)) + abs(getPositionY(last_candy_choosen.y) - getPositionY(y)) != 0)
{
last_candy_choosen = {x, y};
ANIMATION_MODE = false;
break;
}
// swap
// meaning that it is adjacent cell and not itself
int ddx = x - last_candy_choosen.x;
int ddy = y - last_candy_choosen.y;
// if its horizontal (ddx > ddy)
x = last_candy_choosen.x + (abs(ddx) / ddx) * (abs(ddx) > abs(ddy)) * Candy::CandyWidth;
// else if its vertical
y = last_candy_choosen.y + (abs(ddy) / ddy) * (abs(ddy) >= abs(ddx)) * Candy::CandyHeight;
printf("Next choose: X %d Y %d\n", x, y);
if(abs(getPositionX(last_candy_choosen.x) - getPositionX(x)) + abs(getPositionY(last_candy_choosen.y) - getPositionY(y)) <= 1
&& abs(getPositionX(last_candy_choosen.x) - getPositionX(x)) + abs(getPositionY(last_candy_choosen.y) - getPositionY(y)) != 0)
{
Candy* last_candy = getCandyCoord(last_candy_choosen.x, last_candy_choosen.y);
Candy* current_candy = getCandyCoord(x, y);
CandyBreed tmp = last_candy->getBreed();
// swapping effect
// render to create effect
Candy tmp_last_candy = *(last_candy);
Candy tmp_current_candy = *(current_candy);
// caculate the difference in coordinate between two candy
int dx = tmp_current_candy.getX() - tmp_last_candy.getX();
int dy = tmp_current_candy.getY() - tmp_last_candy.getY();
// if horizontally
if(dx)
{
for(int i = 0; i < abs(dx); i+=10)
{
// i * (abs(dx) / dx) == might be the RIGHT side
SDL_RenderClear(gRenderer);
renderFrame();
for(int i = 0; i < mRows; i ++)
for(int j = 0; j < mColumns; j++)
{
if(getCandy(i,j) != last_candy && getCandy(i, j) != current_candy)
getCandy(i ,j)->render(gRenderer);
}
tmp_last_candy.render(gRenderer, tmp_last_candy.getX() + i * (abs(dx) / dx), tmp_last_candy.getY());
tmp_current_candy.render(gRenderer, tmp_current_candy.getX() - i * (abs(dx) / dx), tmp_last_candy.getY());
SDL_RenderPresent(gRenderer);
//delay(100);
}
}
// else if vertically
else if(dy)
{
for(int i = 0;i < abs(dy); i+= 10)
{
SDL_RenderClear(gRenderer);
renderFrame();
for(int i = 0; i < mRows; i++)
for(int j = 0; j < mColumns; j++)
{
if(getCandy(i,j) != last_candy && getCandy(i, j) != current_candy)
getCandy(i ,j)->render(gRenderer);
}
tmp_last_candy.render(gRenderer, tmp_last_candy.getX(), tmp_last_candy.getY() + i * (abs(dy) / dy));
tmp_current_candy.render(gRenderer, tmp_current_candy.getX(), tmp_current_candy.getY() - i * (abs(dy) / dy));
SDL_RenderPresent(gRenderer);
//delay(100);
}
}
// swap after effect
last_candy->setBreed(current_candy->getBreed());
current_candy->setBreed(tmp);
// play swap sound
soundEffect[NORMAL_SWITCH].play();
// reset multiplier
multiplier = 1;
// reset cascade counter
mCascadeCounter = 0;
// if one of them is color bomb
if(last_candy->getBreed() == COLOUR_BOMB)
{
gMoves -= 1;
last_candy->setBreed(EMPTY);
deleteByColourBomb(current_candy);
}
else if(current_candy->getBreed() == COLOUR_BOMB)
{
gMoves -= 1;
current_candy->setBreed(EMPTY);
deleteByColourBomb(last_candy);
}
else
{
// check if valid move
bool last_candy_match = false;
// -1 to avoid count this twice
// calculate the number of match after swapping
int last_candy_horizontal_match = countMatchInDirectionCoord(last_candy_choosen.x, last_candy_choosen.y, LEFT)
+ countMatchInDirectionCoord(last_candy_choosen.x, last_candy_choosen.y, RIGHT) - 1;
int last_candy_vertical_match = countMatchInDirectionCoord(last_candy_choosen.x, last_candy_choosen.y, UP)
+ countMatchInDirectionCoord(last_candy_choosen.x, last_candy_choosen.y, DOWN) - 1;
last_candy_match |= (last_candy_vertical_match >= 3);
last_candy_match |= (last_candy_horizontal_match >= 3);
printf("Last: ver %d hor %d\n",last_candy_vertical_match, last_candy_horizontal_match);
bool current_candy_match = false;
int current_candy_horizontal_match = countMatchInDirectionCoord(x, y, LEFT)
+ countMatchInDirectionCoord(x, y, RIGHT) - 1;
int current_candy_vertical_match = countMatchInDirectionCoord(x, y, UP)
+ countMatchInDirectionCoord(x, y, DOWN) - 1;
current_candy_match |= (current_candy_vertical_match >= 3);
current_candy_match |= (current_candy_horizontal_match >= 3);
printf("Current: ver %d hor %d\n",current_candy_vertical_match, current_candy_horizontal_match);
// if there is no match after swapping
if( !last_candy_match && !current_candy_match)
{
current_candy->setBreed(last_candy->getBreed());
last_candy->setBreed(tmp);
// play swap back sound
soundEffect[NEGATIVE_SWITCH].play();
}
//
else
{
// decrease the moves number
gMoves -= 1;
// delete the match
// normal candy is used in case there is both ver and hor match but the before set the candy to EMPTY
CandyBreed normal_last_type = (CandyBreed) ((int)getCandyCoord(last_candy_choosen.x, last_candy_choosen.y)->getBreed() - (int) getCandyCoord(last_candy_choosen.x, last_candy_choosen.y)->getType());
CandyBreed last_type = getCandyCoord(last_candy_choosen.x, last_candy_choosen.y)->getBreed();
CandyBreed normal_cur_type = (CandyBreed) ((int) getCandyCoord(x, y)->getBreed() - (int) getCandyCoord(x, y)->getType());
CandyBreed cur_type = getCandyCoord(x, y)->getBreed();
SDL_RenderClear(gRenderer);
renderFrame();
displayCandyBoard(gRenderer);
SDL_RenderPresent(gRenderer);
delay(100);
printf("Last type: %d Cur Type: %d\n",last_type, cur_type);
if(last_candy_vertical_match >= 3)
{
// set to current state of the candy because the delete() base on the condition that the current candy is not EMPTY
// getCandyCoord(last_candy_choosen.x, last_candy_choosen.y)->setBreed(last_type);
deleteMatchInDirectionCoord(last_candy_choosen.x, last_candy_choosen.y, VERTICAL);
// reset for hor match
getCandyCoord(last_candy_choosen.x, last_candy_choosen.y)->setBreed(normal_last_type);
}
if(last_candy_horizontal_match >= 3)
{
// getCandyCoord(last_candy_choosen.x, last_candy_choosen.y)->setBreed(last_type);
deleteMatchInDirectionCoord(last_candy_choosen.x, last_candy_choosen.y, HORIZONTAL);
}
// reset
if(last_candy_vertical_match >= 3)
getCandyCoord(last_candy_choosen.x, last_candy_choosen.y)->setBreed(EMPTY);
if(current_candy_vertical_match >= 3)
{
// getCandyCoord(x, y)->setBreed(cur_type);
deleteMatchInDirectionCoord(x, y, VERTICAL);
getCandyCoord(x, y)->setBreed(normal_cur_type);
}
if(current_candy_horizontal_match >= 3)
{
// (x, y)->setBreed(cur_type);
deleteMatchInDirectionCoord(x, y, HORIZONTAL);
}
if(current_candy_vertical_match >= 3)
{
getCandyCoord(x, y)->setBreed(EMPTY);
}
// now case it creates special candy
// the L-candy case
if(last_candy_horizontal_match >= 3 && last_candy_vertical_match >= 3)
{
// becoming wrapped candy
last_candy->setBreed( (CandyBreed) ((int) normal_last_type + (int )WRAPPED));
gScoreNumber += 200;
// play wrapped sound
soundEffect[WRAPPED_CANDY_CREATED].play();
}
// COLOUR BOMB case
else if(last_candy_vertical_match >= 5 || last_candy_horizontal_match >= 5)
{
last_candy->setBreed(COLOUR_BOMB);
gScoreNumber += 200;
// play create sound
soundEffect[COLOUR_BOMB_CREATE].play();
}
// the Striped case
else
{
if(last_candy_horizontal_match >= 4)
{
last_candy->setBreed((CandyBreed) ((int) normal_last_type + (int) STRIPED_H));
gScoreNumber += 120;
// play create sound
soundEffect[STRIPED_CANDY_CREATE].play();
}
if(last_candy_vertical_match >= 4)
{
last_candy->setBreed((CandyBreed) ((int) normal_last_type + (int) STRIPED_V));
gScoreNumber += 120;
// play create sound
soundEffect[STRIPED_CANDY_CREATE].play();
}
}
// the same goes for the current candy
if(current_candy_horizontal_match >= 3 && current_candy_vertical_match >= 3)
{
// becoming wrapped candy
current_candy->setBreed((CandyBreed) ((int) normal_cur_type + (int) WRAPPED));
gScoreNumber += 200;
soundEffect[WRAPPED_CANDY_CREATED].play();
}
// COLOUR BOMB case
else if(current_candy_horizontal_match >= 5 || current_candy_vertical_match >= 5)
{
current_candy->setBreed(COLOUR_BOMB);
gScoreNumber += 200;
soundEffect[COLOUR_BOMB_CREATE].play();
}
// the Striped case
else
{
if(current_candy_horizontal_match >= 4)
{
current_candy->setBreed((CandyBreed) ((int) normal_cur_type + (int) STRIPED_H));
gScoreNumber += 120;
soundEffect[STRIPED_H].play();
}
if(current_candy_vertical_match >= 4)
{
current_candy->setBreed((CandyBreed) ((int) normal_cur_type + (int) STRIPED_V));
gScoreNumber += 120;
soundEffect[STRIPED_V].play();
}
}
// display the board being EMPTY some spot
SDL_RenderClear(gRenderer);
renderFrame();
displayCandyBoard(gRenderer);
SDL_RenderPresent(gRenderer);
delay(100);
}
}
// reIndex() : to fill the EMPTY spot
// return: nunber of candies being deleted
int sweetOrNot = reIndex();
int old_man_start_x = SCREEN_WIDTH / 3;
int old_man_start_y = SCREEN_HEIGHT / 3;
int display = -1;
printf("Sweet or not: %d\n", sweetOrNot);
// DIVINE
if(sweetOrNot >= 30)
{
display = DIVINE;
}
// DELICIOUS
else if(sweetOrNot >= 24)
{
display = DELICIOUS;
}
else if(sweetOrNot >= 18)
{
display = TASTY;
}
else if(sweetOrNot >= 12)
{
display = SWEET;
}
if(display != -1)
{
oldManVoice[display].play();
Animation drop;
drop.setTexture(&oldManTexture[display]);
drop.setPosition(old_man_start_x, 0);
drop.setDest(old_man_start_x, old_man_start_y);
do
{
SDL_RenderClear(gRenderer);
renderFrame();
displayCandyBoard(gRenderer);
drop.fall(gRenderer);
SDL_RenderPresent(gRenderer);
delay(10);
} while(drop.getStatus() != STANDING_STILL);
delay(500);
}
}
//reset
last_candy_choosen = {-1, -1};
ANIMATION_MODE = false;
}
break;
}
}
}
}
// can be fixed to run faster by generating without caring about the possible moves
int CandyBoard::reIndex()
{
bool have_empty = false;
// number of falls down a.k.a. number of cascades
int times = 0;
// number of candies deleted
int number_candies_deleted = 0;
// candy fall down
do
{
// reset
have_empty = false;
// cascade -> multiplier += 1
multiplier += 1;
std::vector< std::vector<Candy*> > emptyNeedFilling;
emptyNeedFilling.assign(mColumns, std::vector<Candy*>());
// using for falling animation
std::vector<Animation> fallCandy;
// to render when others candies trying to fall
std::vector<Candy*> notFallingCandies;
// count number of EMPTY cell -> number of candies deleted previouslt
for(int i = 0; i < mRows; i++)
for(int j = 0; j < mColumns; j++)
{
number_candies_deleted += (getCandy(j, i)->getBreed() == EMPTY);
}
// prepare which one is empty
for(int x = 0; x < mColumns; x++)
{
// the algorithm is: swap the EMPTY with the nearest not EMPTY above in the same column
std::queue<Candy*> needSwap;
for(int y = mRows - 1; y >= 0; y--)
{
// if empty candy
if(getCandy(x, y)->getBreed() == EMPTY)
{
needSwap.push(getCandy(x, y));
}
else
{
// have sth need swapped
if(!needSwap.empty())
{
// take the farthest EMPTY
Candy* down_empty_candy = needSwap.front();
needSwap.pop();
// preparing for animation
// nearest not EMPTY
Candy* up_not_empty_candy = getCandy(x, y);
// swapping
// the down
//down_empty_candy->setBreed(up_not_empty_candy->getBreed());
down_empty_candy->swap(up_not_empty_candy);
// the up
up_not_empty_candy->setBreed(EMPTY);
// now is the new EMPTY
needSwap.push(up_not_empty_candy);
// for animation
Animation down_candy_anime;
// take the falling candy image
down_candy_anime.setTexture(down_empty_candy->getTexture());
// set the start position to the falling
down_candy_anime.setPosition(up_not_empty_candy->getX(), up_not_empty_candy->getY());
// set the destination to the EMPTY has just been filled
down_candy_anime.setDest(down_empty_candy->getX(), down_empty_candy->getY());
fallCandy.push_back(down_candy_anime);
}
else
{
notFallingCandies.push_back(getCandy(x, y));
}
}
}
while(!needSwap.empty())
{
// put the EMPTY cell of column x in this
emptyNeedFilling[x].push_back(needSwap.front());
needSwap.pop();
}
}
// fill EMPTY
for(int x = 0; x < mColumns; x++)
{
for(int i = 0; i < emptyNeedFilling[x].size(); i++)
{
Candy* cur_candy = emptyNeedFilling[x][i];
CandyBreed generate_type;
do
{
generate_type = (CandyBreed) (CandyBreed)( ( (rand() % (int) NUMBER_OF_CANDY_TYPES) / 4) * 4);
} while(generate_type == EMPTY || generate_type == COLOUR_BOMB);
cur_candy->setBreed(generate_type);
Animation empty_down_candy_anime;
// falling from outside of the board
empty_down_candy_anime.setTexture(cur_candy->getTexture());
// (i+1) so that it has falling order
empty_down_candy_anime.setPosition(cur_candy->getX(),(i + 1) * (- Candy::CandyHeight) );
empty_down_candy_anime.setDest(cur_candy->getX(), cur_candy->getY());
fallCandy.push_back(empty_down_candy_anime);
}
}
// falling animation
bool falling = true;
do
{
// reset
falling = false;
SDL_RenderClear(gRenderer);
renderFrame();
for(int i = 0; i < notFallingCandies.size(); i++)
{
notFallingCandies[i]->render(gRenderer);
}
for(int i = 0; i < fallCandy.size(); i++)
{
// now fall
fallCandy[i].fall(gRenderer);
if(fallCandy[i].getStatus() == FALLING)
{
falling = true;
//break;
}
}
// no candy to fall
if(fallCandy.empty())
{
falling = false;
Sound land = (Sound) std::min((int) CANDY_LAND1 + times, (int) CANDY_LAND4);
// play landing sound
soundEffect[land].play();
}
SDL_RenderPresent(gRenderer);
delay(10);
//delay(2000);
} while(falling == true);
// show after falling down
SDL_RenderClear(gRenderer);
renderFrame();
displayCandyBoard(gRenderer);
SDL_RenderPresent(gRenderer);
// delay so that player can see it
delay(200);
// play combo sound
// only have 12 landing sound so ...
Sound combo = (Sound) std::min((int) COMBO_SOUND1 + times, (int) COMBO_SOUND12);
soundEffect[combo].play();
// increase time
times ++;
// delete unwrapped candy from wrapped candy first before matching
for(int y = 0; y < mRows; y++)
for(int x = 0; x < mColumns; x++)
{
if(getCandy(x, y) -> isUnwrapped() == true && getCandy(x, y) -> getType() == WRAPPED)
{
deleteBySpecialCandy(getCandy(x, y));
have_empty = true;
}
}
for(int y = 0; y < mRows; y++)
for(int x = 0; x < mColumns; x++)
{
CandyBreed cur_type = (CandyBreed) ((int) getCandy(x, y) -> getBreed() - (int) getCandy(x, y) -> getType());
bool vertical_match = false;
int numb_v_match = countMatchInDirection(x, y, VERTICAL);
int numb_h_match = countMatchInDirection(x, y, HORIZONTAL);
// create special candy
if(numb_v_match >= 3 && numb_h_match >= 3)
{
// delete match
deleteMatchInDirection(x, y, VERTICAL);
getCandy(x, y)->setBreed(cur_type);
deleteMatchInDirection(x, y, HORIZONTAL);
have_empty = true;
gScoreNumber += 200;
// become wrapped
getCandy(x, y)->setBreed((CandyBreed) ((int) cur_type + (int) WRAPPED));
soundEffect[WRAPPED_CANDY_CREATED].play();
}
}
// count and match the match after falling and filling
// create special candy before matching normally
for(int y = 0; y < mRows; y++)
for(int x = 0; x < mColumns; x++)
{
CandyBreed cur_type = getCandy(x, y) -> getBreed();
bool vertical_match = false;
int numb_v_match = countMatchInDirection(x, y, VERTICAL);
int numb_h_match = countMatchInDirection(x, y, HORIZONTAL);
// create special candy
/*
if(numb_v_match >= 3 && numb_h_match >= 3)
{
// delete match
deleteMatchInDirection(x, y, VERTICAL);
getCandy(x, y)->setBreed(cur_type);
deleteMatchInDirection(x, y, HORIZONTAL);
have_empty = true;
gScoreNumber += 200;
// become wrapped
getCandy(x, y)->setBreed((CandyBreed) ((int) cur_type + (int) WRAPPED));
soundEffect[WRAPPED_CANDY_CREATED].play();
}
*/
// the bomb
if(numb_v_match >= 5 || numb_h_match >= 5)
{
if(numb_v_match >= 5)
{
deleteMatchInDirection(x, y, VERTICAL);
have_empty = true;
}
if(numb_h_match >= 5)
{
deleteMatchInDirection(x, y, HORIZONTAL);
have_empty = true;
}
getCandy(x ,y)->setBreed(COLOUR_BOMB);
gScoreNumber += 200;
// play creation sound
soundEffect[COLOUR_BOMB_CREATE].play();
}
// the striped case
else
{
if(numb_v_match >= 4)
{
deleteMatchInDirection(x, y, VERTICAL);
have_empty = true;
getCandy(x,y)->setBreed((CandyBreed) ((int) cur_type + (int) STRIPED_V));
gScoreNumber += 120;
soundEffect[STRIPED_V].play();
}
if(numb_h_match >= 4)
{
deleteMatchInDirection(x, y, HORIZONTAL);
have_empty = true;
getCandy(x, y)->setBreed((CandyBreed) ((int) cur_type + (int) STRIPED_H));
gScoreNumber += 120;
soundEffect[STRIPED_H].play();
}
}
}
// deal with match that not create special candies
for(int y = 0; y < mRows; y++)
for(int x = 0; x < mColumns;x ++)
{
CandyBreed cur_type = getCandy(x, y) -> getBreed();
bool vertical_match = false;
int numb_v_match = 0;
int numb_h_match = 0;
if(countMatchInDirection(x, y, VERTICAL) >= 3)
{
numb_v_match = deleteMatchInDirection(x, y, VERTICAL);
have_empty = true;
vertical_match = true;
}
// only if have vertical match
// reset so that the count horizontally is correct in the L shape
if(vertical_match)
getCandy(x, y)->setBreed(cur_type);
if(countMatchInDirection(x, y, HORIZONTAL) >= 3)
{
numb_h_match = deleteMatchInDirection(x, y, HORIZONTAL);
have_empty = true;
}
if(vertical_match)
getCandy(x,y) -> setBreed(EMPTY);
}
// show board after clearing matches
SDL_RenderClear(gRenderer);
renderFrame();
displayCandyBoard(gRenderer);
SDL_RenderPresent(gRenderer);
} while(have_empty);
return number_candies_deleted;
}
void CandyBoard::sugarCrush()
{
// for when there is still special candy left
bool have_special = false;
int sweetOrNot = 0;
int old_man_start_x;
int old_man_start_y;
int display = -1;
do
{
// reset
have_special = false;
for(int i = 0; i < mRows;i ++)
for(int j = 0; j < mColumns; j++)
{
Candy* tmp_candy = getCandy(j ,i);
if(tmp_candy == NULL)
continue;
if(tmp_candy->getType() != NORMAL)
{
have_special = true;
deleteBySpecialCandy(tmp_candy);
}
if(tmp_candy->getBreed() == COLOUR_BOMB)
{
have_special = true;
printf("COLOUR BOMB!\n");
// choose randomly an adjancent cell that is a NORMAL candy
int dx[3] = {-1, 0, 1};
int dy[3] = {-1, 0, 1};
int ix = 0, iy = 0;
// in case there is no normal candy
bool have_normal = false;
for(int ii = 0; ii < 3; ii++)
for(int jj = 0; jj < 3; jj++)
{
if(j + ix >= 0 && j + ix < mColumns && i + iy >= 0 && i + iy < mRows && getCandy(j + ix, i + iy)->getType() == NORMAL && getCandy(j + ix, i + iy)->getBreed() != EMPTY)
have_normal = true;
}
// if have a normal cell
if(have_normal)
do
{
ix = dx[rand() % 3];
iy = dy[rand() % 3];
} while(!(j + ix >= 0 && j + ix < mColumns && i + iy >= 0 && i + iy < mRows && getCandy(j + ix, i + iy)->getType() == NORMAL && getCandy(j + ix, i + iy)->getBreed() != EMPTY));
// empty the bomb beforehand
getCandy(j, i)->setBreed(EMPTY);
// treat the candy as if trade with colour bomb
deleteByColourBomb(getCandy(j + ix, i + iy));
}
}
// call reindex after sugar crush
// reIndex() : to fill the EMPTY spot
// return: nunber of candies being deleted
sweetOrNot += reIndex();
old_man_start_x = SCREEN_WIDTH / 3;
old_man_start_y = SCREEN_HEIGHT / 3;
printf("Done\n");
} while(have_special == true);
printf("Sweet or not: %d\n", sweetOrNot);
// DIVINE
if(sweetOrNot >= 30)
{
display = DIVINE;
}
// DELICIOUS
else if(sweetOrNot >= 24)
{
display = DELICIOUS;
}
else if(sweetOrNot >= 18)
{
display = TASTY;
}
else if(sweetOrNot >= 12)
{
display = SWEET;
}
if(display != -1)
{
oldManVoice[display].play();
Animation drop;
drop.setTexture(&oldManTexture[display]);
drop.setPosition(old_man_start_x, 0);
drop.setDest(old_man_start_x, old_man_start_y);
do
{
SDL_RenderClear(gRenderer);
renderFrame();
displayCandyBoard(gRenderer);
drop.fall(gRenderer);
SDL_RenderPresent(gRenderer);
_sleep(10);
} while(drop.getStatus() != STANDING_STILL);
//_sleep(200);
}
}
void CandyBoard::generateRandomBoard()
{
int least_moves = 5;
// see if there is already match exists
int already_match = 0;
do
{
// reset each loop
already_match = 0;
for(int i = 0; i < mRows; i++)
for(int j = 0; j < mColumns; j++)
{
int candyY = mY + i * Candy::CandyHeight;
int candyX = mX + j * Candy::CandyWidth;
CandyBreed candyType;
do
{
// "/4" to get the color
// "*4" to get the actual candy
candyType = (CandyBreed)( ( (rand() % (int) NUMBER_OF_CANDY_TYPES) / 4) * 4 );
//if(rand() % 10 == 1)
// candyType = (CandyBreed) ((int) candyType + (int) WRAPPED);
} while(candyType == EMPTY || candyType == COLOUR_BOMB);
candies[i][j].setInfo(candyType, candyX, candyY);
}
for(int i = 0; i < mRows;i ++)
for(int j = 0; j < mColumns; j++)
{
if(countMatchInDirection(i, j, RIGHT) >= 3)
already_match = 1;
if(countMatchInDirection(i, j, DOWN) >= 3)
already_match = 1;
}
} while(countPossibleMove() < LEAST_POSSIBLE_MOVES || already_match == 1);
}
int CandyBoard::countPossibleMove()
{
int moves = 0;
// count already match
for(int i = 0; i < mRows;i ++)
for(int j = 0; j < mColumns; j++)
{
if(countMatchInDirection(i, j, RIGHT) >= 3)
moves += 1;
if(countMatchInDirection(i, j, DOWN) >= 3)
moves += 1;
}
// count potential match
// algorithm: swap the (i,j) with the right cell (i,j+1) or down cell (i+1,j) then check possible move
for(int i = 0; i < mRows; i++)
for(int j = 0; j < mColumns; j++)
{
// horizontal swap
// (i,j) with (i,j+1)
// for recover after swap
CandyBreed oldCur = candies[i][j].getBreed();
int right_swap_match = 0;
int down_swap_match = 0;
// if a valid X
if(j+1 < mColumns)
{
CandyBreed rightType = candies[i][j+1].getBreed();
// swap 2 types of candies
candies[i][j].setBreed(rightType);
candies[i][j+1].setBreed(oldCur);
// - 1 because the current candy is counted twice
right_swap_match = countMatchInDirection(i,j,RIGHT) + countMatchInDirection(i,j,LEFT) - 1;
right_swap_match += countMatchInDirection(i,j,UP) + countMatchInDirection(i,j,DOWN) - 1;
if(oldCur != rightType)
{
right_swap_match += countMatchInDirection(i,j+1,RIGHT) + countMatchInDirection(i,j+1,LEFT) - 1;
right_swap_match += countMatchInDirection(i,j+1,UP) + countMatchInDirection(i,j+1,DOWN) - 1;
}
//swap back
candies[i][j].setBreed(oldCur);
candies[i][j+1].setBreed(rightType);
}
// vertical swap
// (i,j) with (i+1,j)
// if a valid Y
if(i+1 < mRows)
{
CandyBreed downType = candies[i+1][j].getBreed();
// swap
candies[i][j].setBreed(downType);
candies[i+1][j].setBreed(oldCur);
down_swap_match = countMatchInDirection(i,j,UP) + countMatchInDirection(i,j,DOWN) - 1;
down_swap_match += countMatchInDirection(i,j,RIGHT) + countMatchInDirection(i,j,LEFT) - 1;
if(oldCur != downType)
{
down_swap_match += countMatchInDirection(i+1,j,UP) + countMatchInDirection(i+1,j,DOWN) - 1;
down_swap_match += countMatchInDirection(i+1,j,RIGHT) + countMatchInDirection(i+1,j,LEFT) - 1;
}
// swap back
candies[i][j].setBreed(oldCur);
candies[i+1][j].setBreed(downType);
}
// need fixed bc might be more than 1 match
if(down_swap_match >= 3)
moves += 1;
if(right_swap_match >= 3)
moves += 1;
// run faster
if(moves >= LEAST_POSSIBLE_MOVES)
return moves;
}
return moves;
}
int CandyBoard::countMatchInDirection(int lX, int lY, DIRECTION direction)
{
if(lX < 0 || lX >= mColumns || lY < 0 || lY >= mRows)
{
printf("CandyBoard::countMatchInDirection Invalid position passed! %d %d\n", lX, lY);
return -1;
}
// not for empty cell
if(getCandy(lX, lY) -> getBreed() == EMPTY)
return -1;
int match = 0;
int x = lX, y = lY;
CandyBreed curType = getCandy(lX, lY)->getBreed();
Candy* cur_candy = getCandy(lX, lY);
switch(direction)
{
case LEFT:
while(x >= 0 && getCandy(x, lY)->equal(cur_candy))
{
match ++;
x --;
}
break;
case RIGHT:
while(x < mColumns && getCandy(x, lY)->equal(cur_candy))
{
match ++;
x ++;
}
break;
case UP:
while(y >= 0 && getCandy(lX, y)->equal(cur_candy))
{
match ++;
y --;
}
break;
case DOWN:
while(y < mRows && getCandy(lX, y)->equal(cur_candy))
{
match ++;
y ++;
}
break;
case VERTICAL:
match = countMatchInDirection(lX, lY, UP)
+ countMatchInDirection(lX, lY, DOWN) - 1;
break;
case HORIZONTAL:
match = countMatchInDirection(lX, lY, LEFT)
+ countMatchInDirection(lX, lY, RIGHT) - 1;
break;
default:
printf("CandyBoard::countMatchInDirection : Invalid direction passed\n");
break;
}
return match;
}