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
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2006 Tom Ross
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "plugin.h"
#include "card_deck.h"
#include "card_back.h"
PLUGIN_HEADER
/* save files */
#define SCORE_FILE PLUGIN_GAMES_DIR "/blackjack.score"
#define SAVE_FILE PLUGIN_GAMES_DIR "/blackjack.save"
#define NUM_SCORES LCD_HEIGHT/8-2
/* final game return status */
#define BJ_END 3
#define BJ_USB 2
#define BJ_QUIT 1
#define BJ_LOSE 0
#if CONFIG_KEYPAD == RECORDER_PAD
#define BJACK_START BUTTON_ON
#define BJACK_QUIT BUTTON_OFF
#define BJACK_MAX (BUTTON_ON|BUTTON_UP)
#define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
#define BJACK_HIT BUTTON_F1
#define BJACK_STAY BUTTON_F2
#define BJACK_DOUBLEDOWN BUTTON_F3
#define BJACK_SCORES BUTTON_RIGHT
#define BJACK_RESUME BUTTON_PLAY
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == ONDIO_PAD
#define BJACK_START BUTTON_MENU
#define BJACK_QUIT BUTTON_OFF
#define BJACK_MAX (BUTTON_MENU|BUTTON_UP)
#define BJACK_MIN (BUTTON_MENU|BUTTON_DOWN)
#define BJACK_HIT BUTTON_LEFT
#define BJACK_STAY BUTTON_RIGHT
#define BJACK_DOUBLEDOWN BUTTON_UP
#define BJACK_SCORES BUTTON_UP
#define BJACK_RESUME BUTTON_DOWN
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
#define BJACK_START BUTTON_PLAY
#define BJACK_QUIT BUTTON_POWER
#define BJACK_MAX (BUTTON_PLAY|BUTTON_SCROLL_UP)
#define BJACK_MIN (BUTTON_PLAY|BUTTON_SCROLL_DOWN)
#define BJACK_HIT BUTTON_PLAY
#define BJACK_STAY BUTTON_FF
#define BJACK_DOUBLEDOWN BUTTON_REW
#define BJACK_SCORES BUTTON_LEFT
#define BJACK_RESUME BUTTON_RIGHT
#define BJACK_UP BUTTON_SCROLL_UP
#define BJACK_DOWN BUTTON_SCROLL_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
(CONFIG_KEYPAD == IRIVER_H300_PAD)
#define BJACK_START BUTTON_ON
#define BJACK_QUIT BUTTON_OFF
#define BJACK_MAX (BUTTON_ON|BUTTON_UP)
#define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
#define BJACK_HIT BUTTON_ON
#define BJACK_STAY BUTTON_REC
#define BJACK_DOUBLEDOWN BUTTON_SELECT
#define BJACK_SCORES BUTTON_SELECT
#define BJACK_RESUME BUTTON_MODE
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
(CONFIG_KEYPAD == IPOD_3G_PAD) || \
(CONFIG_KEYPAD == IPOD_1G2G_PAD)
#define BJACK_START BUTTON_SELECT
#define BJACK_QUIT BUTTON_MENU
#define BJACK_MAX (BUTTON_SELECT|BUTTON_SCROLL_FWD)
#define BJACK_MIN (BUTTON_SELECT|BUTTON_SCROLL_BACK)
#define BJACK_HIT BUTTON_SELECT
#define BJACK_STAY BUTTON_RIGHT
#define BJACK_DOUBLEDOWN BUTTON_LEFT
#define BJACK_SCORES BUTTON_RIGHT
#define BJACK_RESUME BUTTON_PLAY
#define BJACK_UP BUTTON_SCROLL_FWD
#define BJACK_DOWN BUTTON_SCROLL_BACK
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
#define BJACK_START BUTTON_PLAY
#define BJACK_QUIT BUTTON_POWER
#define BJACK_MAX (BUTTON_PLAY|BUTTON_UP)
#define BJACK_MIN (BUTTON_PLAY|BUTTON_DOWN)
#define BJACK_HIT BUTTON_SELECT
#define BJACK_STAY BUTTON_REC
#define BJACK_DOUBLEDOWN BUTTON_PLAY
#define BJACK_SCORES BUTTON_RIGHT
#define BJACK_RESUME BUTTON_DOWN
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
#define BJACK_START BUTTON_MODE
#define BJACK_QUIT BUTTON_PLAY
#define BJACK_MAX (BUTTON_EQ|BUTTON_UP)
#define BJACK_MIN (BUTTON_EQ|BUTTON_DOWN)
#define BJACK_HIT BUTTON_EQ
#define BJACK_STAY BUTTON_MODE
#define BJACK_DOUBLEDOWN BUTTON_SELECT
#define BJACK_SCORES BUTTON_SELECT
#define BJACK_RESUME (BUTTON_EQ|BUTTON_MODE)
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == GIGABEAT_PAD
#define BJACK_START BUTTON_POWER
#define BJACK_QUIT BUTTON_A
#define BJACK_MAX BUTTON_VOL_UP
#define BJACK_MIN BUTTON_VOL_DOWN
#define BJACK_HIT BUTTON_VOL_UP
#define BJACK_STAY BUTTON_VOL_DOWN
#define BJACK_DOUBLEDOWN BUTTON_SELECT
#define BJACK_SCORES BUTTON_RIGHT
#define BJACK_RESUME BUTTON_MENU
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == SANSA_E200_PAD
#define BJACK_START BUTTON_SELECT
#define BJACK_QUIT BUTTON_POWER
#define BJACK_MAX (BUTTON_REC|BUTTON_UP)
#define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
#define BJACK_HIT BUTTON_SELECT
#define BJACK_STAY BUTTON_RIGHT
#define BJACK_DOUBLEDOWN BUTTON_LEFT
#define BJACK_SCORES BUTTON_UP
#define BJACK_RESUME BUTTON_REC
#define BJACK_UP BUTTON_SCROLL_FWD
#define BJACK_DOWN BUTTON_SCROLL_BACK
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == SANSA_C200_PAD
#define BJACK_START BUTTON_SELECT
#define BJACK_QUIT BUTTON_POWER
#define BJACK_MAX BUTTON_VOL_UP
#define BJACK_MIN BUTTON_VOL_DOWN
#define BJACK_HIT BUTTON_SELECT
#define BJACK_STAY BUTTON_RIGHT
#define BJACK_DOUBLEDOWN BUTTON_LEFT
#define BJACK_SCORES BUTTON_REC
#define BJACK_RESUME BUTTON_DOWN
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == ELIO_TPJ1022_PAD
#define BJACK_START BUTTON_MAIN
#define BJACK_QUIT BUTTON_POWER
#define BJACK_MAX (BUTTON_REC|BUTTON_UP)
#define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
#define BJACK_HIT BUTTON_MAIN
#define BJACK_STAY BUTTON_MENU
#define BJACK_DOUBLEDOWN BUTTON_DOWN
#define BJACK_SCORES BUTTON_UP
#define BJACK_RESUME BUTTON_FF
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == GIGABEAT_S_PAD
#define BJACK_START BUTTON_PLAY
#define BJACK_QUIT BUTTON_BACK
#define BJACK_MAX BUTTON_VOL_UP
#define BJACK_MIN BUTTON_VOL_DOWN
#define BJACK_HIT BUTTON_VOL_UP
#define BJACK_STAY BUTTON_VOL_DOWN
#define BJACK_DOUBLEDOWN BUTTON_SELECT
#define BJACK_SCORES BUTTON_RIGHT
#define BJACK_RESUME BUTTON_MENU
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == MROBE100_PAD
#define BJACK_START BUTTON_SELECT
#define BJACK_QUIT BUTTON_POWER
#define BJACK_MAX BUTTON_MENU
#define BJACK_MIN BUTTON_DISPLAY
#define BJACK_HIT BUTTON_MENU
#define BJACK_STAY BUTTON_DISPLAY
#define BJACK_DOUBLEDOWN BUTTON_SELECT
#define BJACK_SCORES BUTTON_RIGHT
#define BJACK_RESUME BUTTON_PLAY
#define BJACK_UP BUTTON_UP
#define BJACK_DOWN BUTTON_DOWN
#define BJACK_RIGHT BUTTON_RIGHT
#define BJACK_LEFT BUTTON_LEFT
#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
#define BJACK_START BUTTON_RC_PLAY
#define BJACK_QUIT BUTTON_RC_REC
#define BJACK_MAX (BUTTON_RC_PLAY|BUTTON_RC_VOL_UP)
#define BJACK_MIN (BUTTON_RC_PLAY|BUTTON_RC_VOL_DOWN)
#define BJACK_HIT BUTTON_RC_PLAY
#define BJACK_STAY BUTTON_RC_FF
#define BJACK_DOUBLEDOWN BUTTON_RC_REW
#define BJACK_SCORES BUTTON_RC_MENU
#define BJACK_RESUME BUTTON_RC_MODE
#define BJACK_UP BUTTON_RC_VOL_UP
#define BJACK_DOWN BUTTON_RC_VOL_DOWN
#define BJACK_RIGHT BUTTON_RC_FF
#define BJACK_LEFT BUTTON_RC_REW
#elif CONFIG_KEYPAD == COWOND2_PAD
#define BJACK_QUIT BUTTON_POWER
#define BJACK_DOUBLEDOWN BUTTON_MINUS
#define BJACK_SCORES BUTTON_MENU
#else
#error No keymap defined!
#endif
#ifdef HAVE_TOUCHPAD
#ifndef BJACK_START
#define BJACK_START BUTTON_CENTER
#endif
#ifndef BJACK_HIT
#define BJACK_HIT BUTTON_CENTER
#endif
#ifndef BJACK_MAX
#define BJACK_MAX BUTTON_TOPRIGHT
#endif
#ifndef BJACK_MIN
#define BJACK_MIN BUTTON_TOPLEFT
#endif
#ifndef BJACK_RESUME
#define BJACK_RESUME BUTTON_BOTTOMRIGHT
#endif
#ifndef BJACK_STAY
#define BJACK_STAY BUTTON_BOTTOMLEFT
#endif
#ifndef BJACK_UP
#define BJACK_UP BUTTON_TOPMIDDLE
#endif
#ifndef BJACK_DOWN
#define BJACK_DOWN BUTTON_BOTTOMMIDDLE
#endif
#ifndef BJACK_RIGHT
#define BJACK_RIGHT BUTTON_MIDRIGHT
#endif
#ifndef BJACK_LEFT
#define BJACK_LEFT BUTTON_MIDLEFT
#endif
#endif
#ifdef HAVE_LCD_COLOR
#define BG_COLOR LCD_RGBPACK(0,157,0)
#define FG_COLOR LCD_WHITE
#elif LCD_DEPTH > 1
#define BG_COLOR LCD_WHITE
#define FG_COLOR LCD_BLACK
#endif
#define CARD_WIDTH BMPWIDTH_card_back
#define CARD_HEIGHT BMPHEIGHT_card_back
/* This is the max amount of cards onscreen before condensing */
#define MAX_CARDS LCD_WIDTH/(CARD_WIDTH+4)
extern const fb_data card_deck[];
extern const fb_data card_back[];
#define NEXT_CARD bj->player_cards[done][bj->num_player_cards[done]]
/* global rockbox api */
static const struct plugin_api* rb;
MEM_FUNCTION_WRAPPERS(rb);
/* dealer and player card positions */
unsigned int dealer_x, dealer_y, player_x, player_y;
typedef struct card {
unsigned int value; /* Card's value in Blackjack */
unsigned int num; /* Value on card face 0-12 (0=Ace, 1=2, 11=Q) */
unsigned int suit; /* 0:Spades, 1:Hearts, 2: Clubs; 3: Diamonds */
bool is_soft_ace;
} card;
typedef struct game_context {
struct card player_cards[2][22]; /* 22 Cards means the deal was all aces */
struct card dealer_cards[22]; /* That is the worst-case scenario */
unsigned int player_total;
unsigned int dealer_total;
signed int player_money;
unsigned int num_player_cards[2];
unsigned int num_dealer_cards;
unsigned int current_bet;
unsigned int split_status; /* 0 = split hasn't been asked, *
* 1 = split did not occur *
* 2 = split occurred *
* 3 = split occurred and 1st hand done */
bool is_blackjack;
bool end_hand;
bool asked_insurance;
signed short highscores[NUM_SCORES];
bool resume;
bool dirty;
} game_context;
/*****************************************************************************
* blackjack_init() initializes blackjack data structures.
******************************************************************************/
static void blackjack_init(struct game_context* bj) {
/* seed the rand generator */
rb->srand(*rb->current_tick);
/* reset card positions */
dealer_x = 4;
dealer_y = LCD_HEIGHT/4 - CARD_HEIGHT/2;
player_x = 4;
player_y = LCD_HEIGHT - LCD_HEIGHT/4 - CARD_HEIGHT/2;
/* check for resumed game */
if(bj->resume) return;
/* reset scoring */
bj->player_total = 0;
bj->dealer_total = 0;
bj->num_player_cards[0] = 2;
bj->num_player_cards[1] = 0;
bj->num_dealer_cards = 2;
bj->end_hand = false;
bj->split_status = 0;
bj->is_blackjack = false;
bj->asked_insurance = false;
}
/*****************************************************************************
* blackjack_drawtable() draws the table and some text.
******************************************************************************/
static void blackjack_drawtable(struct game_context* bj) {
unsigned int w, h, y_loc;
char str[10];
#if LCD_HEIGHT <= 64
rb->lcd_getstringsize("Bet", &w, &h);
rb->lcd_putsxy(LCD_WIDTH - w, 2*h + 1, "Bet");
rb->snprintf(str, 9, "$%d", bj->current_bet);
rb->lcd_getstringsize(str, &w, &h);
rb->lcd_putsxy(LCD_WIDTH - w, 3*h + 1, str);
y_loc = LCD_HEIGHT/2;
#else
rb->lcd_getstringsize("Bet", &w, &h);
rb->lcd_putsxy(LCD_WIDTH - w, 5*h / 2, "Bet");
rb->snprintf(str, 9, "$%d", bj->current_bet);
rb->lcd_getstringsize(str, &w, &h);
rb->lcd_putsxy(LCD_WIDTH - w, 7*h / 2, str);
rb->lcd_hline(0, LCD_WIDTH, LCD_HEIGHT/2);
y_loc = LCD_HEIGHT/2 + h;
#endif
rb->lcd_putsxy(0,0, "Dealer");
rb->lcd_getstringsize("Player", &w, &h);
rb->lcd_putsxy(0, y_loc, "Player");
rb->lcd_getstringsize("Total", &w, &h);
rb->lcd_putsxy(LCD_WIDTH - w, y_loc, "Total");
rb->lcd_getstringsize("Money", &w, &h);
rb->lcd_putsxy(LCD_WIDTH - w, 0, "Money");
rb->snprintf(str, 9, "$%d", bj->player_money - bj->current_bet);
rb->lcd_getstringsize(str, &w, &h);
rb->lcd_putsxy(LCD_WIDTH - w, h + 1, str);
rb->snprintf(str, 3, "%d", bj->player_total);
rb->lcd_getstringsize(str, &w, &h);
rb->lcd_putsxy(LCD_WIDTH - w, y_loc + h, str);
}
/*****************************************************************************
* find_value() is passed a card and returns its blackjack value.
******************************************************************************/
static unsigned int find_value(unsigned int number) {
unsigned int thisValue;
if (number == 0)
thisValue = 11; /* Aces get a value of 11 at first */
else if (number < 10)
thisValue = number + 1;
else
thisValue = 10; /* Anything 10 or higher gets a value of 10 */
return thisValue;
}
/*****************************************************************************
* draw_card() draws a card to the screen.
******************************************************************************/
static void draw_card(struct card temp_card, bool shown, unsigned int x,
unsigned int y) {
if(shown)
rb->lcd_bitmap_part(card_deck, CARD_WIDTH*temp_card.num,
CARD_HEIGHT*temp_card.suit, BMPWIDTH_card_deck,
x+1, y+1, CARD_WIDTH, CARD_HEIGHT);
else
rb->lcd_bitmap(card_back, x+1, y+1,CARD_WIDTH, CARD_HEIGHT);
#if LCD_DEPTH > 1
rb->lcd_set_foreground(LCD_BLACK);
#endif
/* Print outlines */
#if CARD_WIDTH >= 26
rb->lcd_hline(x+2, x+CARD_WIDTH-1, y);
rb->lcd_hline(x+2, x+CARD_WIDTH-1, y+CARD_HEIGHT+1);
rb->lcd_vline(x, y+2, y+CARD_HEIGHT-3);
rb->lcd_vline(x+CARD_WIDTH+1, y+2, y+CARD_HEIGHT-1);
rb->lcd_drawpixel(x+1, y+1);
rb->lcd_drawpixel(x+1, y+CARD_HEIGHT);
rb->lcd_drawpixel(x+CARD_WIDTH, y+1);
rb->lcd_drawpixel(x+CARD_WIDTH, y+CARD_HEIGHT);
#else
rb->lcd_hline(x+1, x+CARD_WIDTH, y);
rb->lcd_hline(x+1, x+CARD_WIDTH, y+CARD_HEIGHT+1);
rb->lcd_vline(x, y+1, y+CARD_HEIGHT);
rb->lcd_vline(x+CARD_WIDTH+1, y+1, y+CARD_HEIGHT);
#endif
#if LCD_DEPTH > 1
rb->lcd_set_foreground(FG_COLOR);
#endif
}
/*****************************************************************************
* new_card() initializes a new card and gives it values.
******************************************************************************/
static struct card new_card(void) {
struct card new_card;
new_card.suit = rb->rand()%4; /* Random number 0-3 */
new_card.num = rb->rand()%13; /* Random number 0-12 */
new_card.value = find_value(new_card.num);
new_card.is_soft_ace = new_card.num == 0 ? true : false;
return new_card;
}
/*****************************************************************************
* deal_init_card() deals and draws to the screen the player's and dealer's
* initial cards.
******************************************************************************/
static void deal_init_cards(struct game_context* bj) {
bj->dealer_cards[0] = new_card();
bj->dealer_total += bj->dealer_cards[0].value;
draw_card(bj->dealer_cards[0], false, dealer_x, dealer_y);
bj->dealer_cards[1] = new_card();
bj->dealer_total += bj->dealer_cards[1].value;
draw_card(bj->dealer_cards[1], true, dealer_x + CARD_WIDTH + 4, dealer_y);
bj->player_cards[0][0] = new_card();
bj->player_total += bj->player_cards[0][0].value;
draw_card(bj->player_cards[0][0], true, player_x, player_y);
player_x += CARD_WIDTH + 4;
bj->player_cards[0][1] = new_card();
bj->player_total += bj->player_cards[0][1].value;
draw_card(bj->player_cards[0][1], true, player_x, player_y);
player_x += CARD_WIDTH + 4;
}
/*****************************************************************************
* redraw_board() redraws all the cards and the board
******************************************************************************/
static void redraw_board(struct game_context* bj) {
unsigned int i, n, upper_bound;
rb->lcd_clear_display();
blackjack_drawtable(bj);
player_x = 4;
dealer_x = 4;
upper_bound = bj->split_status > 1 ? 2 : 1;
for (i = 0; i < bj->num_dealer_cards; i++) {
if (!bj->end_hand) {
draw_card(bj->dealer_cards[0], false, dealer_x, dealer_y);
/* increment i so the dealer's first card isn't displayed */
i++;
dealer_x += CARD_WIDTH + 4;
}
draw_card(bj->dealer_cards[i], true, dealer_x, dealer_y);
if (bj->num_dealer_cards > MAX_CARDS-1)
dealer_x += 10;
else
dealer_x += CARD_WIDTH + 4;
}
for (n = 0; n < upper_bound; n++) {
for (i = 0; i < bj->num_player_cards[n]; i++) {
draw_card(bj->player_cards[n][i], true, player_x, player_y);
if (bj->split_status>1 || bj->num_player_cards[n]>MAX_CARDS)
player_x += 10;
else
player_x += CARD_WIDTH + 4;
}
if (bj->split_status > 1)
player_x = LCD_WIDTH/2 + 4;
}
}
/*****************************************************************************
* update_total updates the player's total
******************************************************************************/
static void update_total(struct game_context* bj) {
char total[3];
unsigned int w, h;
rb->snprintf(total, 3, "%d", bj->player_total);
rb->lcd_getstringsize(total, &w, &h);
#if LCD_HEIGHT > 64
h *= 2;
#endif
rb->lcd_putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 + h, total);
rb->lcd_update_rect(LCD_WIDTH - w, LCD_HEIGHT/2 + h, w, h);
}
/*****************************************************************************
* check_for_aces() is passed an array of cards and returns where an ace is
* located. Otherwise, returns -1.
******************************************************************************/
static signed int check_for_aces(struct card temp_cards[],
unsigned int size) {
unsigned int i;
for(i = 0; i < size; i++) {
if (temp_cards[i].is_soft_ace == true)
return i;
}
return -1;
}
/*****************************************************************************
* check_totals() compares player and dealer totals.
* 0: bust 1: loss, 2: push, 3: win, 4: blackjack, 5: something's not right...
******************************************************************************/
static unsigned int check_totals(struct game_context* bj)
{
unsigned int temp;
if (bj->player_total > 21)
temp = 0;
else if (bj->player_total == 21 && bj->is_blackjack)
if (bj->dealer_total == 21 && bj->num_dealer_cards == 2)
temp = 2;
else
temp = 4;
else if (bj->player_total == bj->dealer_total)
temp = 2;
else if (bj->dealer_total > 21 && bj->player_total < 22)
temp = 3;
else if (bj->dealer_total > bj->player_total)
temp = 1;
else if (bj->player_total > bj->dealer_total)
temp = 3;
else
temp = 5;
return temp;
}
/*****************************************************************************
* finish_dealer() draws cards for the dealer until he has 17 or more.
******************************************************************************/
static void finish_dealer(struct game_context* bj) {
signed int temp = 0;
if (bj->dealer_total > 16 && bj->dealer_total < 22)
return;
while (bj->dealer_total < 17) {
bj->dealer_cards[bj->num_dealer_cards] = new_card();
bj->dealer_total += bj->dealer_cards[bj->num_dealer_cards].value;
bj->num_dealer_cards++;
}
while (bj->dealer_total > 21) {
temp = check_for_aces(bj->dealer_cards, bj->num_dealer_cards);
if(temp != -1) {
bj->dealer_cards[temp].is_soft_ace = false;
bj->dealer_total -= 10;
}
else
return;
}
}
/*****************************************************************************
* finish_game() completes the game once player's turn is over.
******************************************************************************/
static void finish_game(struct game_context* bj) {
unsigned int rValue, w, h;
char str[19];
do {
finish_dealer(bj);
} while (bj->dealer_total < 17);
redraw_board(bj);
rValue = check_totals(bj);
if (rValue == 0) {
rb->snprintf(str, sizeof(str), " Bust! ");
bj->player_money -= bj->current_bet;
}
else if (rValue == 1) {
rb->snprintf(str, sizeof(str), " Sorry, you lost. ");
bj->player_money -= bj->current_bet;
}
else if (rValue == 2) {
rb->snprintf(str, sizeof(str), " Push ");
}
else if (rValue == 3) {
rb->snprintf(str, sizeof(str), " You won! ");
bj->player_money+= bj->current_bet;
}
else {
rb->snprintf(str, sizeof(str), " Blackjack! ");
bj->player_money += bj->current_bet * 3 / 2;
}
rb->lcd_getstringsize(str, &w, &h);
#if LCD_HEIGHT <= 64
rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
rb->lcd_fillrect(0, LCD_HEIGHT/2, LCD_WIDTH, LCD_HEIGHT/2);
rb->lcd_set_drawmode(DRMODE_SOLID);
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + h, str);
rb->snprintf(str, 12, "You have %d", bj->player_total);
rb->lcd_getstringsize(str, &w, &h);
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2, str);
#else
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, str);
#endif
rb->lcd_update();
}
/*****************************************************************************
* blackjack_recordscore() inserts a high score into the high scores list and
* returns the high score position.
******************************************************************************/
static unsigned int blackjack_recordscore(struct game_context* bj) {
unsigned int i;
unsigned int position = 0;
signed short current, temp;
/* calculate total score */
current = bj->player_money;
if(current <= 10) return 0;
/* insert the current score into the high scores */
for(i=0; i<NUM_SCORES; i++) {
if(current >= bj->highscores[i]) {
if(!position) {
position = i+1;
bj->dirty = true;
}
temp = bj->highscores[i];
bj->highscores[i] = current;
current = temp;
}
}
return position;
}
/*****************************************************************************
* blackjack_loadscores() loads the high scores saved file.
******************************************************************************/
static void blackjack_loadscores(struct game_context* bj) {
signed int fd;
bj->dirty = false;
/* clear high scores */
rb->memset(bj->highscores, 0, sizeof(bj->highscores));
/* open scores file */
fd = rb->open(SCORE_FILE, O_RDONLY);
if(fd < 0) return;
/* read in high scores */
if(rb->read(fd, bj->highscores, sizeof(bj->highscores)) <= 0) {
/* scores are bad, reset */
rb->memset(bj->highscores, 0, sizeof(bj->highscores));
}
rb->close(fd);
}
/*****************************************************************************
* blackjack_savescores() saves the high scores saved file.
******************************************************************************/
static void blackjack_savescores(struct game_context* bj) {
unsigned int fd;
/* write out the high scores to the save file */
fd = rb->open(SCORE_FILE, O_WRONLY|O_CREAT);
rb->write(fd, bj->highscores, sizeof(bj->highscores));
rb->close(fd);
bj->dirty = false;
}
/*****************************************************************************
* blackjack_loadgame() loads the saved game and returns load success.
******************************************************************************/
static bool blackjack_loadgame(struct game_context* bj) {
signed int fd;
bool loaded = false;
/* open game file */
fd = rb->open(SAVE_FILE, O_RDONLY);
if(fd < 0) return loaded;
/* read in saved game */
while(true) {
if(rb->read(fd, &bj->player_money, sizeof(bj->player_money)) <= 0) break;
if(rb->read(fd, &bj->player_total, sizeof(bj->player_total)) <= 0) break;
if(rb->read(fd, &bj->dealer_total, sizeof(bj->dealer_total)) <= 0) break;
if(rb->read(fd, &bj->num_player_cards, sizeof(bj->num_player_cards))<=0)
break;
if(rb->read(fd, &bj->num_dealer_cards, sizeof(bj->num_dealer_cards))<=0)
break;
if(rb->read(fd, &bj->current_bet, sizeof(bj->current_bet)) <= 0) break;
if(rb->read(fd, &bj->is_blackjack, sizeof(bj->is_blackjack)) <= 0) break;
if(rb->read(fd, &bj->split_status, sizeof(bj->split_status)) <= 0) break;
if(rb->read(fd, &bj->asked_insurance, sizeof(bj->asked_insurance)) <= 0)
break;
if(rb->read(fd, &bj->end_hand, sizeof(bj->end_hand)) <= 0) break;
if(rb->read(fd, &bj->player_cards, sizeof(bj->player_cards)) <= 0) break;
if(rb->read(fd, &bj->dealer_cards, sizeof(bj->dealer_cards)) <= 0) break;
bj->resume = true;
loaded = true;
break;
}
rb->close(fd);
/* delete saved file */
rb->remove(SAVE_FILE);
return loaded;
}
/*****************************************************************************
* blackjack_savegame() saves the current game state.
******************************************************************************/
static void blackjack_savegame(struct game_context* bj) {
unsigned int fd;
/* write out the game state to the save file */
fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT);
rb->write(fd, &bj->player_money, sizeof(bj->player_money));
rb->write(fd, &bj->player_total, sizeof(bj->player_total));
rb->write(fd, &bj->dealer_total, sizeof(bj->dealer_total));
rb->write(fd, &bj->num_player_cards, sizeof(bj->num_player_cards));
rb->write(fd, &bj->num_dealer_cards, sizeof(bj->num_dealer_cards));
rb->write(fd, &bj->current_bet, sizeof(bj->current_bet));
rb->write(fd, &bj->is_blackjack, sizeof(bj->is_blackjack));
rb->write(fd, &bj->split_status, sizeof(bj->split_status));
rb->write(fd, &bj->asked_insurance, sizeof(bj->asked_insurance));
rb->write(fd, &bj->end_hand, sizeof(bj->end_hand));
rb->write(fd, &bj->player_cards, sizeof(bj->player_cards));
rb->write(fd, &bj->dealer_cards, sizeof(bj->dealer_cards));
rb->close(fd);
bj->resume = true;
}
/*****************************************************************************
* blackjack_callback() is the default event handler callback which is called
* on usb connect and shutdown.
******************************************************************************/
static void blackjack_callback(void* param) {
struct game_context* bj = (struct game_context*) param;
if(bj->dirty) {
rb->splash(HZ, "Saving high scores...");
blackjack_savescores(bj);
}
}
/*****************************************************************************
* blackjack_get_yes_no() gets a yes/no answer from the user
******************************************************************************/
static unsigned int blackjack_get_yes_no(char message[20]) {
int button;
unsigned int w, h, b, choice = 0;
bool breakout = false;
char message_yes[24], message_no[24];
rb->strcpy(message_yes, message);
rb->strcpy(message_no, message);
rb->strcat(message_yes, " Yes");
rb->strcat(message_no, " No");
rb->lcd_getstringsize(message_yes, &w, &h);
const char *stg[] = {message_yes, message_no};
#if LCD_HEIGHT <= 64
b = 2*h+1;
#else
b = h-1;
#endif
#ifdef HAVE_LCD_COLOR
rb->lcd_fillrect(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + b, w+1, h+3);
rb->lcd_set_foreground(LCD_BLACK);
rb->lcd_set_background(LCD_WHITE);
#else
rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
rb->lcd_fillrect(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + b, w+1, h+3);
rb->lcd_set_drawmode(DRMODE_SOLID);
#endif
rb->lcd_drawrect(LCD_WIDTH/2 - w/2 - 1, LCD_HEIGHT/2 + b - 1, w+3, h+4);
while(!breakout) {
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + b +1, stg[choice]);
rb->lcd_update_rect(LCD_WIDTH/2 - w/2 - 1, LCD_HEIGHT/2 + b -1,
w+3, h+4);
button = rb->button_get(true);
switch(button) {
case BJACK_LEFT:
case (BJACK_LEFT|BUTTON_REPEAT):
case BJACK_RIGHT:
case (BJACK_RIGHT|BUTTON_REPEAT):
choice ^= 1;
break;
case BJACK_START: breakout = true;
break;
case BJACK_QUIT: breakout = true;
choice = BJ_QUIT;
break;
}
}
#if LCD_DEPTH > 1
rb->lcd_set_foreground(FG_COLOR);
rb->lcd_set_background(BG_COLOR);
#endif
return choice;
}
/*****************************************************************************
* blackjack_get_amount() gets an amount from the player to be used
******************************************************************************/
static signed int blackjack_get_amount(char message[20], signed int lower_limit,
signed int upper_limit,
signed int start) {
int button;
char str[6];
bool changed = false;
unsigned int w, h;
signed int amount;
rb->lcd_getstringsize("A", &w, &h); /* find the size of one character */
if (start > upper_limit)
amount = upper_limit;
else if (start < lower_limit)
amount = lower_limit;
else
amount = start;
#if LCD_DEPTH > 1
rb->lcd_set_background(LCD_WHITE);
rb->lcd_set_foreground(LCD_BLACK);
#endif
#if LCD_HEIGHT <= 64
rb->lcd_clear_display();
rb->lcd_puts(0, 1, message);
rb->snprintf(str, 9, "$%d", amount);
rb->lcd_puts(0, 2, str);
rb->lcd_puts(0, 3, "RIGHT: +1");
rb->lcd_puts(0, 4, "LEFT: -1");
rb->lcd_puts(0, 5, "UP: +10");
rb->lcd_puts(0, 6, "DOWN: -10");
rb->lcd_update();
#else
rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
rb->lcd_fillrect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3, 37*w / 2,
8*h -3);
rb->lcd_set_drawmode(DRMODE_SOLID);
rb->lcd_drawrect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3, 37*w / 2,
8*h -3);
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 4*h - 1, message);
rb->snprintf(str, 9, "$%d", amount);
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, str);
#if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
(CONFIG_KEYPAD == IPOD_3G_PAD) || \
(CONFIG_KEYPAD == IPOD_1G2G_PAD)
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - h-2, " >>|: +1");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 1, " |<<: -1");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + h, "SCROLL+: +10");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + 2*h + 1, "SCROLL-: -10");
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - h-2, "RIGHT: +1");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 1, "LEFT: -1");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + h, "SCROLL+: +10");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + 2*h + 1, "SCROLL-: -10");
#else
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - h-2, "RIGHT: +1");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 1, "LEFT: -1");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + h, "UP: +10");
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + 2*h + 1, "DOWN: -10");
#endif
rb->lcd_update_rect(LCD_WIDTH/2 - 9*w - 2, LCD_HEIGHT/2 - 9*h/2, 37*w/2 + 1,
8*h-2);
#endif
while(true) {
button = rb->button_get(true);
switch(button) {
case BJACK_UP:
case (BJACK_UP|BUTTON_REPEAT):
if (amount + 10 < upper_limit + 1) {
amount += 10;
changed = true;
}
break;
case BJACK_DOWN:
case (BJACK_DOWN|BUTTON_REPEAT):
if (amount - 10 > lower_limit - 1) {
amount -= 10;
changed = true;
}
break;
case BJACK_RIGHT:
case (BJACK_RIGHT|BUTTON_REPEAT):
if (amount + 1 < upper_limit + 1) {
amount++;
changed = true;
}
break;
case BJACK_LEFT:
case (BJACK_LEFT|BUTTON_REPEAT):
if (amount - 1 > lower_limit - 1) {
amount--;
changed = true;
}
break;
case BJACK_MAX :
amount = upper_limit;
changed = true;
break;
case BJACK_MIN :
amount = lower_limit;
changed = true;
break;
case BJACK_QUIT:
return 0;
case BJACK_START:
#if LCD_DEPTH > 1
rb->lcd_set_foreground(FG_COLOR);
rb->lcd_set_background(BG_COLOR);
#endif
rb->lcd_clear_display();
return amount;
}
if(changed) {
rb->snprintf(str, 9, "$%d", amount);
#if LCD_HEIGHT <= 64
rb->lcd_puts(0, 2, str);
rb->lcd_update();
#else
rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
rb->lcd_fillrect(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, 5*w, h);
rb->lcd_set_drawmode(DRMODE_SOLID);
rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, str);
rb->lcd_update_rect(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, 5*w, h);
#endif
changed = false;
}
}
}
/*****************************************************************************
* blackjack_get_bet() gets the player's bet.
******************************************************************************/
static void blackjack_get_bet(struct game_context* bj) {
bj->current_bet = blackjack_get_amount("Please enter a bet", 10,
bj->player_money, bj->current_bet);
}
/*****************************************************************************
* double_down() returns one final card then finishes the game
******************************************************************************/
static void double_down(struct game_context* bj) {
bj->current_bet *= 2;
bj->player_cards[0][bj->num_player_cards[0]] = new_card();
bj->player_total += bj->player_cards[0][bj->num_player_cards[0]].value;
bj->num_player_cards[0]++;
}
/*****************************************************************************
* split() checks if the player wants to split and acts accordingly.
* When bj->split_status is 1, no split occurred. 2 means the player split and 3
* means a split has already occurred and the first hand is done.
******************************************************************************/
static void split(struct game_context* bj) {
if (blackjack_get_yes_no("Split?") == 1)
bj->split_status = 1;
else {
bj->split_status = 2;
bj->current_bet *= 2;
bj->num_player_cards[0] = 1;
bj->num_player_cards[1] = 1;
bj->player_cards[1][0] = bj->player_cards[0][1];
bj->player_total = bj->player_cards[0][0].value;
}
}
/*****************************************************************************
* insurance() see if the player wants to buy insurance and how much.
******************************************************************************/
static unsigned int insurance(struct game_context* bj) {
unsigned int insurance, max_amount;
insurance = blackjack_get_yes_no("Buy Insurance?");
bj->asked_insurance = true;
max_amount = bj->current_bet < (unsigned int)bj->player_money ?
bj->current_bet/2 : (unsigned int)bj->player_money;
if (insurance == 1) return 0;
insurance = blackjack_get_amount("How much?", 0, max_amount, 0);
redraw_board(bj);
return insurance;
}
/*****************************************************************************
* play_again() checks to see if the player wants to keep playing.
******************************************************************************/
static unsigned int play_again(void) {
return blackjack_get_yes_no("Play Again?");
}
/*****************************************************************************
* blackjack_menu() is the initial menu at the start of the game.
******************************************************************************/
static unsigned int blackjack_menu(struct game_context* bj) {
int button;
char *title = "Blackjack";
char str[18];
unsigned int i, w, h;
bool breakout = false;
bool showscores = false;
while(true){
#if LCD_DEPTH > 1
rb->lcd_set_background(BG_COLOR);
rb->lcd_set_foreground(FG_COLOR);
#endif
rb->lcd_clear_display();
if(!showscores) {
/* welcome screen to display key bindings */
rb->lcd_getstringsize(title, &w, &h);
rb->lcd_putsxy((LCD_WIDTH-w)/2, 0, title);
#if CONFIG_KEYPAD == RECORDER_PAD
rb->lcd_puts(0, 1, "ON: start");
rb->lcd_puts(0, 2, "OFF: exit");
rb->lcd_puts(0, 3, "F1: hit");
rb->lcd_puts(0, 4, "F2: stay");
rb->lcd_puts(0, 5, "F3: double down");
rb->lcd_puts(0, 6, "PLAY: save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 7, str);
#elif CONFIG_KEYPAD == ONDIO_PAD
rb->lcd_puts(0, 1, "MENU: start");
rb->lcd_puts(0, 2, "OFF: exit");
rb->lcd_puts(0, 3, "LEFT: hit");
rb->lcd_puts(0, 4, "RIGHT: stay");
rb->lcd_puts(0, 5, "UP: double down");
rb->lcd_puts(0, 6, "DOWN: save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 7, str);
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
rb->lcd_puts(0, 2, "PLAY to start & to hit");
rb->lcd_puts(0, 3, "STOP to exit");
rb->lcd_puts(0, 4, "REC to stay");
rb->lcd_puts(0, 5, "NAVI to double down ");
rb->lcd_puts(0, 6, " & to view highscores");
rb->lcd_puts(0, 7, "AB to save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 8, str);
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
rb->lcd_puts(0, 2, "PLAY to start & hit");
rb->lcd_puts(0, 3, "POWER to exit");
rb->lcd_puts(0, 4, ">>| to stay");
rb->lcd_puts(0, 5, "|<< to double down");
rb->lcd_puts(0, 6, "LEFT to view scores");
rb->lcd_puts(0, 7, "RIGHT to save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 8, str);
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
(CONFIG_KEYPAD == IPOD_3G_PAD) || \
(CONFIG_KEYPAD == IPOD_1G2G_PAD)
#if LCD_WIDTH >=176
rb->lcd_puts(0, 2, "SELECT to start & to hit");
rb->lcd_puts(0, 3, "MENU to exit");
rb->lcd_puts(0, 4, ">>| to stay & to view highscores");
rb->lcd_puts(0, 5, "|<< to double down");
rb->lcd_puts(0, 6, "PLAY to save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 7, str);
#else
rb->lcd_puts(0, 2, "SELECT to start & to ");
rb->lcd_puts(0, 3, " hit");
rb->lcd_puts(0, 4, "MENU to exit");
rb->lcd_puts(0, 5, ">>| to stay & to view ");
rb->lcd_puts(0, 6, " highscores");
rb->lcd_puts(0, 7, "|<< to double down");
rb->lcd_puts(0, 8, "PLAY to save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 9, str);
#endif
#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
rb->lcd_puts(0, 2, "PLAY to start to hit");
rb->lcd_puts(0, 3, "POWER to exit");
rb->lcd_puts(0, 4, "SELECT to hit");
rb->lcd_puts(0, 5, "REC to stay");
rb->lcd_puts(0, 6, "PLAY to double down");
rb->lcd_puts(0, 7, "RIGHT to view highscores ");
rb->lcd_puts(0, 8, "DOWN to save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 9, str);
#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
rb->lcd_puts(0, 2, "AB to start & to");
rb->lcd_puts(0, 3, " stay");
rb->lcd_puts(0, 4, "EQ to hit");
rb->lcd_puts(0, 5, "PLAY to exit");
rb->lcd_puts(0, 6, "CLICK to double down");
rb->lcd_puts(0, 7, "& to view highscores");
rb->lcd_puts(0, 8, "AB+EQ to save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 9, str);
#elif CONFIG_KEYPAD == GIGABEAT_PAD
rb->lcd_puts(0, 2, "POWER to start");
rb->lcd_puts(0, 3, "A to exit");
rb->lcd_puts(0, 4, "VOL+ to hit");
rb->lcd_puts(0, 5, "VOL- to stay");
rb->lcd_puts(0, 6, "CENTER to double down");
rb->lcd_puts(0, 6, "RIGHT to view highscores ");
rb->lcd_puts(0, 8, "MENU to save/resume");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 9, str);
#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
rb->lcd_puts(0, 2, "SELECT to start & to hit");
rb->lcd_puts(0, 3, "POWER to exit");
rb->lcd_puts(0, 4, "RIGHT to stay");
rb->lcd_puts(0, 5, "LEFT to double down");
rb->lcd_puts(0, 6, "REC to save/resume");
rb->lcd_puts(0, 7, "UP to view scores");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 8, str);
#elif (CONFIG_KEYPAD == SANSA_C200_PAD)
rb->lcd_puts(0, 2, "SELECT to start & to hit");
rb->lcd_puts(0, 3, "POWER to exit");
rb->lcd_puts(0, 4, "RIGHT to stay");
rb->lcd_puts(0, 5, "LEFT to double down");
rb->lcd_puts(0, 6, "DOWN to save/resume");
rb->lcd_puts(0, 7, "REC to view scores");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 9, str);
#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
rb->lcd_puts(0, 2, "PLAY to start & to");
rb->lcd_puts(0, 3, " hit");
rb->lcd_puts(0, 4, "REC to exit");
rb->lcd_puts(0, 5, "FF to stay");
rb->lcd_puts(0, 6, "REW to double down");
rb->lcd_puts(0, 7, "MODE to save/resume");
rb->lcd_puts(0, 8, "MENU to view scores");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 10, str);
#elif CONFIG_KEYPAD == COWOND2_PAD
rb->lcd_puts(0, 6, "POWER to exit");
rb->lcd_puts(0, 7, "MINUS to double down");
rb->lcd_puts(0, 8, "MENU to view scores");
rb->snprintf(str, 21, "High Score: $%d", bj->highscores[0]);
rb->lcd_puts(0, 10, str);
#endif
#ifdef HAVE_TOUCHPAD
rb->lcd_puts(0, 2, "LCD CENTRE to start & to hit");
rb->lcd_puts(0, 3, "LCD BOTTOMLEFT to stay");
rb->lcd_puts(0, 4, "LCD BOTTOMRIGHT to save/resume");
#endif
} else {
rb->snprintf(str, 12, "%s", "High Scores");
rb->lcd_getstringsize(str, &w, &h);
rb->lcd_putsxy((LCD_WIDTH-w)/2, 0, str);
/* print high scores */
for(i=0; i<NUM_SCORES; i++) {
rb->snprintf(str, 14, "#%02d: $%d", i+1, bj->highscores[i]);
rb->lcd_puts(0, i+1, str);
}
}
rb->lcd_update();
/* handle menu button presses */
button = rb->button_get(true);
switch(button) {
case BJACK_START: /* start playing */
breakout = true;
break;
case BJACK_QUIT: /* quit program */
if(showscores) {
showscores = 0;
break;
}
return BJ_QUIT;
case BJACK_RESUME:/* resume game */
if(!blackjack_loadgame(bj)) {
rb->splash(HZ*2, "Nothing to resume");
} else {
rb->splash(HZ*2, "Loading...");
breakout = true;
}
break;
case BJACK_SCORES:/* toggle high scores */
showscores = !showscores;
break;
default:
if(rb->default_event_handler_ex(button, blackjack_callback,
(void*) bj) == SYS_USB_CONNECTED)
return BJ_USB;
break;
}
if(breakout) break;
}
return(0);
}
/*****************************************************************************
* blackjack() is the main game subroutine, it returns the final game status.
******************************************************************************/
static int blackjack(struct game_context* bj) {
int button;
unsigned int w, h, temp_var, done = 0, todo = 1;
signed int temp;
bool breakout = false;
bool dbl_down = false;
/* don't resume by default */
bj->resume = false;
/********************
* menu *
********************/
temp_var = blackjack_menu(bj);
if (temp_var == BJ_QUIT || temp_var == BJ_USB)
return temp_var;
/********************
* init *
********************/
blackjack_init(bj);
bj->current_bet=10;
/********************
* play *
********************/
/* check for resumed game */
if(bj->resume) {
bj->resume = false;
redraw_board(bj);
if (bj->split_status == 2) {
todo=2;
player_x = bj->num_player_cards[0] * 10 + 4;
}
else if (bj->split_status == 3) {
player_x = bj->num_player_cards[1] * 10 + LCD_WIDTH/2 + 4;
todo=2;
done=1;
}
}
else {
bj->player_money = 1000;
blackjack_get_bet(bj);
if (bj->current_bet == 0)
return BJ_QUIT;
rb->lcd_clear_display();
deal_init_cards(bj);
blackjack_drawtable(bj);
}
rb->lcd_update();
breakout = false;
while(true){
if(bj->player_total == 21 && bj->num_player_cards[0] == 2) {
bj->is_blackjack = true;
bj->end_hand = true;
finish_game(bj);
}
else if(bj->dealer_cards[1].is_soft_ace && !breakout &&
!bj->asked_insurance) {
temp_var = insurance(bj);
if (bj->dealer_total == 21) {
rb->splash(HZ, "Dealer has blackjack");
bj->player_money += temp_var;
bj->end_hand = true;
breakout = true;
redraw_board(bj);
finish_game(bj);
}
else {
rb->splash(HZ, "Dealer does not have blackjack");
bj->player_money -= temp_var;
breakout = true;
redraw_board(bj);
rb->lcd_update();
}
}
if(bj->split_status == 0 &&
bj->player_cards[0][0].num == bj->player_cards[0][1].num) {
split(bj);
redraw_board(bj);
rb->lcd_update_rect(0, LCD_HEIGHT/2, LCD_WIDTH, LCD_HEIGHT/2);
if (bj->split_status == 2) {
todo++;
player_x = bj->num_player_cards[0] * 10 + 4;
}
}
while(done < todo) {
button = rb->button_get(true);
switch(button) {
case BJACK_HIT:
NEXT_CARD = new_card();
bj->player_total += NEXT_CARD.value;
draw_card(NEXT_CARD, true, player_x, player_y);
bj->num_player_cards[done]++;
if (bj->num_player_cards[done] == MAX_CARDS + 1) {
redraw_board(bj);
rb->lcd_update_rect(0, LCD_HEIGHT/2, LCD_WIDTH,
LCD_HEIGHT/2);
}
else if (bj->num_player_cards[done]>MAX_CARDS || todo > 1) {
rb->lcd_update_rect(player_x, player_y, CARD_WIDTH+2,
CARD_HEIGHT+2);
player_x += 10;
}
else {
rb->lcd_update_rect(player_x, player_y, CARD_WIDTH+2,
CARD_HEIGHT+2);
player_x += CARD_WIDTH + 4;
}
update_total(bj);
break;
case BJACK_STAY:
bj->end_hand = true;
break;
case BJACK_DOUBLEDOWN:
if ((signed int)bj->current_bet * 2 < bj->player_money + 1 &&
bj->num_player_cards[0]==2 && todo==1) {
double_down(bj);
dbl_down = true;
if (bj->player_total < 22) {
bj->end_hand = true;
finish_game(bj);
}
}
else if((signed int)bj->current_bet * 2 > bj->player_money) {
rb->splash(HZ, "Not enough money to double down.");
redraw_board(bj);
rb->lcd_update();
}
break;
case BJACK_RESUME: /* save and end game */
rb->splash(HZ, "Saving game...");
blackjack_savegame(bj);
/* fall through to BJACK_QUIT */
case BJACK_QUIT:
return BJ_END;
}
while (bj->player_total > 21 && !bj->end_hand) {
temp = check_for_aces(bj->player_cards[done],
bj->num_player_cards[done]);
if(temp != -1) {
bj->player_cards[done][temp].is_soft_ace = false;
bj->player_total -= 10;
update_total(bj);
if (dbl_down) {
bj->end_hand = true;
finish_game(bj);
}
}
else
bj->end_hand = true;
}
if (bj->end_hand) {
done++;
if(todo > 1) {
if (done == 2) {
temp = bj->player_total;
bj->player_total = temp_var;
temp_var = temp;
finish_game(bj);
rb->lcd_getstringsize(" Split 1 ", &w, &h);
rb->lcd_putsxy(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
" Split 1 ");
rb->lcd_update_rect(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
w,h);
bj->current_bet /= 2;
rb->lcd_update_rect(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
w,h);
rb->sleep(HZ*2);
bj->player_total = temp_var;
finish_game(bj);
rb->lcd_getstringsize(" Split 2 ", &w, &h);
rb->lcd_putsxy(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
" Split 2 ");
rb->lcd_update_rect(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
w,h);
rb->sleep(HZ*2);
}
else {
bj->end_hand = false;
bj->split_status = 3;
temp_var = bj->player_total;
bj->player_total = bj->player_cards[1][0].value;
update_total(bj);
redraw_board(bj);
player_x += 10;
rb->lcd_update();
}
}
else
finish_game(bj);
}
}
if (bj->player_money < 10) {
rb->sleep(HZ);
return BJ_LOSE;
}
if (bj->end_hand) { /* If hand is over */
if (play_again() != 0) /* User wants to quit */
return BJ_END;
else { /* User keeps playing */
breakout = false;
redraw_board(bj);
if(dbl_down) {
bj->current_bet /= 2;
dbl_down = false;
}
done = 0;
todo = 1;
blackjack_init(bj);
blackjack_get_bet(bj);
if (bj->current_bet == 0)
return BJ_END;
deal_init_cards(bj);
blackjack_drawtable(bj);
rb->lcd_update();
}
}
}
/* Never reached */
return PLUGIN_OK;
}
/*****************************************************************************
* plugin entry point.
******************************************************************************/
enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
{
struct game_context bj;
bool exit = false;
unsigned int position;
char str[19];
(void)parameter;
rb = api;
#if LCD_DEPTH > 1
rb->lcd_set_backdrop(NULL);
#endif
/* load high scores */
blackjack_loadscores(&bj);
rb->lcd_setfont(FONT_SYSFIXED);
while(!exit) {
switch(blackjack(&bj)){
case BJ_LOSE:
rb->splash(HZ, "Not enough money to continue");
/* fall through to BJ_END */
case BJ_END:
if(!bj.resume) {
if((position = blackjack_recordscore(&bj))) {
rb->snprintf(str, 19, "New high score #%d!", position);
rb->splash(HZ*2, str);
}
}
break;
case BJ_USB:
rb->lcd_setfont(FONT_UI);
return PLUGIN_USB_CONNECTED;
case BJ_QUIT:
if(bj.dirty) {
rb->splash(HZ, "Saving high scores...");
blackjack_savescores(&bj);
}
exit = true;
break;
default:
break;
}
}
rb->lcd_setfont(FONT_UI);
return PLUGIN_OK;
}
|