diff options
Diffstat (limited to 'apps/plugins/blackjack.c')
-rw-r--r-- | apps/plugins/blackjack.c | 126 |
1 files changed, 53 insertions, 73 deletions
diff --git a/apps/plugins/blackjack.c b/apps/plugins/blackjack.c index 1f3f426ed8..f46d378f2b 100644 --- a/apps/plugins/blackjack.c +++ b/apps/plugins/blackjack.c | |||
@@ -32,13 +32,15 @@ PLUGIN_HEADER | |||
32 | #define HIGH_SCORE PLUGIN_GAMES_DIR "/blackjack.score" | 32 | #define HIGH_SCORE PLUGIN_GAMES_DIR "/blackjack.score" |
33 | #define SAVE_FILE PLUGIN_GAMES_DIR "/blackjack.save" | 33 | #define SAVE_FILE PLUGIN_GAMES_DIR "/blackjack.save" |
34 | #define NUM_SCORES 5 | 34 | #define NUM_SCORES 5 |
35 | struct highscore highest[NUM_SCORES]; | ||
36 | 35 | ||
37 | /* final game return status */ | 36 | /* final game return status */ |
38 | #define BJ_END 3 | 37 | enum { |
39 | #define BJ_USB 2 | 38 | BJ_LOSE, |
40 | #define BJ_QUIT 1 | 39 | BJ_QUIT, |
41 | #define BJ_LOSE 0 | 40 | BJ_SAVE_AND_QUIT, |
41 | BJ_USB, | ||
42 | BJ_END, | ||
43 | }; | ||
42 | 44 | ||
43 | #if CONFIG_KEYPAD == RECORDER_PAD | 45 | #if CONFIG_KEYPAD == RECORDER_PAD |
44 | #define BJACK_SELECT_NAME "PLAY" | 46 | #define BJACK_SELECT_NAME "PLAY" |
@@ -506,9 +508,11 @@ typedef struct game_context { | |||
506 | bool is_blackjack; | 508 | bool is_blackjack; |
507 | bool end_hand; | 509 | bool end_hand; |
508 | bool asked_insurance; | 510 | bool asked_insurance; |
509 | bool resume; | ||
510 | } game_context; | 511 | } game_context; |
511 | 512 | ||
513 | static bool resume; | ||
514 | static struct highscore highest[NUM_SCORES]; | ||
515 | |||
512 | /***************************************************************************** | 516 | /***************************************************************************** |
513 | * blackjack_init() initializes blackjack data structures. | 517 | * blackjack_init() initializes blackjack data structures. |
514 | ******************************************************************************/ | 518 | ******************************************************************************/ |
@@ -523,7 +527,7 @@ static void blackjack_init(struct game_context* bj) { | |||
523 | player_y = LCD_HEIGHT - LCD_HEIGHT/4 - CARD_HEIGHT/2; | 527 | player_y = LCD_HEIGHT - LCD_HEIGHT/4 - CARD_HEIGHT/2; |
524 | 528 | ||
525 | /* check for resumed game */ | 529 | /* check for resumed game */ |
526 | if(bj->resume) return; | 530 | if(resume) return; |
527 | 531 | ||
528 | /* reset scoring */ | 532 | /* reset scoring */ |
529 | bj->player_total = 0; | 533 | bj->player_total = 0; |
@@ -845,39 +849,17 @@ static bool blackjack_loadgame(struct game_context* bj) { | |||
845 | signed int fd; | 849 | signed int fd; |
846 | bool loaded = false; | 850 | bool loaded = false; |
847 | 851 | ||
852 | resume = false; | ||
848 | /* open game file */ | 853 | /* open game file */ |
849 | fd = rb->open(SAVE_FILE, O_RDONLY); | 854 | fd = rb->open(SAVE_FILE, O_RDONLY); |
850 | if(fd < 0) return loaded; | 855 | if(fd < 0) return loaded; |
851 | 856 | ||
852 | /* read in saved game */ | 857 | /* read in saved game */ |
853 | while(true) { | 858 | if(rb->read(fd, bj, sizeof(struct game_context)) |
854 | if(rb->read(fd, &bj->player_money, sizeof(bj->player_money)) <= 0) | 859 | == (long)sizeof(struct game_context)) |
855 | break; | 860 | { |
856 | if(rb->read(fd, &bj->player_total, sizeof(bj->player_total)) <= 0) | 861 | resume = true; |
857 | break; | ||
858 | if(rb->read(fd, &bj->dealer_total, sizeof(bj->dealer_total)) <= 0) | ||
859 | break; | ||
860 | if(rb->read(fd, &bj->num_player_cards, sizeof(bj->num_player_cards))<=0) | ||
861 | break; | ||
862 | if(rb->read(fd, &bj->num_dealer_cards, sizeof(bj->num_dealer_cards))<=0) | ||
863 | break; | ||
864 | if(rb->read(fd, &bj->current_bet, sizeof(bj->current_bet)) <= 0) | ||
865 | break; | ||
866 | if(rb->read(fd, &bj->is_blackjack, sizeof(bj->is_blackjack)) <= 0) | ||
867 | break; | ||
868 | if(rb->read(fd, &bj->split_status, sizeof(bj->split_status)) <= 0) | ||
869 | break; | ||
870 | if(rb->read(fd, &bj->asked_insurance, sizeof(bj->asked_insurance)) <= 0) | ||
871 | break; | ||
872 | if(rb->read(fd, &bj->end_hand, sizeof(bj->end_hand)) <= 0) | ||
873 | break; | ||
874 | if(rb->read(fd, &bj->player_cards, sizeof(bj->player_cards)) <= 0) | ||
875 | break; | ||
876 | if(rb->read(fd, &bj->dealer_cards, sizeof(bj->dealer_cards)) <= 0) | ||
877 | break; | ||
878 | bj->resume = true; | ||
879 | loaded = true; | 862 | loaded = true; |
880 | break; | ||
881 | } | 863 | } |
882 | 864 | ||
883 | rb->close(fd); | 865 | rb->close(fd); |
@@ -891,25 +873,16 @@ static bool blackjack_loadgame(struct game_context* bj) { | |||
891 | * blackjack_savegame() saves the current game state. | 873 | * blackjack_savegame() saves the current game state. |
892 | ******************************************************************************/ | 874 | ******************************************************************************/ |
893 | static void blackjack_savegame(struct game_context* bj) { | 875 | static void blackjack_savegame(struct game_context* bj) { |
894 | unsigned int fd; | 876 | int fd; |
895 | 877 | ||
878 | if(!resume) | ||
879 | return; | ||
896 | /* write out the game state to the save file */ | 880 | /* write out the game state to the save file */ |
897 | fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT); | 881 | fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT); |
898 | rb->write(fd, &bj->player_money, sizeof(bj->player_money)); | 882 | if(fd < 0) |
899 | rb->write(fd, &bj->player_total, sizeof(bj->player_total)); | 883 | return; |
900 | rb->write(fd, &bj->dealer_total, sizeof(bj->dealer_total)); | 884 | rb->write(fd, bj, sizeof(struct game_context)); |
901 | rb->write(fd, &bj->num_player_cards, sizeof(bj->num_player_cards)); | ||
902 | rb->write(fd, &bj->num_dealer_cards, sizeof(bj->num_dealer_cards)); | ||
903 | rb->write(fd, &bj->current_bet, sizeof(bj->current_bet)); | ||
904 | rb->write(fd, &bj->is_blackjack, sizeof(bj->is_blackjack)); | ||
905 | rb->write(fd, &bj->split_status, sizeof(bj->split_status)); | ||
906 | rb->write(fd, &bj->asked_insurance, sizeof(bj->asked_insurance)); | ||
907 | rb->write(fd, &bj->end_hand, sizeof(bj->end_hand)); | ||
908 | rb->write(fd, &bj->player_cards, sizeof(bj->player_cards)); | ||
909 | rb->write(fd, &bj->dealer_cards, sizeof(bj->dealer_cards)); | ||
910 | rb->close(fd); | 885 | rb->close(fd); |
911 | |||
912 | bj->resume = true; | ||
913 | } | 886 | } |
914 | 887 | ||
915 | /***************************************************************************** | 888 | /***************************************************************************** |
@@ -1181,9 +1154,8 @@ static bool blackjack_help(void) { | |||
1181 | BJACK_SELECT_NAME, ":", "hit", "/", "select", "", | 1154 | BJACK_SELECT_NAME, ":", "hit", "/", "select", "", |
1182 | BJACK_STAY_NAME, ":", "stay", "", | 1155 | BJACK_STAY_NAME, ":", "stay", "", |
1183 | BJACK_DOUBLE_NAME, ":", "double", "down", "", | 1156 | BJACK_DOUBLE_NAME, ":", "double", "down", "", |
1184 | BJACK_QUIT_NAME, ":", "Go", "to", "menu", "without", "save", "", | 1157 | BJACK_QUIT_NAME, ":", "End", "game", "and", "go", "to", "menu", "", |
1185 | BJACK_RESUME_NAME, ":", "Save", "and", "go", "to", "menu", "", | 1158 | BJACK_RESUME_NAME, ":", "Go", "to", "menu", "without", "end", "game", |
1186 | |||
1187 | }; | 1159 | }; |
1188 | static struct style_text formation[]={ | 1160 | static struct style_text formation[]={ |
1189 | { 0, TEXT_CENTER|TEXT_UNDERLINE }, | 1161 | { 0, TEXT_CENTER|TEXT_UNDERLINE }, |
@@ -1191,7 +1163,7 @@ static bool blackjack_help(void) { | |||
1191 | { 18, C_RED }, /* Stay */ | 1163 | { 18, C_RED }, /* Stay */ |
1192 | { 22, C_RED }, /* Double Down */ | 1164 | { 22, C_RED }, /* Double Down */ |
1193 | { 27, C_RED }, /* Quit */ | 1165 | { 27, C_RED }, /* Quit */ |
1194 | { 35, C_RED }, /* Save */ | 1166 | { 36, C_RED }, /* Menu */ |
1195 | { -1, 0 } | 1167 | { -1, 0 } |
1196 | }; | 1168 | }; |
1197 | int button; | 1169 | int button; |
@@ -1219,26 +1191,28 @@ static bool blackjack_help(void) { | |||
1219 | * blackjack_menu() is the initial menu at the start of the game. | 1191 | * blackjack_menu() is the initial menu at the start of the game. |
1220 | ******************************************************************************/ | 1192 | ******************************************************************************/ |
1221 | static unsigned int blackjack_menu(struct game_context* bj) { | 1193 | static unsigned int blackjack_menu(struct game_context* bj) { |
1222 | int selection=0; | 1194 | int selection = resume?0:1; |
1223 | bool breakout = false; | 1195 | bool breakout = false; |
1196 | (void)bj; | ||
1224 | 1197 | ||
1225 | MENUITEM_STRINGLIST(menu, "BlackJack Menu", NULL, | 1198 | MENUITEM_STRINGLIST(menu, "BlackJack Menu", NULL, |
1226 | "Start Game","Resume Game", | 1199 | "Resume Game", "Start New Game", |
1227 | "High Scores", "Help", | 1200 | "High Scores", "Help", |
1228 | "Playback Control", "Quit"); | 1201 | "Playback Control", "Quit", "Save and Quit"); |
1229 | 1202 | ||
1230 | while(!breakout) { | 1203 | while(!breakout) { |
1231 | switch(rb->do_menu(&menu, &selection, NULL, false)) { | 1204 | switch(rb->do_menu(&menu, &selection, NULL, false)) { |
1232 | case 0: | 1205 | case 0: |
1233 | breakout = true; | 1206 | if(!resume) { |
1234 | break; | ||
1235 | case 1: | ||
1236 | if(!blackjack_loadgame(bj)) { | ||
1237 | rb->splash(HZ*2, "Nothing to resume"); | 1207 | rb->splash(HZ*2, "Nothing to resume"); |
1238 | } else { | 1208 | } else { |
1239 | breakout = true; | 1209 | breakout = true; |
1240 | } | 1210 | } |
1241 | break; | 1211 | break; |
1212 | case 1: | ||
1213 | breakout = true; | ||
1214 | resume = false; | ||
1215 | break; | ||
1242 | case 2: | 1216 | case 2: |
1243 | highscore_show(NUM_SCORES, highest, NUM_SCORES, false); | 1217 | highscore_show(NUM_SCORES, highest, NUM_SCORES, false); |
1244 | break; | 1218 | break; |
@@ -1252,6 +1226,8 @@ static unsigned int blackjack_menu(struct game_context* bj) { | |||
1252 | break; | 1226 | break; |
1253 | case 5: | 1227 | case 5: |
1254 | return BJ_QUIT; | 1228 | return BJ_QUIT; |
1229 | case 6: | ||
1230 | return BJ_SAVE_AND_QUIT; | ||
1255 | 1231 | ||
1256 | case MENU_ATTACHED_USB: | 1232 | case MENU_ATTACHED_USB: |
1257 | return BJ_USB; | 1233 | return BJ_USB; |
@@ -1279,14 +1255,11 @@ static int blackjack(struct game_context* bj) { | |||
1279 | rb->lcd_set_foreground(FG_COLOR); | 1255 | rb->lcd_set_foreground(FG_COLOR); |
1280 | #endif | 1256 | #endif |
1281 | 1257 | ||
1282 | /* don't resume by default */ | ||
1283 | bj->resume = false; | ||
1284 | |||
1285 | /******************** | 1258 | /******************** |
1286 | * menu * | 1259 | * menu * |
1287 | ********************/ | 1260 | ********************/ |
1288 | temp_var = blackjack_menu(bj); | 1261 | temp_var = blackjack_menu(bj); |
1289 | if (temp_var == BJ_QUIT || temp_var == BJ_USB) | 1262 | if (temp_var != 0) |
1290 | return temp_var; | 1263 | return temp_var; |
1291 | 1264 | ||
1292 | 1265 | ||
@@ -1300,8 +1273,8 @@ static int blackjack(struct game_context* bj) { | |||
1300 | ********************/ | 1273 | ********************/ |
1301 | 1274 | ||
1302 | /* check for resumed game */ | 1275 | /* check for resumed game */ |
1303 | if(bj->resume) { | 1276 | if(resume) { |
1304 | bj->resume = false; | 1277 | resume = false; |
1305 | redraw_board(bj); | 1278 | redraw_board(bj); |
1306 | if (bj->split_status == 2) { | 1279 | if (bj->split_status == 2) { |
1307 | todo=2; | 1280 | todo=2; |
@@ -1413,9 +1386,8 @@ static int blackjack(struct game_context* bj) { | |||
1413 | } | 1386 | } |
1414 | break; | 1387 | break; |
1415 | case BJACK_RESUME: /* save and end game */ | 1388 | case BJACK_RESUME: /* save and end game */ |
1416 | rb->splash(HZ, "Saving game..."); | 1389 | resume = true; |
1417 | blackjack_savegame(bj); | 1390 | return BJ_END; |
1418 | /* fall through to BJACK_QUIT */ | ||
1419 | 1391 | ||
1420 | case BJACK_QUIT: | 1392 | case BJACK_QUIT: |
1421 | return BJ_END; | 1393 | return BJ_END; |
@@ -1530,6 +1502,7 @@ enum plugin_status plugin_start(const void* parameter) | |||
1530 | 1502 | ||
1531 | /* load high scores */ | 1503 | /* load high scores */ |
1532 | highscore_load(HIGH_SCORE,highest,NUM_SCORES); | 1504 | highscore_load(HIGH_SCORE,highest,NUM_SCORES); |
1505 | blackjack_loadgame(&bj); | ||
1533 | 1506 | ||
1534 | rb->lcd_setfont(FONT_SYSFIXED); | 1507 | rb->lcd_setfont(FONT_SYSFIXED); |
1535 | 1508 | ||
@@ -1540,14 +1513,14 @@ enum plugin_status plugin_start(const void* parameter) | |||
1540 | /* fall through to BJ_END */ | 1513 | /* fall through to BJ_END */ |
1541 | 1514 | ||
1542 | case BJ_END: | 1515 | case BJ_END: |
1543 | if(!bj.resume && bj.player_money > 10) { | 1516 | if(!resume && bj.player_money > 10) { |
1544 | /* There is no level, so store -1 to blank column */ | 1517 | /* There is no level, so store -1 to blank column */ |
1545 | int position = highscore_update(bj.player_money, -1, "", | 1518 | int position = highscore_update(bj.player_money, -1, "", |
1546 | highest, NUM_SCORES); | 1519 | highest, NUM_SCORES); |
1547 | if (position == 0) { | ||
1548 | rb->splash(HZ*2, "New High Score"); | ||
1549 | } | ||
1550 | if (position != -1) { | 1520 | if (position != -1) { |
1521 | if (position == 0) { | ||
1522 | rb->splash(HZ*2, "New High Score"); | ||
1523 | } | ||
1551 | highscore_show(position, highest, NUM_SCORES, false); | 1524 | highscore_show(position, highest, NUM_SCORES, false); |
1552 | } | 1525 | } |
1553 | } | 1526 | } |
@@ -1558,6 +1531,13 @@ enum plugin_status plugin_start(const void* parameter) | |||
1558 | highscore_save(HIGH_SCORE,highest,NUM_SCORES); | 1531 | highscore_save(HIGH_SCORE,highest,NUM_SCORES); |
1559 | return PLUGIN_USB_CONNECTED; | 1532 | return PLUGIN_USB_CONNECTED; |
1560 | 1533 | ||
1534 | case BJ_SAVE_AND_QUIT: | ||
1535 | if (resume) { | ||
1536 | rb->splash(HZ, "Saving game..."); | ||
1537 | blackjack_savegame(&bj); | ||
1538 | } | ||
1539 | /* fall through */ | ||
1540 | |||
1561 | case BJ_QUIT: | 1541 | case BJ_QUIT: |
1562 | highscore_save(HIGH_SCORE,highest,NUM_SCORES); | 1542 | highscore_save(HIGH_SCORE,highest,NUM_SCORES); |
1563 | exit = true; | 1543 | exit = true; |