-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.c
1895 lines (1558 loc) · 56.5 KB
/
game.c
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
/*===========================================
Mahjongg Solitaire Wii
Code : Justin Wood
Game class
============================================*/
#include "game.h"
#include "commons.h"
#include "GRRLIB/GRRLIB.h"
#include "aesndlib.h" // sound library
#include "main.h"
#include "disk.h"
//#include "gameback_jpg.h"
#include "gfx/bk_spooky_jpg.h"
//#include "tileset_png.h"
#include "gfx/clock_png.h"
#include "gfx/matches_png.h"
#include "gfx/playerone_png.h"
#include "gfx/playertwo_png.h"
#include "gfx/pause_png.h"
#include "gfx/paused_png.h"
#include "gfx/pauseover_png.h"
#include "gfx/hint_png.h"
#include "gfx/hintover_png.h"
#include "gfx/undo_png.h"
#include "gfx/undoover_png.h"
#include "gfx/finished_png.h"
#include "gfx/nomorematches_png.h"
#include "gfx/winnerone_png.h"
#include "gfx/winnertwo_png.h"
#include "gfx/draw_png.h"
#include "sound/Click17a_raw.h"
#include "sound/gromb_raw.h"
#include "sound/ptwiiing_raw.h"
#include "sound/gong_raw.h"
#include "sound/a_yokatta_raw.h"
#include "sound/yatta_raw.h"
#include "sound/Haha_raw.h"
#include "sound/Haha2_raw.h"
#include "sound/oh_raw.h"
#include "sound/ohhh_raw.h"
#include "sound/mm_raw.h"
#include "sound/mmmm_raw.h"
#include "sound/aiee_raw.h"
#include "sound/nani_raw.h"
#include "sound/doshiyo_raw.h"
#include "sound/uss_raw.h"
#define BLANK 150
#define PLACE 151
#define TILEHEIGHT 54
#define TILEWIDTH 38
#define TILESIDE 5
#define TILEBOTTOM 4
#define STARTX 12
#define STARTY 22
#define SPECIALY STARTY+TILEHEIGHT/2+3*TILEHEIGHT
#define TOPX STARTX+TILEWIDTH/2+6*TILEWIDTH-(3*TILESIDE+2)
#define RIGHT1X 1+STARTX+13*TILEWIDTH
#define RIGHT2X 1+STARTX+14*TILEWIDTH
#define TOPY SPECIALY-3*TILEBOTTOM
#define TILEX(x,z) STARTX+x*(TILEWIDTH/2)-(z*TILESIDE)
#define TILEY(y,z) STARTY+y*(TILEHEIGHT/2)-(z*TILEBOTTOM)
#define SEL_NONE 0
#define SEL_TILE 5
#define GAME_PLAY 0
#define GAME_PAUSED 1
#define GAME_SHUFFLE 2
#define GAME_AUTOSHUFFLE 3
#define GAME_FINISHED 4
#define GAME_NOMORE 5
#define GAME_BOARDINIT 6
// static functions
static void setLayout(int laynum);
static void initGrid();
static void resetToPlaces();
/**
* Check all tiles if they are selectable.
* The bool selectable[tile] array will be set accordingly
*/
static void checkSelectable();
/**
* Check if given coordinates are selectable.
* @param x X coordinate.
* @param y Y coordinate.
* @param z Z coordinate.
*/
static bool isSelectable(int x, int y, int z);
static bool isHigher(char x,char y,char z);
static void setupGame();
//void shufflePieces(int shuffles);
static void mixPairs(int shuffles);
static void placeTilePair();
/**
* This function checks whether this is the best attempt to place the tiles,
* if it is then save the grid in case we cannot competely place the tiles at all
*/
static void checkAndSaveGrid();
static void placeRemainingtiles();
/**
* Returns a tile which isn't placed yet
* @return tile index in the mahjongg layout array
*/
static int getTile();
/**
* This checks whether this tile can be placed.
* @param tile Tile index to check.
* @return true if tile can be placed.
*/
static bool checkTile(int tile);
/**
* This function is used when we have placed the first tile of a pair and need to check
* whether tile2 could still be placed if tile1 was not there to make sure that we are
* not placing two next to each other that are not accessible
* @param tile1 Tile index to ignore.
* @param tile2 Tile to be checked.
* @return True if tiles can be placed.
*/
static bool checkWithoutTile1(int tile1, int tile2);
/**
* This checks out whether the tile has a foundation from the layer below.
* @param x X coordinate.
* @param y Y coordinate.
* @param z Z coordinate.
*/
static bool isCovered(int x,int y,int z);
static void drawBoard();
static void drawTime();
static void drawMatches();
static void drawScores();
static void drawTilesLeft();
static void drawPauseIcon();
static void drawHintIcon();
static void drawRichTile(f32 xpos, f32 ypos, unsigned char tile,f32 scale,u8 alpha);
static void selectProcessing(WPADData *wd, int player);
static void removeMatched(int player,int s1, int s2);
static void finishGame();
static bool isSameTile(int s1, int s2);
static bool selectTile(f32 x, f32 y,int selnum);
static bool selectExactTile(int x, int y, int z, int selnum);
static void removeTile(int selnum);
static void clearSelected();
static bool checkMatch();
static int calcScore(int player);
static bool goesLeft();
static void findHint();
static void compressTiles();
static char realTileNum(char tileNum);
static void setupFade(int n);
static void drawStatistics();
static void drawUndoIcon();
static void undo();
static void storeUndo();
//variables
static u8 *tex_tiles, *tex_clock, *tex_matches, *tex_pause, *tex_pauseover, *tex_paused, *tex_finished;
static u8 *tex_hint, *tex_hintover, *tex_playerone, *tex_playertwo, *tex_winnerone, *tex_winnertwo, *tex_draw, *tex_nomorematches;
static u8 *tex_undo, *tex_undoover;
u8* tex_gameback;
static bool selectable[MAX_TILES];
// grid to know where you can place tiles
static unsigned char grid[MAX_WIDTH][MAX_HEIGHT][MAX_LAYERS];
static unsigned char bestGuess[MAX_TILES];
// array of tiles
static unsigned char tiles[MAX_TILES];
static int game_state=GAME_PLAY;
static int tot=0,dif=2,score[2],shuffleretries;
// current versus standings
static unsigned int totals[2]={0,0};
static u8 *mtl=(u8 *)default_mtl;
static time_t startTime,endTime,pauseTime;
static unsigned char tilesLeft,tilesToPlace,bestGuessNum=MAX_TILES;
typedef struct selection
{
char type;
unsigned char tile;
int x,y,z;
} selection;
static selection undoTiles[2];
static selection sel[6] =
{
{
SEL_NONE,BLANK,-1,-1,-1
}
,
{
SEL_NONE,BLANK,-1,-1,-1
},
{SEL_NONE,BLANK,-1,-1,-1},
{
SEL_NONE,BLANK,-1,-1,-1
},
{SEL_NONE,BLANK,-1,-1,-1},
{
SEL_NONE,BLANK,-1,-1,-1
}
};
static const u32 playercol[3] = {0x00001111,0x00110011,0x00000011};
typedef struct fadestruct
{
u8 alpha;
f32 scale;
f32 x,y;
unsigned char tile;
bool fading;
} fadestruct;
static fadestruct fade[4] =
{
{
0,0,0,0,0,false
}
,
{
0,0,0,0,0,false
}
,
{
0,0,0,0,0,false
}
,
{
0,0,0,0,0,false
}
};
static bool pauseover=false,menuover=false,shuffleover=false,hintover=false;
static bool restartover=false,newgameover=false,undoover=false, randomgameover = false;
static int gamemode,matches;
static int imgx=0,imgacc=0;
static unsigned char openTiles[36];
static int pausepos[4] = {578,408,56,44};
static int hintpos[4] = {578,353,56,44};
static const int undopos[4] = {578,298,56,44};
static int menupos[4] = {499,398,108,56};
static int shufflepos[4] = {463,328,168,48};
static int restartpos[4] = {40,398,168,48};
static int newgamepos[4] = {40,398,168,48};
static int randomgamepos[4] = {40, 348, 168, 48};
static int multi[2] = {0,0};
void initGame(int gm)
{
gamemode=gm;
setLayout(opt_layout);
// load the textures from the .h tilesets
switch(opt_tileset) {
case DEFAULT :
tex_gameback=GRRLIB_LoadJPG(gameback_jpg, gameback_jpg_size);
tex_tiles=GRRLIB_LoadTexture(ts_default_png);
break;
case SIMPLE :
tex_gameback=GRRLIB_LoadJPG(gameback_jpg, gameback_jpg_size);
tex_tiles=GRRLIB_LoadTexture(ts_simple_png);
break;
case OLD :
tex_gameback=GRRLIB_LoadJPG(gameback_jpg, gameback_jpg_size);
tex_tiles=GRRLIB_LoadTexture(ts_old_png);
break;
case SPOOKY :
tex_gameback=GRRLIB_LoadJPG(bk_spooky_jpg, bk_spooky_jpg_size);
tex_tiles=GRRLIB_LoadTexture(ts_spooky_png);
break;
case EGYPTIAN :
tex_gameback=GRRLIB_LoadJPG(bk_egypt_jpg, bk_egypt_jpg_size);
tex_tiles=GRRLIB_LoadTexture(ts_egypt_png);
break;
case SPACE :
tex_gameback=GRRLIB_LoadJPG(bk_space_jpg, bk_space_jpg_size);
tex_tiles=GRRLIB_LoadTexture(ts_space_png);
break;
default :
tex_gameback=GRRLIB_LoadJPG(gameback_jpg, gameback_jpg_size);
tex_tiles=GRRLIB_LoadTexture(ts_default_png);
break;
}
tex_clock=GRRLIB_LoadTexture(clock_png);
tex_matches=GRRLIB_LoadTexture(matches_png);
tex_playerone=GRRLIB_LoadTexture(playerone_png);
tex_playertwo=GRRLIB_LoadTexture(playertwo_png);
tex_hint=GRRLIB_LoadTexture(hint_png);
tex_hintover=GRRLIB_LoadTexture(hintover_png);
tex_pause=GRRLIB_LoadTexture(pause_png);
tex_paused=GRRLIB_LoadTexture(paused_png);
tex_pauseover=GRRLIB_LoadTexture(pauseover_png);
tex_undo=GRRLIB_LoadTexture(undo_png);
tex_undoover=GRRLIB_LoadTexture(undoover_png);
tex_finished=GRRLIB_LoadTexture(finished_png);
tex_nomorematches=GRRLIB_LoadTexture(nomorematches_png);
tex_winnerone=GRRLIB_LoadTexture(winnerone_png);
tex_winnertwo=GRRLIB_LoadTexture(winnertwo_png);
tex_draw=GRRLIB_LoadTexture(draw_png);
int strsize=GRRLIB_GetStringWidth(CUR_FONT(false),curtext[LNG_GAME_MENU]);
menupos[0]=605-strsize;
menupos[2]=strsize;
strsize=GRRLIB_GetStringWidth(CUR_FONT(false),curtext[LNG_GAME_SHUFFLE]);
shufflepos[0]=605-strsize;
shufflepos[2]=strsize;
restartpos[2]=GRRLIB_GetStringWidth(CUR_FONT(false),curtext[LNG_GAME_RESTART]);
newgamepos[2]=GRRLIB_GetStringWidth(CUR_FONT(false),curtext[LNG_GAME_NEW]);
randomgamepos[2]=GRRLIB_GetStringWidth(CUR_FONT(false),curtext[LNG_GAME_RANDOMLAYOUT]);
setupGame();
}
static void setLayout(int layoutNum)
{
mtl = (u8 *)layouts[layoutNum];
}
static void initGrid()
{
int l,x,y,z,pos=0;
// clear the grid
for(z=0;z<MAX_LAYERS;z++)
for(x=0;x<MAX_WIDTH;x++)
for(y=0;y<MAX_HEIGHT;y++)
grid[x][y][z]=BLANK;
// put placeholders on the grid for the tiles
for(l=0;l<MAX_TILES;++l) {
x=mtl[pos++];
y=mtl[pos++];
z=mtl[pos++];
grid[x][y][z]=PLACE;
}
}
static void resetToPlaces()
{
int l,x,y,z;
tilesToPlace=0;
for(l=0;l<MAX_TILES;l++) {
x=mtl[l*3];
y=mtl[l*3+1];
z=mtl[l*3+2];
if(grid[x][y][z]!=BLANK) {
grid[x][y][z]=PLACE;
tilesToPlace++;
}
}
}
static void checkSelectable()
{
int x,y,z;
for(x=0;x<MAX_TILES;++x) selectable[x]=false;
for(z=MAX_LAYERS-1;z>=0;--z) {
for(y=0;y<MAX_HEIGHT;++y) {
for(x=0;x<MAX_WIDTH;++x) {
if(grid[x][y][z]!=BLANK) {
selectable[grid[x][y][z]]=isSelectable(x,y,z);
}
}
}
}
}
static bool isSelectable(int x, int y, int z)
{
// first check if there are any overlapping from the row above
if(z<(MAX_LAYERS-1)) {
if(isHigher(x,y,z+1)) {
return false;
}
}
int tot;
// check left hand side
if(x-2>=0) {
tot=0;
if(y-1>=0) {
tot+=grid[x-2][y-1][z]==BLANK?0:1;
}
tot+=grid[x-2][y][z]==BLANK?0:1;
tot+=grid[x-2][y+1][z]==BLANK?0:1;
if(tot==0) return true;
}
else {
return true;
}
// check right hand side
if(x+2<MAX_WIDTH) {
tot=0;
if(y-1>=0) {
tot+=grid[x+2][y-1][z]==BLANK?0:1;
}
tot+=grid[x+2][y][z]==BLANK?0:1;
tot+=grid[x+2][y+1][z]==BLANK?0:1;
if(tot==0) return true;
}
else {
return true;
}
return false;
}
static bool isHigher(char x,char y,char z)
{
int r,c,tot=0;
int x1=x-1,x2=x+2,y1=y-1,y2=y+2;
if(x1<0) x1=0;
if(x2>=MAX_WIDTH) x2=MAX_WIDTH-1;
if(y1<0) y1=0;
if(y2>=MAX_HEIGHT) y2=MAX_HEIGHT-1;
for(r=x1;r<x2;r++)
for(c=y1;c<y2;c++)
tot+=grid[r][c][(int)z]==BLANK?0:1;
return tot>0;
}
void killGame()
{
if(tex_draw) free(tex_draw);
if(tex_winnertwo) free(tex_winnertwo);
if(tex_winnerone) free(tex_winnerone);
if(tex_nomorematches) free(tex_nomorematches);
if(tex_finished) free(tex_finished);
if(tex_pauseover) free(tex_pauseover);
if(tex_paused) free(tex_paused);
if(tex_pause) free(tex_pause);
if(tex_hintover) free(tex_hintover);
if(tex_hint) free(tex_hint);
if(tex_undoover) free(tex_undoover);
if(tex_undo) free(tex_undo);
if(tex_playertwo) free(tex_playertwo);
if(tex_playerone) free(tex_playerone);
if(tex_matches) free(tex_matches);
if(tex_clock) free(tex_clock);
if(tex_tiles) free(tex_tiles);
if(tex_gameback) free(tex_gameback);
}
static void setupGame()
{
srand(time(NULL));
// This sets up the array of tiles so that they correspond to the number in the graphical tile set
int z=0,x,y;
for(x=0;x<34;x++) {
for(y=0;y<4;y++) {
tiles[z++]=x;
}
}
for(x=34;x<42;x++) {
tiles[z++]=x;
}
tot=0;
dif=2;
score[0]=0;
score[1]=0;
undoTiles[0].x = -1; // disable undo from start
for(x=0;x<4;x++) {
clearSelected(x);
fade[x].fading=false;
}
clearSelected(4);
clearSelected(5);
multi[0]=0;
multi[1]=0;
shuffleretries=0;
initGrid();
tilesLeft=tilesToPlace=MAX_TILES;
mixPairs(tilesLeft*6);
shuffleretries=0;
bestGuessNum=MAX_TILES;
while(tilesToPlace>0) {
placeTilePair();
}
checkSelectable();
goesLeft();
startTime = time(NULL);
game_state=GAME_PLAY;
playRaw(&gong_raw, gong_raw_size, 11025, opt_sound, 0, 0);
playRaw(&gong_raw, gong_raw_size, 11025, 0, opt_sound, 40);
//SND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_16BIT, 11025, 0,&gong_raw, gong_raw_size, opt_sound, 0, NULL);
//SND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_16BIT, 11025, 40,&gong_raw, gong_raw_size, 0, opt_sound, NULL);
}
static void mixPairs(int shuffles)
{
int x,t1,t2;
unsigned char dummy;
for(x=0;x<shuffles;x++) {
do {
t1=((rand()%tilesLeft)/2)*2;
} while(tiles[t1]==BLANK);
do {
t2=((rand()%tilesLeft)/2)*2;
} while(tiles[t2]==BLANK || t1==t2);
dummy=tiles[t1];
tiles[t1]=tiles[t2];
tiles[t2]=dummy;
dummy=tiles[t1+1];
tiles[t1+1]=tiles[t2+1];
tiles[t2+1]=dummy;
}
}
static void drawStatistics()
{
}
void drawGame()
{
static int tot=0,dif=1;
int strlen,x;
GRRLIB_DrawImg(0, 0, 640, 480, tex_gameback, 0, 1, 1, 255);
drawBoard();
if(gamemode==TWO_PLAYER_VERSUS) {
drawScores();
}
else {
drawTime();
drawMatches();
drawTilesLeft();
}
for(x=0;x<4;x++) {
if(fade[x].fading) {
drawRichTile(fade[x].x,fade[x].y,fade[x].tile,fade[x].scale,fade[x].alpha);
if(fade[x].alpha-8<0) fade[x].fading=false;
if(x%2==1 && multi[x/2]>1) GRRLIB_GPrintf((fade[x].x+fade[x-1].x)/2,(fade[x].y+fade[x-1].y)/2,0x00FFFFFF-(0x01000000*fade[x].alpha),fade[x].scale,fade[x].scale,ALIGN_CENTRE,2,"X%d",multi[x/2]);
fade[x].alpha-=8;
fade[x].scale+=0.2;
if(fade[x].alpha==191) {
if(opt_rumble) WPAD_Rumble(x/2==0?WPAD_CHAN_0:WPAD_CHAN_1, 0);
}
}
}
switch(game_state) {
case GAME_PLAY :
drawPauseIcon();
if(gamemode!=TWO_PLAYER_VERSUS) {
drawHintIcon();
drawUndoIcon();
}
break;
case GAME_PAUSED :
GRRLIB_FillScreen(0xAA000000);
GRRLIB_DrawImg(198,134,228,196,tex_paused, 0, 1, 1, 255);
strlen = GRRLIB_GetStringWidth(CUR_FONT(false), curtext[LNG_GAME_PAUSED]);
if(strlen>192) {
f32 zoom = 192.0/(float)strlen;;
GRRLIB_GPrintf(198+26,274,0xFFEEEEBB,zoom,1, ALIGN_LEFT,CUR_FONT(false),curtext[LNG_GAME_PAUSED]);
}
else {
GRRLIB_GPrintf(198+22+200/2,274,0xFFEEEEBB,1,1, ALIGN_CENTRE,CUR_FONT(false),curtext[LNG_GAME_PAUSED]);
}
GRRLIB_GPrintf(restartpos[0], restartpos[1],0xFFFFFFFF,1,1, ALIGN_LEFT,CUR_FONT(restartover),curtext[LNG_GAME_RESTART]);
drawPauseIcon();
break;
case GAME_SHUFFLE :
if(tot++==dif) {
dif+=10;
if(tot>90 && tilesToPlace<=0) {
game_state=GAME_PLAY;
checkSelectable();
goesLeft();
dif=2;
tot=0;
for(x=0;x<4;x++) {
clearSelected(x);
}
}
}
if(tilesToPlace>0)
placeTilePair();
if(tot*2.5<=255) {
GRRLIB_GPrintf(320, 220,0xFFFFFFFF- (0x01000000*(tot*2.5)),1+((float)tot)/16,1+((float)tot)/32, ALIGN_CENTRE,CUR_FONT(true),curtext[LNG_GAME_SHUFFLE]);
}
break;
case GAME_FINISHED :
{
GRRLIB_FillScreen(0xAA000000);
if(gamemode==TWO_PLAYER_VERSUS) {
if(score[0]>score[1]) {
GRRLIB_DrawImg(185+imgx,103,260,220,tex_winnerone, 0, 1, 1, 255);
GRRLIB_GPrintf(181+imgx,232,0xFF9E9E6B,0.6,0.68, ALIGN_LEFT,CUR_FONT(false),curtext[LNG_GAME_WINNER]);
GRRLIB_GPrintf(188+6+119+imgx,270,0xFFC02600,1,1, ALIGN_CENTRE,CUR_FONT(false),curtext[LNG_GAME_PLAYER1]);
}
else
if(score[1]>score[0]) {
GRRLIB_DrawImg(185+imgx,103,260,220,tex_winnertwo, 0, 1, 1, 255);
GRRLIB_GPrintf(181+imgx,232,0xFF9E9E6B,0.6,0.68, ALIGN_LEFT,CUR_FONT(false),curtext[LNG_GAME_WINNER]);
GRRLIB_GPrintf(188+6+119+imgx,270,0xFF8DC033,1,1, ALIGN_CENTRE,CUR_FONT(false),curtext[LNG_GAME_PLAYER2]);
}
else {
GRRLIB_DrawImg(150+imgx,99,344,224,tex_draw, 0, 1, 1, 255);
GRRLIB_GPrintf(188+4+119+imgx,260,0xFFA67319,1,1, ALIGN_CENTRE,CUR_FONT(false),curtext[LNG_GAME_DRAW]);
}
GRRLIB_DrawImg(205, 330, 60, 44, tex_playerone, 0, 1, 1, 255- ((float)imgx*0.75));
GRRLIB_DrawImg(365, 330, 60, 44, tex_playertwo, 0, 1, 1, 255- ((float)imgx*0.75));
GRRLIB_GPrintf(315, 340,0xFFFFFFFF - ((float)imgx*0.75)*0x01000000,1,1,ALIGN_CENTRE,0,"%02d - %02d",totals[0],totals[1]);
}
else {
//this indicates that a new highscore was reached
if( totals[0] > 0) {
GRRLIB_GPrintf( 30, 50, 0xFFFFFFFF, 1, 1, ALIGN_LEFT, CUR_FONT(true), curtext[LNG_GAME_NEWHIGHSCORE]);
}
GRRLIB_DrawImg(186+imgx,151,252,172,tex_finished, 0, 1, 1, 255);
drawStatistics();
strlen = GRRLIB_GetStringWidth(CUR_FONT(false), curtext[LNG_GAME_FINISHED]);
if(strlen>238) {
f32 zoom = (float)238/(float)strlen;
GRRLIB_GPrintf(185+8+imgx,255,0xFFEEEEBB,zoom,1, ALIGN_LEFT,CUR_FONT(false),curtext[LNG_GAME_FINISHED]);
}
else {
GRRLIB_GPrintf(188+8+119+imgx,255,0xFFEEEEBB,1,1, ALIGN_CENTRE,CUR_FONT(false),curtext[LNG_GAME_FINISHED]);
}
}
if(imgx>0 && imgacc>0) {
imgx-=imgacc;
imgacc--;
}
GRRLIB_GPrintf(newgamepos[0], newgamepos[1],0xFFFFFFFF,1,1, ALIGN_LEFT,CUR_FONT(newgameover),curtext[LNG_GAME_RESTART]);
GRRLIB_GPrintf(randomgamepos[0], randomgamepos[1],0xFFFFFFFF,1,1, ALIGN_LEFT,CUR_FONT(randomgameover),curtext[LNG_GAME_RANDOMLAYOUT]);
GRRLIB_GPrintf(menupos[0], menupos[1],0xFFFFFFFF,1,1, ALIGN_LEFT,CUR_FONT(menuover),curtext[LNG_GAME_MENU]);
}
break;
case GAME_NOMORE :
{
GRRLIB_FillScreen(0xAA000000);
GRRLIB_DrawImg(170+imgx,149,284,160,tex_nomorematches, 0, 1, 1, 255);
strlen = GRRLIB_GetStringWidth(CUR_FONT(false), curtext[LNG_GAME_NOMORETILES]);
if(strlen>238) {
f32 zoom = (float)238/(float)strlen;
GRRLIB_GPrintf(170+32+imgx,250,0xFFEEEEBB,zoom,0.8, ALIGN_LEFT,CUR_FONT(false),curtext[LNG_GAME_NOMORETILES]);
}
else {
GRRLIB_GPrintf(170+36+119+imgx,250,0xFFEEEEBB,1,0.8, ALIGN_CENTRE,CUR_FONT(false),curtext[LNG_GAME_NOMORETILES]);
}
drawStatistics();
if(imgx>0 && imgacc>0) {
imgx-=imgacc;
imgacc--;
}
GRRLIB_GPrintf(restartpos[0], restartpos[1],0xFFFFFFFF,1,1, ALIGN_LEFT,CUR_FONT(restartover),curtext[LNG_GAME_RESTART]);
GRRLIB_GPrintf(menupos[0], menupos[1],0xFFFFFFFF,1,1, ALIGN_LEFT,CUR_FONT(menuover),curtext[LNG_GAME_MENU]);
GRRLIB_GPrintf(shufflepos[0], shufflepos[1],0xFFFFFFFF,1,1, ALIGN_LEFT,CUR_FONT(shuffleover),curtext[LNG_GAME_SHUFFLE]);
}
break;
}
}
static void placeTilePair()
{
int tile1;
int retries=0;
do {
tile1 = getTile();
} while(!checkTile(tile1) && retries++ < tilesLeft*8);
if(retries>=tilesLeft*8) {
shuffleretries++;
if(shuffleretries>7) {
// we have tried to create a new board serveral times this probably means that there are no more goes
// so lets bring place all the remaining tiles randomly
placeRemainingtiles();
checkSelectable();
if(!goesLeft()) {
if(shuffleretries>10) {
// assume that we cannot place the remaining tiles
finishGame();
return;
}
resetToPlaces();
}
}
else {
checkAndSaveGrid();
resetToPlaces();
}
return;
}
// place tile 1 in the grid
grid[mtl[tile1*3]][mtl[tile1*3+1]][mtl[tile1*3+2]]=--tilesToPlace;
// find spare tile 2
int tile2;
retries=0;
do {
tile2 = getTile();
} while((!checkTile(tile2) || tile1==tile2 || !checkWithoutTile1(tile1,tile2)) && retries++ < tilesLeft*8);
if(retries>=tilesLeft*8) {
shuffleretries++;
if(shuffleretries>7) {
// we have tried to create a new board serveral times this probably means that there are no more goes
// so lets bring place all the remaining tiles randomly
placeRemainingtiles();
checkSelectable();
if(!goesLeft()) {
if(shuffleretries>10) {
// assume that we cannot place the remaining tiles
finishGame();
return;
}
resetToPlaces();
}
}
else {
checkAndSaveGrid();
resetToPlaces();
}
return;
}
grid[mtl[tile2*3]][mtl[tile2*3+1]][mtl[tile2*3+2]]=--tilesToPlace;
}
static void checkAndSaveGrid()
{
if(tilesToPlace>=bestGuessNum) return;
bestGuessNum=tilesToPlace;
int x;
for(x=0;x<MAX_TILES;x++) {
bestGuess[x]=grid[mtl[x*3]][mtl[x*3+1]][mtl[x*3+2]];
}
}
// This function is used to place all remaining tiles randomly to complete a uncompletable shuffle
// this can be resolved later when there are less tiles left
static void placeRemainingtiles()
{
int x,xe;
// set grid up with the best guess so far
tilesToPlace=MAX_TILES;
for(x=0;x<MAX_TILES;x++) {
grid[mtl[x*3]][mtl[x*3+1]][mtl[x*3+2]]=bestGuess[x];
if(bestGuess[x]!=PLACE) tilesToPlace--;
}
xe=tilesToPlace;
for(x=0;x<xe;x++) {
int tile = getTile();
grid[mtl[tile*3]][mtl[tile*3+1]][mtl[tile*3+2]]=--tilesToPlace;
}
}
static int getTile()
{
int tile;
int x, y, z;
// get tiles until we find one that we can place
do {
tile=rand() % MAX_TILES;
x=mtl[tile*3];
y=mtl[tile*3+1];
z=mtl[tile*3+2];
} while (grid[x][y][z] != PLACE);
return tile;
}
static bool checkTile(int tile)
{
int x=mtl[tile*3];
int y=mtl[tile*3+1];
int z=mtl[tile*3+2];
//if z>0 check that tiles below are already placed
if(z>0 && !isCovered(x,y,z)) {
return false;
}
// check that we have tiles immediately to the left
if(x>1) {
if(grid[x-2][y][z]<BLANK) return true;
if(y==0 && grid[x-2][y+1][z]<BLANK) return true;
if(y>0) {
if(grid[x-2][y-1][z]<BLANK && grid[x-2][y+1][z]<BLANK) return true;
if(grid[x-2][y-1][z]<BLANK && grid[x-2][y+1][z]==BLANK) return true;
if(grid[x-2][y-1][z]==BLANK && grid[x-2][y+1][z]<BLANK) return true;
}
}
// check that we have tiles immediately to the right
if(x<MAX_WIDTH-2) {
if(grid[x+2][y][z]<BLANK) return true;
if(y==0 && grid[x+2][y+1][z]<BLANK) return true;
if(y>0) {
if(grid[x+2][y-1][z]<BLANK && grid[x+2][y+1][z]<BLANK) return true;
if(grid[x+2][y-1][z]<BLANK && grid[x+2][y+1][z]==BLANK) return true;
if(grid[x+2][y-1][z]==BLANK && grid[x+2][y+1][z]<BLANK) return true;
}
}
// if any other placed tiles on the same row return false
//
// tilebreak signifies that there is a break of tiles on the current row
// tileplace indicates that there are two unplaced tiles on the current row
// this works out that
int col;
// check left of the selected
bool tilebreak=false,tileplace=false;
int blank,yy;
if(x==0) {
tilebreak=true;
}
else {
// copy y and use the copy as we may move up and down
yy=y;
for(col=x-2;col>=0 && tilebreak==false && tileplace==false;col-=2) {
// check for tiles already placed on this row
if(grid[col][yy][z]<BLANK || grid[col][yy+1][z]<BLANK) return false;
if(yy>0 && grid[col][yy-1][z]<BLANK) return false;
// check whether this is a tilebreak
blank=3;
if(grid[col][yy][z]==BLANK) blank--;
if((yy>0 && grid[col][yy-1][z]==BLANK) || yy==0) blank--;
if((yy<MAX_HEIGHT-2 && grid[col][yy+1][z]==BLANK) || yy>=MAX_HEIGHT-2) blank--;
if(blank==0) tilebreak=true;
// see if line moves up or down or finishes
if(grid[col][yy][z] != PLACE) {
if(yy>0) {
if(grid[col][yy-1][z]==PLACE && grid[col][yy+1][z]==PLACE) tileplace=true;
else
if(grid[col][yy-1][z]==PLACE && grid[col][yy+1][z]==BLANK) yy--;
else
if(grid[col][yy-1][z]==BLANK && grid[col][yy+1][z]==PLACE) yy++;
}
else {
// currently on top row
if(grid[col][yy][z]==BLANK && grid[col][yy+1][z]==PLACE) yy++;
}
}
}
if(!tileplace && !tilebreak) tilebreak=true;
}
// check right of the selected
if(x>=MAX_WIDTH-2) {
if(tileplace) return false;
if(tilebreak) return true;
}
else {
// copy y and use the copy as we may move up and down
yy=y;
for(col=x+2;col<MAX_WIDTH-2;col+=2) {
// check for tiles already placed on this row
if(grid[col][yy][z]<BLANK || grid[col][yy+1][z]<BLANK) return false;
if(yy>0 && grid[col][yy-1][z]<BLANK) return false;
// check whether this is a tilebreak
blank=3;