summaryrefslogtreecommitdiff
path: root/apps/plugins/solitaire.c
blob: 20be137c625988aad8c8db6cafa80ea062a7ea39 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2004 dionoea (Antoine Cellerier)
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/*****************************************************************************
Solitaire by dionoea

use arrows to move the cursor
use ON to select cards, move cards, reveal hidden cards, ...
use PLAY to move a card from the remains' stack to the top of the cursor
use F1 to put card under cursor on one of the 4 final color stacks
use F2 to un-select card if a card was selected, else draw 3 new cards
    out of the remains' stack
use F3 to put card on top of the remains' stack on one of the 4 final color
    stacks

*****************************************************************************/

#include "plugin.h"
#include "button.h"
#include "lcd.h"

#ifdef HAVE_LCD_BITMAP
#if CONFIG_KEYPAD != IRIVER_H100_PAD /* FIX! */

/* here is a global api struct pointer. while not strictly necessary,
   it's nice not to have to pass the api pointer in all function calls
   in the plugin */
static struct plugin_api* rb;

#define min(a,b) (a<b?a:b)

/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define SOL_QUIT BUTTON_OFF
#define SOL_UP BUTTON_UP
#define SOL_DOWN BUTTON_DOWN
#define SOL_LEFT BUTTON_LEFT
#define SOL_RIGHT BUTTON_RIGHT
#define SOL_MOVE BUTTON_ON
#define SOL_DRAW BUTTON_F2
#define SOL_REM2CUR BUTTON_PLAY
#define SOL_CUR2STACK BUTTON_F1
#define SOL_REM2STACK BUTTON_F3
#define SOL_MENU_RUN BUTTON_RIGHT
#define SOL_MENU_RUN2 BUTTON_PLAY
#define SOL_MENU_INFO BUTTON_F1
#define SOL_MENU_INFO2 BUTTON_F2
#define SOL_MENU_INFO3 BUTTON_F3

#elif CONFIG_KEYPAD == ONDIO_PAD
#define SOL_QUIT BUTTON_OFF
#define SOL_UP_PRE BUTTON_UP
#define SOL_UP (BUTTON_UP | BUTTON_REL)
#define SOL_DOWN_PRE BUTTON_DOWN
#define SOL_DOWN (BUTTON_DOWN | BUTTON_REL)
#define SOL_LEFT_PRE BUTTON_LEFT
#define SOL_LEFT (BUTTON_LEFT | BUTTON_REL)
#define SOL_RIGHT_PRE BUTTON_RIGHT
#define SOL_RIGHT (BUTTON_RIGHT | BUTTON_REL)
#define SOL_MOVE_PRE BUTTON_MENU
#define SOL_MOVE (BUTTON_MENU | BUTTON_REL)
#define SOL_DRAW_PRE BUTTON_MENU
#define SOL_DRAW (BUTTON_MENU | BUTTON_REPEAT)
#define SOL_REM2CUR_PRE BUTTON_LEFT
#define SOL_REM2CUR (BUTTON_LEFT | BUTTON_REPEAT)
#define SOL_CUR2STACK_PRE BUTTON_RIGHT
#define SOL_CUR2STACK (BUTTON_RIGHT | BUTTON_REPEAT)
#define SOL_REM2STACK_PRE BUTTON_UP
#define SOL_REM2STACK (BUTTON_UP | BUTTON_REPEAT)
#define SOL_MENU_RUN BUTTON_RIGHT
#define SOL_MENU_INFO BUTTON_MENU

#elif CONFIG_KEYPAD == IRIVER_H100_PAD
#define SOL_QUIT BUTTON_OFF
#define SOL_UP BUTTON_UP
#define SOL_DOWN BUTTON_DOWN
#define SOL_LEFT BUTTON_LEFT
#define SOL_RIGHT BUTTON_RIGHT
#define SOL_MOVE BUTTON_ON
#define SOL_DRAW BUTTON_SELECT
#define SOL_REM2CUR BUTTON_PLAY
#define SOL_CUR2STACK BUTTON_F1
#define SOL_REM2STACK BUTTON_F3
#define SOL_MENU_RUN BUTTON_RIGHT
#define SOL_MENU_RUN2 BUTTON_ON
#define SOL_MENU_INFO BUTTON_SELECT
#define SOL_MENU_INFO2 BUTTON_REC
#define SOL_MENU_INFO3 BUTTON_MODE
#endif

/* common help definitions */
#define HELP_SOL_UP "UP: Move the cursor up in the column."
#define HELP_SOL_DOWN "DOWN: Move the cursor down in the column."
#define HELP_SOL_LEFT "LEFT: Move the cursor to the previous column."
#define HELP_SOL_RIGHT "RIGHT: Move the cursor to the next column."

/* variable help definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define HELP_SOL_MOVE "ON: Select cards, Move cards, reveal hidden cards ..."
#define HELP_SOL_DRAW "F2: Un-select a card if it was selected. Else, draw 3 new cards out of the remains' stack."
#define HELP_SOL_REM2CUR "PLAY: Put the card on top of the remains' stack on top of the cursor."
#define HELP_SOL_CUR2STACK "F1: Put the card under the cursor on one of the 4 final color stacks."
#define HELP_SOL_REM2STACK "F3: Put the card on top of the remains' stack on one of the 4 final color stacks."

#elif CONFIG_KEYPAD == ONDIO_PAD
#define HELP_SOL_MOVE "MODE: Select cards, Move cards, reveal hidden cards ..."
#define HELP_SOL_DRAW "MODE..: Un-select a card if it was selected. Else, draw 3 new cards out of the remains' stack."
#define HELP_SOL_REM2CUR "LEFT..: Put the card on top of the remains' stack on top of the cursor."
#define HELP_SOL_CUR2STACK "RIGHT..: Put the card under the cursor on one of the 4 final color stacks."
#define HELP_SOL_REM2STACK "UP..: Put the card on top of the remains' stack on one of the 4 final color stacks."

#endif

static unsigned char colors[4][8] = {
/* Spades */
    {0x00, /* ........ */
     0x18, /* ...O.... */
     0x1c, /* ..OOO... */
     0x3e, /* .OOOOO.. */
     0x1c, /* .OOOOO.. */
     0x18, /* ...O.... */
     0x00, /* ........ */
     0x00},/* ........ */
/* Hearts */
    {0x00, /* ........ */
     0x0c, /* ..O.O... */
     0x1e, /* .OOOOO.. */
     0x3c, /* .OOOOO.. */
     0x1e, /* ..OOO... */
     0x0c, /* ...O.... */
     0x00, /* ........ */
     0x00},/* ........ */
/* Clubs */
    {0x00, /* ........ */
     0x18, /* ..OOO... */
     0x0a, /* ...O.... */
     0x3e, /* .OOOOO.. */
     0x0a, /* .O.O.O.. */
     0x18, /* ...O.... */
     0x00, /* ........ */
     0x00},/* ........ */
/* Diamonds */
    {0x00, /* ........ */
     0x08, /* ...O.... */
     0x1c, /* ..OOO... */
     0x3e, /* .OOOOO.. */
     0x1c, /* ..OOO... */
     0x08, /* ...O.... */
     0x00, /* ........ */
     0x00} /* ........ */
};

static unsigned char numbers[13][8] = {
/* Ace */
    {0x00, /* ........ */
     0x38, /* ...O.... */
     0x14, /* ..O.O... */
     0x12, /* .O...O.. */
     0x14, /* .OOOOO.. */
     0x38, /* .O...O.. */
     0x00, /* ........ */
     0x00},/* ........ */
/* 2 */
    {0x00, /* ........ */
     0x24, /* ..OOO... */
     0x32, /* .O...O.. */
     0x32, /* ....O... */
     0x2a, /* ..OO.... */
     0x24, /* .OOOOO.. */
     0x00, /* ........ */
     0x00},/* ........ */
/* 3 */
    {0x00, /* ........ */
     0x22, /* .OOOO... */
     0x2a, /* .....O.. */
     0x2a, /* ..OOO... */
     0x2a, /* .....O.. */
     0x14, /* .OOOO... */
     0x00, /* ........ */
     0x00},/* ........ */
/* 4 */
    {0x00, /* ........ */
     0x10, /* ....O... */
     0x18, /* ...O.... */
     0x34, /* ..O..... */
     0x12, /* .OOOOO.. */
     0x10, /* ...O.... */
     0x00, /* ........ */
     0x00},/* ........ */
/* 5 */
    {0x00, /* ........ */
     0x2e, /* .OOOOO.. */
     0x2a, /* .O...... */
     0x2a, /* .OOOO... */
     0x2a, /* .....O.. */
     0x12, /* .OOOO... */
     0x00, /* ........ */
     0x00},/* ........ */
/* 6 */
    {0x00, /* ........ */
     0x1c, /* ..OOO... */
     0x2a, /* .O...... */
     0x2a, /* .OOOO... */
     0x2a, /* .O...O.. */
     0x10, /* ..OOO... */
     0x00, /* ........ */
     0x00},/* ........ */
/* 7 */
    {0x00, /* ........ */
     0x22, /* .OOOOO.. */
     0x12, /* ....O... */
     0x0a, /* ...O.... */
     0x06, /* ..O..... */
     0x02, /* .O...... */
     0x00, /* ........ */
     0x00},/* ........ */
/* 8 */
    {0x00, /* ........ */
     0x14, /* ..OOO... */
     0x2a, /* .O...O.. */
     0x2a, /* ..OOO... */
     0x2a, /* .O...O.. */
     0x14, /* ..OOO... */
     0x00, /* ........ */
     0x00},/* ........ */
/* 9 */
    {0x00, /* ........ */
     0x04, /* ..OOO... */
     0x2a, /* .O...O.. */
     0x2a, /* ..OOOO.. */
     0x2a, /* .....O.. */
     0x1c, /* ..OOO... */
     0x00, /* ........ */
     0x00},/* ........ */
/* 10 */
    {0x00, /* ........ */
     0x3e, /* .O..O... */
     0x00, /* .O.O.O.. */
     0x1c, /* .O.O.O.. */
     0x22, /* .O.O.O.. */
     0x1c, /* .O..O... */
     0x00, /* ........ */
     0x00},/* ........ */
/* Jack */
    {0x00, /* ........ */
     0x12, /* .OOOOO.. */
     0x22, /* ...O.... */
     0x1e, /* ...O.... */
     0x02, /* .O.O.... */
     0x02, /* ..O..... */
     0x00, /* ........ */
     0x00},/* ........ */
/* Queen */
    {0x00, /* ........ */
     0x1c, /* ..OOO... */
     0x22, /* .O...O.. */
     0x32, /* .O...O.. */
     0x22, /* .O.O.O.. */
     0x1c, /* ..OOO... */
     0x00, /* ........ */
     0x00},/* ........ */
/* King */
    {0x00, /* ........ */
     0x3e, /* .O...O.. */
     0x08, /* .O..O... */
     0x08, /* .OOO.... */
     0x14, /* .O..O... */
     0x22, /* .O...O.. */
     0x00, /* ........ */
     0x00} /* ........ */
};

#define NOT_A_CARD 255

/* number of cards per color */
#define CARDS_PER_COLOR 13

/* number of colors */
#define COLORS 4

/* number of columns */
#define COL_NUM 7

/* pseudo column numbers to be used for cursor coordinates */
/* columns COL_NUM t COL_NUM + COLORS - 1 correspond to the color stacks */
#define STACKS_COL COL_NUM
/* column COL_NUM + COLORS corresponds to the remains' stack */
#define REM_COL (STACKS_COL + COLORS)

#define NOT_A_COL 255

/* number of cards that are drawn on the remains' stack (by pressing F2) */
#define CARDS_PER_DRAW 3

/* size of a card on the screen */
#define CARD_WIDTH 14
#define CARD_HEIGHT 10

typedef struct card {
    unsigned char color : 2;
    unsigned char num : 4;
    unsigned char known : 1;
    unsigned char used : 1;/* this is what is used when dealing cards */
    unsigned char next;
} card;

unsigned char next_random_card(card *deck){
    unsigned char i,r;
    
    r = rb->rand()%(COLORS * CARDS_PER_COLOR)+1;
    i = 0;
    
    while(r>0){
        i = (i + 1)%(COLORS * CARDS_PER_COLOR);
        if(!deck[i].used) r--;
    }

    deck[i].used = 1;

    return i;
}

#define HELP_QUIT 0
#define HELP_USB 1

/* help for the not so intuitive interface */
int solitaire_help(void){

    int button;
    int lastbutton = BUTTON_NONE;

    while(1){
    
        rb->lcd_clear_display();

#if CONFIG_KEYPAD == RECORDER_PAD
        rb->lcd_putsxy(0, 0, "Press a key to see");
        rb->lcd_putsxy(0, 7, "it's role.");
        rb->lcd_putsxy(0, 21, "Press OFF to");
        rb->lcd_putsxy(0, 28, "return to menu.");
        rb->lcd_putsxy(0, 42, "All actions can");
        rb->lcd_putsxy(0, 49, "be done using");
        rb->lcd_putsxy(0, 56, "arrows, ON and F2.");
#elif CONFIG_KEYPAD == ONDIO_PAD
        rb->lcd_putsxy(0, 0, "Press a key short");
        rb->lcd_putsxy(0, 7, "or long to see it's");
        rb->lcd_putsxy(0, 21, "role. Press OFF to");
        rb->lcd_putsxy(0, 28, "return to menu.");
        rb->lcd_putsxy(0, 42, "All actions can be");
        rb->lcd_putsxy(0, 49, "done using arrows,");
        rb->lcd_putsxy(0, 56, "short & long MODE.");
#endif

        rb->lcd_update();
    
        button = rb->button_get(true);
        switch(button){
            case SOL_UP:
#ifdef SOL_UP_PRE
                if(lastbutton != SOL_UP_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_UP);
                break;

            case SOL_DOWN:
#ifdef SOL_DOWN_PRE
                if(lastbutton != SOL_DOWN_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_DOWN);
                break;

            case SOL_LEFT:
#ifdef SOL_LEFT_PRE
                if(lastbutton != SOL_LEFT_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_LEFT);
                break;

            case SOL_RIGHT:
#ifdef SOL_RIGHT_PRE
                if(lastbutton != SOL_RIGHT_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_RIGHT);
                break;

            case SOL_MOVE:
#ifdef SOL_MOVE_PRE
                if(lastbutton != SOL_MOVE_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_MOVE);
                break;

            case SOL_DRAW:
#ifdef SOL_DRAW_PRE
                if(lastbutton != SOL_DRAW_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_DRAW);
                break;

            case SOL_CUR2STACK:
#ifdef SOL_CUR2STACK_PRE
                if(lastbutton != SOL_CUR2STACK_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_CUR2STACK);
                break;

            case SOL_REM2STACK:
#ifdef SOL_REM2STACK_PRE
                if(lastbutton != SOL_REM2STACK_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_REM2STACK);
                break;

            case SOL_REM2CUR:
#ifdef SOL_REM2CUR_PRE
                if(lastbutton != SOL_REM2CUR_PRE)
                    break;
#endif
                rb->splash(HZ*2, true, HELP_SOL_REM2CUR);
                break;

            case SOL_QUIT:
                return HELP_QUIT;
                
            default:
                if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
                    return HELP_USB;
                break;
        }
        if(button != BUTTON_NONE)
            lastbutton = button;
    }
}

/* menu return codes */
#define MENU_RESUME 0
#define MENU_RESTART 1
#define MENU_HELP 2
#define MENU_QUIT 3
#define MENU_USB 4

/* menu item number */
#define MENU_LENGTH 4

/* different menu behaviors */
#define MENU_BEFOREGAME 0
#define MENU_DURINGGAME 1

/* the menu */
/* text displayed changes depending on the 'when' parameter */
int solitaire_menu(unsigned char when) {
    
    static char menu[2][MENU_LENGTH][13] = 
        { { "Start Game",
            "",
            "Help",
            "Quit" },
          { "Resume Game",
            "Restart Game",
            "Help",
            "Quit"}
        };
    
    int i;
    int cursor=0;
    int button;
    
    if(when!=MENU_BEFOREGAME && when!=MENU_DURINGGAME)
        when = MENU_DURINGGAME;
    
    while(1){
        
        rb->lcd_clear_display();

        rb->lcd_putsxy(20, 1, "Solitaire");
        
        for(i = 0; i<MENU_LENGTH; i++){
            rb->lcd_putsxy(1, 17+9*i, menu[when][i]);
            if(cursor == i)
                rb->lcd_invertrect(0,17-1+9*i, LCD_WIDTH, 9);
        }

        rb->lcd_update();

        button = rb->button_get(true);
        switch(button){
            case BUTTON_UP:
                cursor = (cursor + MENU_LENGTH - 1)%MENU_LENGTH;
                break;

            case BUTTON_DOWN:
                cursor = (cursor + 1)%MENU_LENGTH;
                break;

            case BUTTON_LEFT:
                return MENU_RESUME;

            case SOL_MENU_RUN:
#ifdef SOL_MENU_RUN2
            case SOL_MENU_RUN2:
#endif
                switch(cursor){
                    case MENU_RESUME:
                    case MENU_RESTART:
                    case MENU_QUIT:
                        return cursor;

                    case MENU_HELP:
                        if(solitaire_help() == HELP_USB)
                            return MENU_USB;
                        break;
                }
                break;
            
            case SOL_MENU_INFO:
#if defined(SOL_MENU_INFO2) && defined(SOL_MENU_INFO3)
            case SOL_MENU_INFO2:
            case SOL_MENU_INFO3:
#endif
                rb->splash(HZ, true, "Solitaire for Rockbox by dionoea");
                break;

            case BUTTON_OFF:
                return MENU_QUIT;

            default:
                if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
                    return MENU_USB;
                break;
        }
    }
}

/* player's cursor */
unsigned char cur_card;
/* player's cursor column num */
unsigned char cur_col;

/* selected card */
unsigned char sel_card;

/* the deck */
card deck[COLORS * CARDS_PER_COLOR];

/* the remaining cards */
unsigned char rem;
unsigned char cur_rem;

/* the 7 game columns */
unsigned char cols[COL_NUM];

/* the 4 final color stacks */
unsigned char stacks[COLORS];

/* initialize the game */
void solitaire_init(void){
    unsigned char c;
    int i,j;

    /* init deck */
    for(i=0;i<COLORS;i++){
        for(j=0;j<CARDS_PER_COLOR;j++){
            deck[i*CARDS_PER_COLOR+j].color = i;
            deck[i*CARDS_PER_COLOR+j].num = j;
            deck[i*CARDS_PER_COLOR+j].known = 1;
            deck[i*CARDS_PER_COLOR+j].used = 0;
            deck[i*CARDS_PER_COLOR+j].next = NOT_A_CARD;
        }
    }
    
    /* deal the cards ... */
    /* ... in the columns */
    for(i=0; i<COL_NUM; i++){
        c = NOT_A_CARD;
        for(j=0; j<=i; j++){
            if(c == NOT_A_CARD){
                cols[i] = next_random_card(deck);
                c = cols[i];
            } else {
                deck[c].next = next_random_card(deck);
                c = deck[c].next;
            }
            if(j<i) deck[c].known = 0;
        }
    }

    /* ... shuffle what's left of the deck */
    rem = next_random_card(deck);
    c = rem;
    
    for(i=1; i<COLORS * CARDS_PER_COLOR - COL_NUM * (COL_NUM + 1)/2; i++){
        deck[c].next = next_random_card(deck);
        c = deck[c].next;
    }

    /* we now finished dealing the cards. The game can start ! (at last) */
    
    /* init the stack */
    for(i = 0; i<COLORS;i++){
        stacks[i] = NOT_A_CARD;
    }
    
    /* the cursor starts on upper left card */
    cur_card = cols[0];
    cur_col = 0;
    
    /* no card is selected */
    sel_card = NOT_A_CARD;
    
    /* init the remainder */
    cur_rem = NOT_A_CARD;
} 

/* find the column number in which 'card' can be found */
unsigned char find_card_col(unsigned char card){
    int i;
    unsigned char c;

    if(card == NOT_A_CARD) return NOT_A_COL;

    for(i=0; i<COL_NUM; i++){
        c = cols[i];
        while(c!=NOT_A_CARD){
            if(c == card) return i;
            c = deck[c].next;
        }
    }

    for(i=0; i<COLORS; i++){
        c = stacks[i];
        while(c!=NOT_A_CARD){
            if(c == card) return STACKS_COL + i;
            c = deck[c].next;
        }
    }

    return REM_COL;
}

/* find the card preceding 'card' */
/* if it doesn't exist, return NOT_A_CARD */
unsigned char find_prev_card(unsigned char card){
    int i;

    for(i=0; i<COLORS*CARDS_PER_COLOR; i++){
        if(deck[i].next == card) return i;
    }

    return NOT_A_CARD;
}

/* find the last card of a given column */
unsigned char find_last_card(unsigned char col){
    unsigned char c;

    if(col < COL_NUM){
        c = cols[col];
    } else if(col < REM_COL){
        c = stacks[col - STACKS_COL];
    } else {
        c = rem;
    }

    if(c == NOT_A_CARD)
        return c;
    else {
        while(deck[c].next != NOT_A_CARD){
            c = deck[c].next;
        }
        return c;
    }
}

#define MOVE_OK 0
#define MOVE_NOT_OK 1
unsigned char move_card(unsigned char dest_col, unsigned char src_card){
    /* the column on which to take src_card */
    unsigned char src_col;

    /* the last card of dest_col */
    unsigned char dest_card;
    
    /* the card under src_card */
    unsigned char src_card_prev;
    
    /* you can't move no card (at least, it doesn't have any consequence) */
    if(src_card == NOT_A_CARD) return MOVE_NOT_OK;
    /* you can't put a card back on the remains' stack */
    if(dest_col == REM_COL) return MOVE_NOT_OK;
    
    src_col = find_card_col(src_card);
    dest_card = find_last_card(dest_col);
    src_card_prev = find_prev_card(src_card);

    /* you can't move more than one card at a time from the colors stack */
    /* to the rest of the game */
    if(src_col >= COL_NUM && src_col < REM_COL
       && deck[src_card].next != NOT_A_CARD){
        return MOVE_NOT_OK;
    }
    
    /* if we (that means dest) are on one of the 7 columns ... */
    if(dest_col < COL_NUM){
        /* ... check is we are on an empty color and that the src is a king */
        if(dest_card == NOT_A_CARD
           && deck[src_card].num == CARDS_PER_COLOR - 1){
            /* this is a winning combination */
            cols[dest_col] = src_card;
        }
        /* ... or check if the cards follow one another and have same color */
        else if((deck[dest_card].color + deck[src_card].color)%2==1
           && deck[dest_card].num == deck[src_card].num + 1){
            /* this is a winning combination */
            deck[dest_card].next = src_card;
        }
        /* ... or, humpf, well that's not good news */
        else {
            /* this is not a winning combination */
            return MOVE_NOT_OK;
        }
    }
    /* if we are on one of the 4 color stacks ... */
    else if(dest_col < REM_COL){
        /* ... check if we are on an empty stack, that the src is an
         * ace and that this is the good color stack */
        if(dest_card == NOT_A_CARD
           && deck[src_card].num == 0
           && deck[src_card].color == dest_col - STACKS_COL){
            /* this is a winning combination */
            stacks[dest_col - STACKS_COL] = src_card;
        }
        /* ... or check if the cards follow one another, have the same 
         * color and {that src has no .next element or is from the remains'
         * stack} */
        else if(deck[dest_card].color == deck[src_card].color
           && deck[dest_card].num + 1 == deck[src_card].num
           && (deck[src_card].next == NOT_A_CARD || src_col == REM_COL) ){
            /* this is a winning combination */
            deck[dest_card].next = src_card;
        }
        /* ... or, well that's not good news */
        else {
            /* this is not a winnong combination */
            return MOVE_NOT_OK;
        }
    }
    /* if we are on the remains' stack */
    else {
        /* you can't move a card back to the remains' stack */
        return MOVE_NOT_OK;
    }
    
    /* if the src card is from the remains' stack, we don't want to take
     * the following cards */
    if(src_col == REM_COL){
        /* if src card is the first card from the stack */
        if(src_card_prev == NOT_A_CARD){
            rem = deck[src_card].next;
        }
        /* if src card is not the first card from the stack */
        else {
            deck[src_card_prev].next = deck[src_card].next;
        }
        cur_rem = src_card_prev;
        deck[src_card].next = NOT_A_CARD;
    }
    /* if the src card is from somewhere else, just take everything */
    else {
        if(src_card_prev == NOT_A_CARD){
            if(src_col < COL_NUM){
                cols[src_col] = NOT_A_CARD;
            } else {
                stacks[src_col - STACKS_COL] = NOT_A_CARD;
            }
        } else {
            deck[src_card_prev].next = NOT_A_CARD;
        }
    }

    /* tada ! */
    return MOVE_OK;
}

#define SOLITAIRE_WIN 0
#define SOLITAIRE_QUIT 1
#define SOLITAIRE_USB 2

/* the game */
int solitaire(void){
    
    int i,j;
    int button, lastbutton = 0;
    unsigned char c;
    int biggest_col_length;
    
    switch(solitaire_menu(MENU_BEFOREGAME)) {
        case MENU_QUIT:
            return SOLITAIRE_QUIT;

        case MENU_USB:
            return SOLITAIRE_USB;
    }

    solitaire_init();
   
    while(true){
        
        rb->lcd_clear_display();

        /* get the biggest column length so that display can be "optimized" */
        biggest_col_length = 0;
        
        for(i=0;i<COL_NUM;i++){
            j = 0;
            c = cols[i];
            while(c != NOT_A_CARD){
                j++;
                c = deck[c].next;
            }
            if(j>biggest_col_length) biggest_col_length = j;
        }
        
        /* check if there are cards remaining in the game. */
        /* if there aren't any, that means you won :) */
        if(biggest_col_length == 0 && rem == NOT_A_CARD){
            rb->splash(HZ*2, true, "You Won :)");
            return SOLITAIRE_WIN;
        }
        
        /* draw the columns */
        for(i=0;i<COL_NUM;i++){
            c = cols[i];
            j = 0;
            while(true){
                if(c==NOT_A_CARD) {
                    /* draw the cursor on empty columns */
                    if(cur_col == i){
                        rb->lcd_invertrect(i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+2, 2, CARD_WIDTH-3, CARD_HEIGHT-1);
                    }
                    break;
                }
                /* clear the card's spot */
                rb->lcd_clearrect(i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM, j+1, CARD_WIDTH, CARD_HEIGHT-1);
                /* known card */
                if(deck[c].known){
                    rb->lcd_bitmap(numbers[deck[c].num], i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+1, j, 8, 8, true);
                    rb->lcd_bitmap(colors[deck[c].color], i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+7, j, 8, 8, true);
                }
                /* draw top line of the card */
                rb->lcd_drawline(i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+1,j,i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+CARD_WIDTH-1,j);
                /* selected card */
                if(c == sel_card && sel_card != NOT_A_CARD){
                     rb->lcd_drawrect(i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+1, j+1, CARD_WIDTH-1, CARD_HEIGHT-1);
                }
                /* cursor (or not) */
                if(c == cur_card){
                    rb->lcd_invertrect(i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+1, j+1, CARD_WIDTH-1, CARD_HEIGHT-1);
                    /* go to the next card */
                    c = deck[c].next;
                    if(c == NOT_A_CARD) break;
                    j += CARD_HEIGHT - 2;
                } else {
                    /* go to the next card */
                    c = deck[c].next;
                    if(c == NOT_A_CARD) break;
                    j += min(CARD_HEIGHT - 2, (LCD_HEIGHT - CARD_HEIGHT)/biggest_col_length);
                }
            }
            if(cols[i]!=NOT_A_CARD){
                /* draw line to the left of the column */
                rb->lcd_drawline(i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM,1,i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM,j+CARD_HEIGHT-1);
                /* draw line to the right of the column */
                rb->lcd_drawline(i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+CARD_WIDTH,1,i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+CARD_WIDTH,j+CARD_HEIGHT-1);
                /* draw bottom of the last card */
                rb->lcd_drawline(i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+1,j+CARD_HEIGHT,i*(LCD_WIDTH - CARD_WIDTH)/COL_NUM+CARD_WIDTH-1,j+CARD_HEIGHT);
            }
        }

        /* draw the stacks */
        for(i=0; i<COLORS; i++){
            c = stacks[i];
            if(c!=NOT_A_CARD){
                while(deck[c].next != NOT_A_CARD){
                    c = deck[c].next;
                }
            }
            if(c != NOT_A_CARD) {
                rb->lcd_bitmap(numbers[deck[c].num], LCD_WIDTH - CARD_WIDTH+1, i*CARD_HEIGHT, 8, 8, true);
            }
            rb->lcd_bitmap(colors[i], LCD_WIDTH - CARD_WIDTH+7, i*CARD_HEIGHT, 8, 8, true);
            /* draw a selected card */
            if(c != NOT_A_CARD) {
                if(sel_card == c){
                    rb->lcd_drawrect(LCD_WIDTH - CARD_WIDTH+1, i*CARD_HEIGHT + 1, CARD_WIDTH-1, CARD_HEIGHT-1);
                }
            }
            rb->lcd_drawline(LCD_WIDTH - CARD_WIDTH+1,i*CARD_HEIGHT,LCD_WIDTH - 1,i*CARD_HEIGHT);
            rb->lcd_drawline(LCD_WIDTH - CARD_WIDTH,i*CARD_HEIGHT+1,LCD_WIDTH - CARD_WIDTH,(i+1)*CARD_HEIGHT-1);
            rb->lcd_drawline(LCD_WIDTH - CARD_WIDTH+1,(i+1)*CARD_HEIGHT,LCD_WIDTH - 1,(i+1)*CARD_HEIGHT);
            /* draw the cursor on one of the stacks */
            if(cur_col == STACKS_COL + i){
                rb->lcd_invertrect(LCD_WIDTH - CARD_WIDTH+1, i*CARD_HEIGHT + 1, CARD_WIDTH-1, CARD_HEIGHT-1);
            }
        }

        /* draw the remains */
        if(rem != NOT_A_CARD) {
            rb->lcd_drawline(LCD_WIDTH - CARD_WIDTH+1,LCD_HEIGHT-CARD_HEIGHT-1,LCD_WIDTH - 1,LCD_HEIGHT-CARD_HEIGHT-1);
            rb->lcd_drawline(LCD_WIDTH - CARD_WIDTH,LCD_HEIGHT-CARD_HEIGHT,LCD_WIDTH - CARD_WIDTH,LCD_HEIGHT-2);
            rb->lcd_drawline(LCD_WIDTH - CARD_WIDTH+1,LCD_HEIGHT-1,LCD_WIDTH - 1,LCD_HEIGHT-1);
            if(cur_rem != NOT_A_CARD){
                rb->lcd_bitmap(numbers[deck[cur_rem].num], LCD_WIDTH - CARD_WIDTH+1, LCD_HEIGHT-CARD_HEIGHT, 8, 8, true);
                 rb->lcd_bitmap(colors[deck[cur_rem].color], LCD_WIDTH - CARD_WIDTH+7, LCD_HEIGHT-CARD_HEIGHT, 8, 8, true);
                 /* draw a selected card */
                 if(sel_card == cur_rem){
                    rb->lcd_drawrect(LCD_WIDTH - CARD_WIDTH+1, LCD_HEIGHT-CARD_HEIGHT,CARD_WIDTH-1, CARD_HEIGHT-1);
                 }
            }
        }
        /* draw the cursor */
        if(cur_col == REM_COL){
            rb->lcd_invertrect(LCD_WIDTH - CARD_WIDTH+1, LCD_HEIGHT-CARD_HEIGHT,CARD_WIDTH-1, CARD_HEIGHT-1);
        }
            

        rb->lcd_update();
        
        /* what to do when a key is pressed ... */
        button = rb->button_get(true);
        switch(button){
            
            /* move cursor to the last card of the previous column */
            /* or to the previous color stack */
            /* or to the remains stack */
            case SOL_RIGHT:
#ifdef SOL_RIGHT_PRE
                if(lastbutton != SOL_RIGHT_PRE)
                    break;
#endif
                if(cur_col >= COL_NUM){
                    cur_col = 0;
                } else if(cur_col == COL_NUM - 1){
                    cur_col = REM_COL;
                } else {
                    cur_col = (cur_col+1)%(REM_COL+1);
                }
                if(cur_col == REM_COL){
                    cur_card = cur_rem;
                    break;
                }
                cur_card  = find_last_card(cur_col);
                break;
            
            /* move cursor to the last card of the next column */
            /* or to the next color stack */
            /* or to the remains stack */
            case SOL_LEFT:
#ifdef SOL_LEFT_PRE
                if(lastbutton != SOL_LEFT_PRE)
                    break;
#endif
                if(cur_col == 0){
                    cur_col = REM_COL;
                } else if(cur_col >= COL_NUM) {
                    cur_col = COL_NUM - 1;
                } else {
                    cur_col = (cur_col + REM_COL)%(REM_COL+1);
                }
                if(cur_col == REM_COL){
                    cur_card = cur_rem;
                    break;
                }
                cur_card = find_last_card(cur_col);
                break;
            
            /* move cursor to card that's bellow */
            case SOL_DOWN:
#ifdef SOL_DOWN_PRE
                if(lastbutton != SOL_DOWN_PRE)
                    break;
#endif
                if(cur_col >= COL_NUM) {
                    cur_col = (cur_col - COL_NUM + 1)%(COLORS + 1) + COL_NUM;
                    if(cur_col == REM_COL){
                        cur_card = cur_rem;
                    } else {
                        cur_card = find_last_card(cur_col);
                    }
                    break;
                }
                if(cur_card == NOT_A_CARD) break;
                if(deck[cur_card].next != NOT_A_CARD){
                    cur_card = deck[cur_card].next;
                } else {
                    cur_card = cols[cur_col];
                    while(deck[cur_card].known == 0
                          && deck[cur_card].next != NOT_A_CARD){
                        cur_card = deck[cur_card].next;
                    }
                }
                break;
            
            /* move cursor to card that's above */
            case SOL_UP:
#ifdef SOL_UP_PRE
                if(lastbutton != SOL_UP_PRE)
                    break;
#endif
                if(cur_col >= COL_NUM) {
                    cur_col = (cur_col - COL_NUM + COLORS)%(COLORS + 1) + COL_NUM;
                    if(cur_col == REM_COL){
                        cur_card = cur_rem;
                    } else {
                        cur_card = find_last_card(cur_col);
                    }
                    break;
                }
                if(cur_card == NOT_A_CARD) break;
                do{
                    cur_card = find_prev_card(cur_card);
                    if(cur_card == NOT_A_CARD){
                        cur_card = find_last_card(cur_col);
                    }
                } while (deck[cur_card].next != NOT_A_CARD
                         && deck[cur_card].known == 0);
                break;

            /* Try to put card under cursor on one of the stacks */
            case SOL_CUR2STACK:
#ifdef SOL_CUR2STACK_PRE
                if(lastbutton != SOL_CUR2STACK_PRE)
                    break;
#endif
                if(cur_card != NOT_A_CARD){
                    move_card(deck[cur_card].color + STACKS_COL, cur_card);
                }
                break;

            /* Move cards arround, Uncover cards, ... */
            case SOL_MOVE:
#ifdef SOL_MOVE_PRE
                if(lastbutton != SOL_MOVE_PRE)
                    break;
#endif
                if(sel_card == NOT_A_CARD) {
                    if(cur_card != NOT_A_CARD){
                        /* reveal a hidden card */
                        if(deck[cur_card].next == NOT_A_CARD && deck[cur_card].known==0){
                            deck[cur_card].known = 1;
                        /* select a card */
                        } else {
                            sel_card = cur_card;
                        }
                    }
                /* unselect card or try putting card on one of the 4 stacks */
                } else if(sel_card == cur_card) {
                    move_card(deck[sel_card].color + COL_NUM, sel_card);
                    sel_card = NOT_A_CARD;
                /* try moving cards */
                } else {
                    if(move_card(cur_col, sel_card) == MOVE_OK){
                        sel_card = NOT_A_CARD;
                    }
                }
                break;
            
            /* If the card on the top of the remains can be put where */
            /* the cursor is, go ahead */
            case SOL_REM2CUR:
#ifdef SOL_REM2CUR_PRE
                if(lastbutton != SOL_REM2CUR_PRE)
                    break;
#endif
                move_card(cur_col, cur_rem);
                break;

            /* If the card on top of the remains can be put on one */
            /* of the stacks, do so */
            case SOL_REM2STACK:
#ifdef SOL_REM2STACK_PRE
                if(lastbutton != SOL_REM2STACK_PRE)
                    break;
#endif
                if(cur_rem != NOT_A_CARD){
                    move_card(deck[cur_rem].color + COL_NUM, cur_rem);
                }
                break;

            /* unselect selected card or ... */
            /* draw new cards from the remains of the deck */
            case SOL_DRAW:
#ifdef SOL_DRAW_PRE
                if(lastbutton != SOL_DRAW_PRE)
                    break;
#endif
                if(sel_card != NOT_A_CARD){
                    /* unselect selected card */
                    sel_card = NOT_A_CARD;
                    break;
                }
                if(rem != NOT_A_CARD) {
                    /* draw new cards form the remains of the deck */
                    if(cur_rem == NOT_A_CARD){
                        cur_rem = rem;
                        i = CARDS_PER_DRAW - 1;
                    } else {
                        i = CARDS_PER_DRAW;
                    }
                    while(i>0 && deck[cur_rem].next != NOT_A_CARD){
                        cur_rem = deck[cur_rem].next;
                        i--;
                    }
                    /* test if any cards are really left on */
                    /* the remains' stack */
                    if(i == CARDS_PER_DRAW){
                        cur_rem = NOT_A_CARD;
                    }
                }
                break;
            
            /* Show the menu */
            case SOL_QUIT:
                switch(solitaire_menu(MENU_DURINGGAME)){
                    case MENU_QUIT:
                        return SOLITAIRE_QUIT;
                        
                    case MENU_USB:
                        return SOLITAIRE_USB;

                    case MENU_RESTART:
                        solitaire_init();
                        break;
                }
            default:
                if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
                    return SOLITAIRE_USB;
                break;
        }
        
        if(button != BUTTON_NONE)
            lastbutton = button;

        /* fix incoherences concerning cur_col and cur_card */
        c = find_card_col(cur_card);
        if(c != NOT_A_COL && c != cur_col)
            cur_card = find_last_card(cur_col);
    }
}

enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    int result;

    /* plugin init */
    TEST_PLUGIN_API(api);
    (void)parameter;
    rb = api;
    /* end of plugin init */
    
    /* Welcome to Solitaire ! */
    rb->splash(HZ*2, true, "Welcome to Solitaire !");
    
    /* play the game :) */
    /* Keep playing if a game was won (that means display the menu after */
    /* winning instead of quiting) */
    while((result = solitaire()) == SOLITAIRE_WIN);

    /* Exit the plugin */
    return (result == SOLITAIRE_USB) ? PLUGIN_USB_CONNECTED : PLUGIN_OK;
}

#endif /* iRiver h100 keypad */
#endif