summaryrefslogtreecommitdiff
path: root/apps/plugins/blackjack.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/blackjack.c')
-rw-r--r--apps/plugins/blackjack.c280
1 files changed, 143 insertions, 137 deletions
diff --git a/apps/plugins/blackjack.c b/apps/plugins/blackjack.c
index e1a856967f..97b44b3d3c 100644
--- a/apps/plugins/blackjack.c
+++ b/apps/plugins/blackjack.c
@@ -406,7 +406,7 @@ struct highscore highest[NUM_SCORES];
406#endif 406#endif
407 407
408#ifdef HAVE_TOUCHSCREEN 408#ifdef HAVE_TOUCHSCREEN
409#ifndef BJACK_DOUBLEDOWN 409#ifndef BJACK_DOUBLEDOWN
410#define BJACK_DOUBLEDOWN BUTTON_MIDLEFT 410#define BJACK_DOUBLEDOWN BUTTON_MIDLEFT
411#define BJACK_DOUBLE_NAME "BUTTON_MIDLEFT" 411#define BJACK_DOUBLE_NAME "BUTTON_MIDLEFT"
412#endif 412#endif
@@ -475,27 +475,26 @@ typedef struct card {
475typedef struct game_context { 475typedef struct game_context {
476 struct card player_cards[2][22]; /* 22 Cards means the deal was all aces */ 476 struct card player_cards[2][22]; /* 22 Cards means the deal was all aces */
477 struct card dealer_cards[22]; /* That is the worst-case scenario */ 477 struct card dealer_cards[22]; /* That is the worst-case scenario */
478 unsigned int player_total; 478 unsigned int player_total;
479 unsigned int dealer_total; 479 unsigned int dealer_total;
480 signed int player_money; 480 signed int player_money;
481 unsigned int num_player_cards[2]; 481 unsigned int num_player_cards[2];
482 unsigned int num_dealer_cards; 482 unsigned int num_dealer_cards;
483 unsigned int current_bet; 483 unsigned int current_bet;
484 unsigned int split_status; /* 0 = split hasn't been asked, * 484 unsigned int split_status; /* 0 = split hasn't been asked, *
485 * 1 = split did not occur * 485 * 1 = split did not occur *
486 * 2 = split occurred * 486 * 2 = split occurred *
487 * 3 = split occurred and 1st hand done */ 487 * 3 = split occurred and 1st hand done */
488 bool is_blackjack; 488 bool is_blackjack;
489 bool end_hand; 489 bool end_hand;
490 bool asked_insurance; 490 bool asked_insurance;
491 bool resume; 491 bool resume;
492 bool dirty;
493} game_context; 492} game_context;
494 493
495/***************************************************************************** 494/*****************************************************************************
496* blackjack_init() initializes blackjack data structures. 495* blackjack_init() initializes blackjack data structures.
497******************************************************************************/ 496******************************************************************************/
498static void blackjack_init(struct game_context* bj) { 497static void blackjack_init(struct game_context* bj) {
499 /* seed the rand generator */ 498 /* seed the rand generator */
500 rb->srand(*rb->current_tick); 499 rb->srand(*rb->current_tick);
501 500
@@ -504,7 +503,7 @@ static void blackjack_init(struct game_context* bj) {
504 dealer_y = LCD_HEIGHT/4 - CARD_HEIGHT/2; 503 dealer_y = LCD_HEIGHT/4 - CARD_HEIGHT/2;
505 player_x = 4; 504 player_x = 4;
506 player_y = LCD_HEIGHT - LCD_HEIGHT/4 - CARD_HEIGHT/2; 505 player_y = LCD_HEIGHT - LCD_HEIGHT/4 - CARD_HEIGHT/2;
507 506
508 /* check for resumed game */ 507 /* check for resumed game */
509 if(bj->resume) return; 508 if(bj->resume) return;
510 509
@@ -517,7 +516,7 @@ static void blackjack_init(struct game_context* bj) {
517 bj->end_hand = false; 516 bj->end_hand = false;
518 bj->split_status = 0; 517 bj->split_status = 0;
519 bj->is_blackjack = false; 518 bj->is_blackjack = false;
520 bj->asked_insurance = false; 519 bj->asked_insurance = false;
521} 520}
522 521
523/***************************************************************************** 522/*****************************************************************************
@@ -526,7 +525,7 @@ static void blackjack_init(struct game_context* bj) {
526static void blackjack_drawtable(struct game_context* bj) { 525static void blackjack_drawtable(struct game_context* bj) {
527 unsigned int w, h, y_loc; 526 unsigned int w, h, y_loc;
528 char str[10]; 527 char str[10];
529 528
530#if LCD_HEIGHT <= 64 529#if LCD_HEIGHT <= 64
531 rb->lcd_getstringsize("Bet", &w, &h); 530 rb->lcd_getstringsize("Bet", &w, &h);
532 rb->lcd_putsxy(LCD_WIDTH - w, 2*h + 1, "Bet"); 531 rb->lcd_putsxy(LCD_WIDTH - w, 2*h + 1, "Bet");
@@ -577,19 +576,19 @@ static unsigned int find_value(unsigned int number) {
577/***************************************************************************** 576/*****************************************************************************
578* draw_card() draws a card to the screen. 577* draw_card() draws a card to the screen.
579******************************************************************************/ 578******************************************************************************/
580static void draw_card(struct card temp_card, bool shown, unsigned int x, 579static void draw_card(struct card temp_card, bool shown,
581 unsigned int y) { 580 unsigned int x, unsigned int y) {
582 if(shown) 581 if(shown)
583 rb->lcd_bitmap_part(card_deck, CARD_WIDTH*temp_card.num, 582 rb->lcd_bitmap_part(card_deck, CARD_WIDTH*temp_card.num,
584 CARD_HEIGHT*temp_card.suit, BMPWIDTH_card_deck, 583 CARD_HEIGHT*temp_card.suit, BMPWIDTH_card_deck,
585 x+1, y+1, CARD_WIDTH, CARD_HEIGHT); 584 x+1, y+1, CARD_WIDTH, CARD_HEIGHT);
586 else 585 else
587 rb->lcd_bitmap(card_back, x+1, y+1,CARD_WIDTH, CARD_HEIGHT); 586 rb->lcd_bitmap(card_back, x+1, y+1,CARD_WIDTH, CARD_HEIGHT);
588#if LCD_DEPTH > 1 587#if LCD_DEPTH > 1
589 rb->lcd_set_foreground(LCD_BLACK); 588 rb->lcd_set_foreground(LCD_BLACK);
590#endif 589#endif
591 590
592 /* Print outlines */ 591 /* Print outlines */
593#if CARD_WIDTH >= 26 592#if CARD_WIDTH >= 26
594 rb->lcd_hline(x+2, x+CARD_WIDTH-1, y); 593 rb->lcd_hline(x+2, x+CARD_WIDTH-1, y);
595 rb->lcd_hline(x+2, x+CARD_WIDTH-1, y+CARD_HEIGHT+1); 594 rb->lcd_hline(x+2, x+CARD_WIDTH-1, y+CARD_HEIGHT+1);
@@ -618,7 +617,7 @@ static struct card new_card(void) {
618 struct card new_card; 617 struct card new_card;
619 new_card.suit = rb->rand()%4; /* Random number 0-3 */ 618 new_card.suit = rb->rand()%4; /* Random number 0-3 */
620 new_card.num = rb->rand()%13; /* Random number 0-12 */ 619 new_card.num = rb->rand()%13; /* Random number 0-12 */
621 new_card.value = find_value(new_card.num); 620 new_card.value = find_value(new_card.num);
622 new_card.is_soft_ace = new_card.num == 0 ? true : false; 621 new_card.is_soft_ace = new_card.num == 0 ? true : false;
623 return new_card; 622 return new_card;
624} 623}
@@ -645,7 +644,7 @@ static void deal_init_cards(struct game_context* bj) {
645 bj->player_cards[0][1] = new_card(); 644 bj->player_cards[0][1] = new_card();
646 bj->player_total += bj->player_cards[0][1].value; 645 bj->player_total += bj->player_cards[0][1].value;
647 draw_card(bj->player_cards[0][1], true, player_x, player_y); 646 draw_card(bj->player_cards[0][1], true, player_x, player_y);
648 player_x += CARD_WIDTH + 4; 647 player_x += CARD_WIDTH + 4;
649} 648}
650 649
651/***************************************************************************** 650/*****************************************************************************
@@ -669,7 +668,7 @@ static void redraw_board(struct game_context* bj) {
669 dealer_x += CARD_WIDTH + 4; 668 dealer_x += CARD_WIDTH + 4;
670 } 669 }
671 draw_card(bj->dealer_cards[i], true, dealer_x, dealer_y); 670 draw_card(bj->dealer_cards[i], true, dealer_x, dealer_y);
672 671
673 if (bj->num_dealer_cards > MAX_CARDS-1) 672 if (bj->num_dealer_cards > MAX_CARDS-1)
674 dealer_x += 10; 673 dealer_x += 10;
675 else 674 else
@@ -682,7 +681,7 @@ static void redraw_board(struct game_context* bj) {
682 if (bj->split_status>1 || bj->num_player_cards[n]>MAX_CARDS) 681 if (bj->split_status>1 || bj->num_player_cards[n]>MAX_CARDS)
683 player_x += 10; 682 player_x += 10;
684 else 683 else
685 player_x += CARD_WIDTH + 4; 684 player_x += CARD_WIDTH + 4;
686 } 685 }
687 if (bj->split_status > 1) 686 if (bj->split_status > 1)
688 player_x = LCD_WIDTH/2 + 4; 687 player_x = LCD_WIDTH/2 + 4;
@@ -709,12 +708,11 @@ static void update_total(struct game_context* bj) {
709* check_for_aces() is passed an array of cards and returns where an ace is 708* check_for_aces() is passed an array of cards and returns where an ace is
710* located. Otherwise, returns -1. 709* located. Otherwise, returns -1.
711******************************************************************************/ 710******************************************************************************/
712static signed int check_for_aces(struct card temp_cards[], 711static signed int check_for_aces(struct card temp_cards[], unsigned int size) {
713 unsigned int size) {
714 unsigned int i; 712 unsigned int i;
715 for(i = 0; i < size; i++) { 713 for(i = 0; i < size; i++) {
716 if (temp_cards[i].is_soft_ace == true) 714 if (temp_cards[i].is_soft_ace == true)
717 return i; 715 return i;
718 } 716 }
719 return -1; 717 return -1;
720} 718}
@@ -723,26 +721,26 @@ static signed int check_for_aces(struct card temp_cards[],
723* check_totals() compares player and dealer totals. 721* check_totals() compares player and dealer totals.
724* 0: bust 1: loss, 2: push, 3: win, 4: blackjack, 5: something's not right... 722* 0: bust 1: loss, 2: push, 3: win, 4: blackjack, 5: something's not right...
725******************************************************************************/ 723******************************************************************************/
726static unsigned int check_totals(struct game_context* bj) 724static unsigned int check_totals(struct game_context* bj) {
727{
728 unsigned int temp; 725 unsigned int temp;
729 if (bj->player_total > 21) 726 if (bj->player_total > 21)
730 temp = 0; 727 temp = 0;
731 else if (bj->player_total == 21 && bj->is_blackjack) 728 else if (bj->player_total == 21 && bj->is_blackjack) {
732 if (bj->dealer_total == 21 && bj->num_dealer_cards == 2) 729 if (bj->dealer_total == 21 && bj->num_dealer_cards == 2)
733 temp = 2; 730 temp = 2;
734 else 731 else
735 temp = 4; 732 temp = 4;
733 }
736 else if (bj->player_total == bj->dealer_total) 734 else if (bj->player_total == bj->dealer_total)
737 temp = 2; 735 temp = 2;
738 else if (bj->dealer_total > 21 && bj->player_total < 22) 736 else if (bj->dealer_total > 21 && bj->player_total < 22)
739 temp = 3; 737 temp = 3;
740 else if (bj->dealer_total > bj->player_total) 738 else if (bj->dealer_total > bj->player_total)
741 temp = 1; 739 temp = 1;
742 else if (bj->player_total > bj->dealer_total) 740 else if (bj->player_total > bj->dealer_total)
743 temp = 3; 741 temp = 3;
744 else 742 else
745 temp = 5; 743 temp = 5;
746 744
747 return temp; 745 return temp;
748} 746}
@@ -752,24 +750,24 @@ static unsigned int check_totals(struct game_context* bj)
752******************************************************************************/ 750******************************************************************************/
753static void finish_dealer(struct game_context* bj) { 751static void finish_dealer(struct game_context* bj) {
754 signed int temp = 0; 752 signed int temp = 0;
755 753
756 if (bj->dealer_total > 16 && bj->dealer_total < 22) 754 if (bj->dealer_total > 16 && bj->dealer_total < 22)
757 return; 755 return;
758 756
759 while (bj->dealer_total < 17) { 757 while (bj->dealer_total < 17) {
760 bj->dealer_cards[bj->num_dealer_cards] = new_card(); 758 bj->dealer_cards[bj->num_dealer_cards] = new_card();
761 bj->dealer_total += bj->dealer_cards[bj->num_dealer_cards].value; 759 bj->dealer_total += bj->dealer_cards[bj->num_dealer_cards].value;
762 bj->num_dealer_cards++; 760 bj->num_dealer_cards++;
763 } 761 }
764 762
765 while (bj->dealer_total > 21) { 763 while (bj->dealer_total > 21) {
766 temp = check_for_aces(bj->dealer_cards, bj->num_dealer_cards); 764 temp = check_for_aces(bj->dealer_cards, bj->num_dealer_cards);
767 if(temp != -1) { 765 if(temp != -1) {
768 bj->dealer_cards[temp].is_soft_ace = false; 766 bj->dealer_cards[temp].is_soft_ace = false;
769 bj->dealer_total -= 10; 767 bj->dealer_total -= 10;
770 } 768 }
771 else 769 else
772 return; 770 return;
773 } 771 }
774} 772}
775 773
@@ -783,31 +781,31 @@ static void finish_game(struct game_context* bj) {
783 do { 781 do {
784 finish_dealer(bj); 782 finish_dealer(bj);
785 } while (bj->dealer_total < 17); 783 } while (bj->dealer_total < 17);
786 784
787 redraw_board(bj); 785 redraw_board(bj);
788 rValue = check_totals(bj); 786 rValue = check_totals(bj);
789 787
790 if (rValue == 0) { 788 if (rValue == 0) {
791 rb->snprintf(str, sizeof(str), " Bust! "); 789 rb->snprintf(str, sizeof(str), " Bust! ");
792 bj->player_money -= bj->current_bet; 790 bj->player_money -= bj->current_bet;
793 } 791 }
794 else if (rValue == 1) { 792 else if (rValue == 1) {
795 rb->snprintf(str, sizeof(str), " Sorry, you lost. "); 793 rb->snprintf(str, sizeof(str), " Sorry, you lost. ");
796 bj->player_money -= bj->current_bet; 794 bj->player_money -= bj->current_bet;
797 } 795 }
798 else if (rValue == 2) { 796 else if (rValue == 2) {
799 rb->snprintf(str, sizeof(str), " Push "); 797 rb->snprintf(str, sizeof(str), " Push ");
800 } 798 }
801 else if (rValue == 3) { 799 else if (rValue == 3) {
802 rb->snprintf(str, sizeof(str), " You won! "); 800 rb->snprintf(str, sizeof(str), " You won! ");
803 bj->player_money+= bj->current_bet; 801 bj->player_money+= bj->current_bet;
804 } 802 }
805 else { 803 else {
806 rb->snprintf(str, sizeof(str), " Blackjack! "); 804 rb->snprintf(str, sizeof(str), " Blackjack! ");
807 bj->player_money += bj->current_bet * 3 / 2; 805 bj->player_money += bj->current_bet * 3 / 2;
808 } 806 }
809 rb->lcd_getstringsize(str, &w, &h); 807 rb->lcd_getstringsize(str, &w, &h);
810 808
811#if LCD_HEIGHT <= 64 809#if LCD_HEIGHT <= 64
812 rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID); 810 rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
813 rb->lcd_fillrect(0, LCD_HEIGHT/2, LCD_WIDTH, LCD_HEIGHT/2); 811 rb->lcd_fillrect(0, LCD_HEIGHT/2, LCD_WIDTH, LCD_HEIGHT/2);
@@ -835,7 +833,7 @@ static bool blackjack_loadgame(struct game_context* bj) {
835 833
836 /* read in saved game */ 834 /* read in saved game */
837 while(true) { 835 while(true) {
838 if(rb->read(fd, &bj->player_money, sizeof(bj->player_money)) <= 0) 836 if(rb->read(fd, &bj->player_money, sizeof(bj->player_money)) <= 0)
839 break; 837 break;
840 if(rb->read(fd, &bj->player_total, sizeof(bj->player_total)) <= 0) 838 if(rb->read(fd, &bj->player_total, sizeof(bj->player_total)) <= 0)
841 break; 839 break;
@@ -845,19 +843,19 @@ static bool blackjack_loadgame(struct game_context* bj) {
845 break; 843 break;
846 if(rb->read(fd, &bj->num_dealer_cards, sizeof(bj->num_dealer_cards))<=0) 844 if(rb->read(fd, &bj->num_dealer_cards, sizeof(bj->num_dealer_cards))<=0)
847 break; 845 break;
848 if(rb->read(fd, &bj->current_bet, sizeof(bj->current_bet)) <= 0) 846 if(rb->read(fd, &bj->current_bet, sizeof(bj->current_bet)) <= 0)
849 break; 847 break;
850 if(rb->read(fd, &bj->is_blackjack, sizeof(bj->is_blackjack)) <= 0) 848 if(rb->read(fd, &bj->is_blackjack, sizeof(bj->is_blackjack)) <= 0)
851 break; 849 break;
852 if(rb->read(fd, &bj->split_status, sizeof(bj->split_status)) <= 0) 850 if(rb->read(fd, &bj->split_status, sizeof(bj->split_status)) <= 0)
853 break; 851 break;
854 if(rb->read(fd, &bj->asked_insurance, sizeof(bj->asked_insurance)) <= 0) 852 if(rb->read(fd, &bj->asked_insurance, sizeof(bj->asked_insurance)) <= 0)
855 break; 853 break;
856 if(rb->read(fd, &bj->end_hand, sizeof(bj->end_hand)) <= 0) 854 if(rb->read(fd, &bj->end_hand, sizeof(bj->end_hand)) <= 0)
857 break; 855 break;
858 if(rb->read(fd, &bj->player_cards, sizeof(bj->player_cards)) <= 0) 856 if(rb->read(fd, &bj->player_cards, sizeof(bj->player_cards)) <= 0)
859 break; 857 break;
860 if(rb->read(fd, &bj->dealer_cards, sizeof(bj->dealer_cards)) <= 0) 858 if(rb->read(fd, &bj->dealer_cards, sizeof(bj->dealer_cards)) <= 0)
861 break; 859 break;
862 bj->resume = true; 860 bj->resume = true;
863 loaded = true; 861 loaded = true;
@@ -911,7 +909,7 @@ static unsigned int blackjack_get_yes_no(char message[20]) {
911 rb->strcat(message_no, " No"); 909 rb->strcat(message_no, " No");
912 rb->lcd_getstringsize(message_yes, &w, &h); 910 rb->lcd_getstringsize(message_yes, &w, &h);
913 const char *stg[] = {message_yes, message_no}; 911 const char *stg[] = {message_yes, message_no};
914 912
915#if LCD_HEIGHT <= 64 913#if LCD_HEIGHT <= 64
916 b = 2*h+1; 914 b = 2*h+1;
917#else 915#else
@@ -928,10 +926,10 @@ static unsigned int blackjack_get_yes_no(char message[20]) {
928 rb->lcd_set_drawmode(DRMODE_SOLID); 926 rb->lcd_set_drawmode(DRMODE_SOLID);
929#endif 927#endif
930 rb->lcd_drawrect(LCD_WIDTH/2 - w/2 - 1, LCD_HEIGHT/2 + b - 1, w+3, h+4); 928 rb->lcd_drawrect(LCD_WIDTH/2 - w/2 - 1, LCD_HEIGHT/2 + b - 1, w+3, h+4);
931 929
932 while(!breakout) { 930 while(!breakout) {
933 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + b +1, stg[choice]); 931 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + b +1, stg[choice]);
934 rb->lcd_update_rect(LCD_WIDTH/2 - w/2 - 1, LCD_HEIGHT/2 + b -1, 932 rb->lcd_update_rect(LCD_WIDTH/2 - w/2 - 1, LCD_HEIGHT/2 + b -1,
935 w+3, h+4); 933 w+3, h+4);
936 button = rb->button_get(true); 934 button = rb->button_get(true);
937 935
@@ -949,7 +947,7 @@ static unsigned int blackjack_get_yes_no(char message[20]) {
949 break; 947 break;
950 } 948 }
951 } 949 }
952 950
953#if LCD_DEPTH > 1 951#if LCD_DEPTH > 1
954 rb->lcd_set_foreground(FG_COLOR); 952 rb->lcd_set_foreground(FG_COLOR);
955 rb->lcd_set_background(BG_COLOR); 953 rb->lcd_set_background(BG_COLOR);
@@ -961,16 +959,16 @@ static unsigned int blackjack_get_yes_no(char message[20]) {
961* blackjack_get_amount() gets an amount from the player to be used 959* blackjack_get_amount() gets an amount from the player to be used
962******************************************************************************/ 960******************************************************************************/
963static signed int blackjack_get_amount(char message[20], signed int lower_limit, 961static signed int blackjack_get_amount(char message[20], signed int lower_limit,
964 signed int upper_limit, 962 signed int upper_limit,
965 signed int start) { 963 signed int start) {
966 int button; 964 int button;
967 char str[6]; 965 char str[9];
968 bool changed = false; 966 bool changed = false;
969 unsigned int w, h; 967 unsigned int w, h;
970 signed int amount; 968 signed int amount;
971 969
972 rb->lcd_getstringsize("A", &w, &h); /* find the size of one character */ 970 rb->lcd_getstringsize("A", &w, &h); /* find the size of one character */
973 971
974 if (start > upper_limit) 972 if (start > upper_limit)
975 amount = upper_limit; 973 amount = upper_limit;
976 else if (start < lower_limit) 974 else if (start < lower_limit)
@@ -995,11 +993,11 @@ static signed int blackjack_get_amount(char message[20], signed int lower_limit,
995 rb->lcd_update(); 993 rb->lcd_update();
996#else 994#else
997 rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID); 995 rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
998 rb->lcd_fillrect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3, 37*w / 2, 996 rb->lcd_fillrect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3,
999 8*h -3); 997 37*w / 2, 8*h -3);
1000 rb->lcd_set_drawmode(DRMODE_SOLID); 998 rb->lcd_set_drawmode(DRMODE_SOLID);
1001 rb->lcd_drawrect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3, 37*w / 2, 999 rb->lcd_drawrect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3,
1002 8*h -3); 1000 37*w / 2, 8*h -3);
1003 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 4*h - 1, message); 1001 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 4*h - 1, message);
1004 rb->snprintf(str, 9, "$%d", amount); 1002 rb->snprintf(str, 9, "$%d", amount);
1005 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, str); 1003 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, str);
@@ -1021,21 +1019,21 @@ static signed int blackjack_get_amount(char message[20], signed int lower_limit,
1021 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + h, "UP: +10"); 1019 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + h, "UP: +10");
1022 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + 2*h + 1, "DOWN: -10"); 1020 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + 2*h + 1, "DOWN: -10");
1023#endif 1021#endif
1024 rb->lcd_update_rect(LCD_WIDTH/2 - 9*w - 2, LCD_HEIGHT/2 - 9*h/2, 37*w/2 + 1, 1022 rb->lcd_update_rect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3,
1025 8*h-2); 1023 37*w / 2, 8*h -3);
1026#endif 1024#endif
1027 1025
1028 while(true) { 1026 while(true) {
1029 button = rb->button_get(true); 1027 button = rb->button_get(true);
1030 1028
1031 switch(button) { 1029 switch(button) {
1032 case BJACK_UP: 1030 case BJACK_UP:
1033 case (BJACK_UP|BUTTON_REPEAT): 1031 case (BJACK_UP|BUTTON_REPEAT):
1034 if (amount + 10 < upper_limit + 1) { 1032 if (amount + 10 < upper_limit + 1) {
1035 amount += 10; 1033 amount += 10;
1036 changed = true; 1034 changed = true;
1037 } 1035 }
1038 break; 1036 break;
1039 case BJACK_DOWN: 1037 case BJACK_DOWN:
1040 case (BJACK_DOWN|BUTTON_REPEAT): 1038 case (BJACK_DOWN|BUTTON_REPEAT):
1041 if (amount - 10 > lower_limit - 1) { 1039 if (amount - 10 > lower_limit - 1) {
@@ -1074,9 +1072,9 @@ static signed int blackjack_get_amount(char message[20], signed int lower_limit,
1074#endif 1072#endif
1075 rb->lcd_clear_display(); 1073 rb->lcd_clear_display();
1076 return amount; 1074 return amount;
1077 } 1075 }
1078 1076
1079 if(changed) { 1077 if(changed) {
1080 rb->snprintf(str, 9, "$%d", amount); 1078 rb->snprintf(str, 9, "$%d", amount);
1081#if LCD_HEIGHT <= 64 1079#if LCD_HEIGHT <= 64
1082 rb->lcd_puts(0, 2, str); 1080 rb->lcd_puts(0, 2, str);
@@ -1097,7 +1095,7 @@ static signed int blackjack_get_amount(char message[20], signed int lower_limit,
1097* blackjack_get_bet() gets the player's bet. 1095* blackjack_get_bet() gets the player's bet.
1098******************************************************************************/ 1096******************************************************************************/
1099static void blackjack_get_bet(struct game_context* bj) { 1097static void blackjack_get_bet(struct game_context* bj) {
1100 bj->current_bet = blackjack_get_amount("Please enter a bet", 10, 1098 bj->current_bet = blackjack_get_amount("Please enter a bet", 10,
1101 bj->player_money, bj->current_bet); 1099 bj->player_money, bj->current_bet);
1102} 1100}
1103 1101
@@ -1133,11 +1131,11 @@ static void split(struct game_context* bj) {
1133* insurance() see if the player wants to buy insurance and how much. 1131* insurance() see if the player wants to buy insurance and how much.
1134******************************************************************************/ 1132******************************************************************************/
1135static unsigned int insurance(struct game_context* bj) { 1133static unsigned int insurance(struct game_context* bj) {
1136 unsigned int insurance, max_amount; 1134 unsigned int insurance, max_amount;
1137 1135
1138 insurance = blackjack_get_yes_no("Buy Insurance?"); 1136 insurance = blackjack_get_yes_no("Buy Insurance?");
1139 bj->asked_insurance = true; 1137 bj->asked_insurance = true;
1140 max_amount = bj->current_bet < (unsigned int)bj->player_money ? 1138 max_amount = bj->current_bet < (unsigned int)bj->player_money ?
1141 bj->current_bet/2 : (unsigned int)bj->player_money; 1139 bj->current_bet/2 : (unsigned int)bj->player_money;
1142 if (insurance == 1) return 0; 1140 if (insurance == 1) return 0;
1143 1141
@@ -1153,7 +1151,10 @@ static unsigned int play_again(void) {
1153 return blackjack_get_yes_no("Play Again?"); 1151 return blackjack_get_yes_no("Play Again?");
1154} 1152}
1155 1153
1156void showhelp(void) { 1154/*****************************************************************************
1155* blackjack_help() displays help text.
1156******************************************************************************/
1157static bool blackjack_help(void) {
1157#define WORDS (sizeof help_text / sizeof (char*)) 1158#define WORDS (sizeof help_text / sizeof (char*))
1158 static char *help_text[] = { 1159 static char *help_text[] = {
1159 "Blackjack", "", 1160 "Blackjack", "",
@@ -1184,16 +1185,16 @@ void showhelp(void) {
1184#endif 1185#endif
1185 1186
1186 if (display_text(WORDS, help_text, formation, NULL)) 1187 if (display_text(WORDS, help_text, formation, NULL))
1187 return; 1188 return true;
1188 do { 1189 do {
1189 button = rb->button_get(true); 1190 button = rb->button_get(true);
1190 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) { 1191 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
1191 return; 1192 return true;
1192 } 1193 }
1193 } while( ( button == BUTTON_NONE ) 1194 } while( ( button == BUTTON_NONE )
1194 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) ); 1195 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
1195 rb->lcd_setfont(FONT_SYSFIXED); 1196 rb->lcd_setfont(FONT_SYSFIXED);
1196 return; 1197 return false;
1197} 1198}
1198 1199
1199/***************************************************************************** 1200/*****************************************************************************
@@ -1202,9 +1203,11 @@ void showhelp(void) {
1202static unsigned int blackjack_menu(struct game_context* bj) { 1203static unsigned int blackjack_menu(struct game_context* bj) {
1203 int selection=0; 1204 int selection=0;
1204 bool breakout = false; 1205 bool breakout = false;
1205 1206
1206 MENUITEM_STRINGLIST(menu,"BlackJack Menu",NULL,"Start Game","Resume Game", 1207 MENUITEM_STRINGLIST(menu, "BlackJack Menu", NULL,
1207 "High Scores", "Help", "Playback Control", "Quit"); 1208 "Start Game","Resume Game",
1209 "High Scores", "Help",
1210 "Playback Control", "Quit");
1208 1211
1209 while(!breakout) { 1212 while(!breakout) {
1210 switch(rb->do_menu(&menu, &selection, NULL, false)) { 1213 switch(rb->do_menu(&menu, &selection, NULL, false)) {
@@ -1215,7 +1218,6 @@ static unsigned int blackjack_menu(struct game_context* bj) {
1215 if(!blackjack_loadgame(bj)) { 1218 if(!blackjack_loadgame(bj)) {
1216 rb->splash(HZ*2, "Nothing to resume"); 1219 rb->splash(HZ*2, "Nothing to resume");
1217 } else { 1220 } else {
1218 rb->splash(HZ*2, "Loading...");
1219 breakout = true; 1221 breakout = true;
1220 } 1222 }
1221 break; 1223 break;
@@ -1223,18 +1225,19 @@ static unsigned int blackjack_menu(struct game_context* bj) {
1223 highscore_show(NUM_SCORES, highest, NUM_SCORES, false); 1225 highscore_show(NUM_SCORES, highest, NUM_SCORES, false);
1224 break; 1226 break;
1225 case 3: 1227 case 3:
1226 showhelp(); 1228 if(blackjack_help())
1229 return BJ_USB;
1227 break; 1230 break;
1228 case 4: 1231 case 4:
1229 if (playback_control(NULL)) 1232 if (playback_control(NULL))
1230 return 1; 1233 return BJ_USB;
1231 break; 1234 break;
1232 case 5: 1235 case 5:
1233 return BJ_QUIT; 1236 return BJ_QUIT;
1234 1237
1235 case MENU_ATTACHED_USB: 1238 case MENU_ATTACHED_USB:
1236 return BJ_USB; 1239 return BJ_USB;
1237 1240
1238 default: 1241 default:
1239 break; 1242 break;
1240 } 1243 }
@@ -1253,6 +1256,11 @@ static int blackjack(struct game_context* bj) {
1253 bool breakout = false; 1256 bool breakout = false;
1254 bool dbl_down = false; 1257 bool dbl_down = false;
1255 1258
1259#if LCD_DEPTH > 1
1260 rb->lcd_set_background(BG_COLOR);
1261 rb->lcd_set_foreground(FG_COLOR);
1262#endif
1263
1256 /* don't resume by default */ 1264 /* don't resume by default */
1257 bj->resume = false; 1265 bj->resume = false;
1258 1266
@@ -1268,7 +1276,6 @@ static int blackjack(struct game_context* bj) {
1268 * init * 1276 * init *
1269 ********************/ 1277 ********************/
1270 blackjack_init(bj); 1278 blackjack_init(bj);
1271 bj->current_bet=10;
1272 1279
1273 /******************** 1280 /********************
1274 * play * 1281 * play *
@@ -1279,21 +1286,21 @@ static int blackjack(struct game_context* bj) {
1279 bj->resume = false; 1286 bj->resume = false;
1280 redraw_board(bj); 1287 redraw_board(bj);
1281 if (bj->split_status == 2) { 1288 if (bj->split_status == 2) {
1282 todo=2; 1289 todo=2;
1283 player_x = bj->num_player_cards[0] * 10 + 4; 1290 player_x = bj->num_player_cards[0] * 10 + 4;
1284 } 1291 }
1285 else if (bj->split_status == 3) { 1292 else if (bj->split_status == 3) {
1286 player_x = bj->num_player_cards[1] * 10 + LCD_WIDTH/2 + 4; 1293 player_x = bj->num_player_cards[1] * 10 + LCD_WIDTH/2 + 4;
1287 todo=2; 1294 todo=2;
1288 done=1; 1295 done=1;
1289 } 1296 }
1290
1291 } 1297 }
1292 else { 1298 else {
1293 bj->player_money = 1000; 1299 bj->player_money = 1000;
1300 bj->current_bet = 10;
1294 blackjack_get_bet(bj); 1301 blackjack_get_bet(bj);
1295 if (bj->current_bet == 0) 1302 if (bj->current_bet == 0)
1296 return BJ_QUIT; 1303 return -1;
1297 rb->lcd_clear_display(); 1304 rb->lcd_clear_display();
1298 deal_init_cards(bj); 1305 deal_init_cards(bj);
1299 blackjack_drawtable(bj); 1306 blackjack_drawtable(bj);
@@ -1309,7 +1316,7 @@ static int blackjack(struct game_context* bj) {
1309 bj->end_hand = true; 1316 bj->end_hand = true;
1310 finish_game(bj); 1317 finish_game(bj);
1311 } 1318 }
1312 else if(bj->dealer_cards[1].is_soft_ace && !breakout && 1319 else if(bj->dealer_cards[1].is_soft_ace && !breakout &&
1313 !bj->asked_insurance) { 1320 !bj->asked_insurance) {
1314 temp_var = insurance(bj); 1321 temp_var = insurance(bj);
1315 if (bj->dealer_total == 21) { 1322 if (bj->dealer_total == 21) {
@@ -1328,7 +1335,7 @@ static int blackjack(struct game_context* bj) {
1328 rb->lcd_update(); 1335 rb->lcd_update();
1329 } 1336 }
1330 } 1337 }
1331 if(!bj->end_hand && bj->split_status == 0 && 1338 if(!bj->end_hand && bj->split_status == 0 &&
1332 bj->player_cards[0][0].num == bj->player_cards[0][1].num) { 1339 bj->player_cards[0][0].num == bj->player_cards[0][1].num) {
1333 split(bj); 1340 split(bj);
1334 redraw_board(bj); 1341 redraw_board(bj);
@@ -1350,16 +1357,16 @@ static int blackjack(struct game_context* bj) {
1350 bj->num_player_cards[done]++; 1357 bj->num_player_cards[done]++;
1351 if (bj->num_player_cards[done] == MAX_CARDS + 1) { 1358 if (bj->num_player_cards[done] == MAX_CARDS + 1) {
1352 redraw_board(bj); 1359 redraw_board(bj);
1353 rb->lcd_update_rect(0, LCD_HEIGHT/2, LCD_WIDTH, 1360 rb->lcd_update_rect(0, LCD_HEIGHT/2, LCD_WIDTH,
1354 LCD_HEIGHT/2); 1361 LCD_HEIGHT/2);
1355 } 1362 }
1356 else if (bj->num_player_cards[done]>MAX_CARDS || todo > 1) { 1363 else if (bj->num_player_cards[done]>MAX_CARDS || todo > 1) {
1357 rb->lcd_update_rect(player_x, player_y, CARD_WIDTH+2, 1364 rb->lcd_update_rect(player_x, player_y, CARD_WIDTH+2,
1358 CARD_HEIGHT+2); 1365 CARD_HEIGHT+2);
1359 player_x += 10; 1366 player_x += 10;
1360 } 1367 }
1361 else { 1368 else {
1362 rb->lcd_update_rect(player_x, player_y, CARD_WIDTH+2, 1369 rb->lcd_update_rect(player_x, player_y, CARD_WIDTH+2,
1363 CARD_HEIGHT+2); 1370 CARD_HEIGHT+2);
1364 player_x += CARD_WIDTH + 4; 1371 player_x += CARD_WIDTH + 4;
1365 } 1372 }
@@ -1370,7 +1377,8 @@ static int blackjack(struct game_context* bj) {
1370 bj->end_hand = true; 1377 bj->end_hand = true;
1371 break; 1378 break;
1372 case BJACK_DOUBLEDOWN: 1379 case BJACK_DOUBLEDOWN:
1373 if ((signed int)bj->current_bet * 2 < bj->player_money + 1&& 1380 if ((signed int)bj->current_bet * 2 <
1381 bj->player_money + 1 &&
1374 bj->num_player_cards[0]==2 && todo==1) { 1382 bj->num_player_cards[0]==2 && todo==1) {
1375 double_down(bj); 1383 double_down(bj);
1376 dbl_down = true; 1384 dbl_down = true;
@@ -1379,7 +1387,8 @@ static int blackjack(struct game_context* bj) {
1379 finish_game(bj); 1387 finish_game(bj);
1380 } 1388 }
1381 } 1389 }
1382 else if((signed int)bj->current_bet * 2 > bj->player_money){ 1390 else if((signed int)bj->current_bet * 2 >
1391 bj->player_money){
1383 rb->splash(HZ, "Not enough money to double down."); 1392 rb->splash(HZ, "Not enough money to double down.");
1384 redraw_board(bj); 1393 redraw_board(bj);
1385 rb->lcd_update(); 1394 rb->lcd_update();
@@ -1395,7 +1404,7 @@ static int blackjack(struct game_context* bj) {
1395 } 1404 }
1396 1405
1397 while (bj->player_total > 21 && !bj->end_hand) { 1406 while (bj->player_total > 21 && !bj->end_hand) {
1398 temp = check_for_aces(bj->player_cards[done], 1407 temp = check_for_aces(bj->player_cards[done],
1399 bj->num_player_cards[done]); 1408 bj->num_player_cards[done]);
1400 if(temp != -1) { 1409 if(temp != -1) {
1401 bj->player_cards[done][temp].is_soft_ace = false; 1410 bj->player_cards[done][temp].is_soft_ace = false;
@@ -1409,7 +1418,7 @@ static int blackjack(struct game_context* bj) {
1409 else 1418 else
1410 bj->end_hand = true; 1419 bj->end_hand = true;
1411 } 1420 }
1412 1421
1413 if (bj->end_hand) { 1422 if (bj->end_hand) {
1414 done++; 1423 done++;
1415 if(todo > 1) { 1424 if(todo > 1) {
@@ -1426,7 +1435,7 @@ static int blackjack(struct game_context* bj) {
1426 bj->current_bet /= 2; 1435 bj->current_bet /= 2;
1427 rb->lcd_update_rect(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2, 1436 rb->lcd_update_rect(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
1428 w,h); 1437 w,h);
1429 rb->sleep(HZ*2); 1438 rb->sleep(HZ*2);
1430 bj->player_total = temp_var; 1439 bj->player_total = temp_var;
1431 finish_game(bj); 1440 finish_game(bj);
1432 rb->lcd_getstringsize(" Split 2 ", &w, &h); 1441 rb->lcd_getstringsize(" Split 2 ", &w, &h);
@@ -1446,7 +1455,7 @@ static int blackjack(struct game_context* bj) {
1446 player_x += 10; 1455 player_x += 10;
1447 rb->lcd_update(); 1456 rb->lcd_update();
1448 } 1457 }
1449 } 1458 }
1450 else 1459 else
1451 finish_game(bj); 1460 finish_game(bj);
1452 } 1461 }
@@ -1462,7 +1471,11 @@ static int blackjack(struct game_context* bj) {
1462 return BJ_END; 1471 return BJ_END;
1463 else { /* User keeps playing */ 1472 else { /* User keeps playing */
1464 breakout = false; 1473 breakout = false;
1474 temp = bj->current_bet;
1475 bj->current_bet = 0;
1465 redraw_board(bj); 1476 redraw_board(bj);
1477 rb->lcd_update();
1478 bj->current_bet = temp;
1466 if(dbl_down) { 1479 if(dbl_down) {
1467 bj->current_bet /= 2; 1480 bj->current_bet /= 2;
1468 dbl_down = false; 1481 dbl_down = false;
@@ -1477,7 +1490,7 @@ static int blackjack(struct game_context* bj) {
1477 blackjack_drawtable(bj); 1490 blackjack_drawtable(bj);
1478 rb->lcd_update(); 1491 rb->lcd_update();
1479 } 1492 }
1480 } 1493 }
1481 } 1494 }
1482 /* Never reached */ 1495 /* Never reached */
1483 return PLUGIN_OK; 1496 return PLUGIN_OK;
@@ -1509,33 +1522,26 @@ enum plugin_status plugin_start(const void* parameter)
1509 /* fall through to BJ_END */ 1522 /* fall through to BJ_END */
1510 1523
1511 case BJ_END: 1524 case BJ_END:
1512 if(!bj.resume) { 1525 if(!bj.resume && bj.player_money > 10) {
1513 /* There is no level, so store -1 to blank column */ 1526 /* There is no level, so store -1 to blank column */
1514 int position = highscore_update(bj.player_money, -1, "", 1527 int position = highscore_update(bj.player_money, -1, "",
1515 highest, NUM_SCORES); 1528 highest, NUM_SCORES);
1516 if (position == 0) { 1529 if (position == 0) {
1517 rb->splash(HZ*2, "New High Score"); 1530 rb->splash(HZ*2, "New High Score");
1518 } 1531 }
1519 if (position != -1) { 1532 if (position != -1) {
1520 highscore_show(position, highest, NUM_SCORES, false); 1533 highscore_show(position, highest, NUM_SCORES, false);
1521 bj.dirty=true;
1522 } else {
1523 rb->sleep(3);
1524 } 1534 }
1525 } 1535 }
1526 break; 1536 break;
1527 1537
1528 case BJ_USB: 1538 case BJ_USB:
1529 rb->lcd_setfont(FONT_UI); 1539 rb->lcd_setfont(FONT_UI);
1530 if(bj.dirty) { 1540 highscore_save(HIGH_SCORE,highest,NUM_SCORES);
1531 highscore_save(HIGH_SCORE,highest,NUM_SCORES);
1532 }
1533 return PLUGIN_USB_CONNECTED; 1541 return PLUGIN_USB_CONNECTED;
1534 1542
1535 case BJ_QUIT: 1543 case BJ_QUIT:
1536 if(bj.dirty) { 1544 highscore_save(HIGH_SCORE,highest,NUM_SCORES);
1537 highscore_save(HIGH_SCORE,highest,NUM_SCORES);
1538 }
1539 exit = true; 1545 exit = true;
1540 break; 1546 break;
1541 1547