1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Tom Ross
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 #include "card_deck.h"
22 #include "card_back.h"
27 #define SCORE_FILE PLUGIN_GAMES_DIR "/blackjack.score"
28 #define SAVE_FILE PLUGIN_GAMES_DIR "/blackjack.save"
30 #define NUM_SCORES LCD_HEIGHT/8-2
32 /* final game return status */
38 #if CONFIG_KEYPAD == RECORDER_PAD
39 #define BJACK_START BUTTON_ON
40 #define BJACK_QUIT BUTTON_OFF
41 #define BJACK_MAX (BUTTON_ON|BUTTON_UP)
42 #define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
43 #define BJACK_HIT BUTTON_F1
44 #define BJACK_STAY BUTTON_F2
45 #define BJACK_DOUBLEDOWN BUTTON_F3
46 #define BJACK_SCORES BUTTON_RIGHT
47 #define BJACK_RESUME BUTTON_PLAY
48 #define BJACK_UP BUTTON_UP
49 #define BJACK_DOWN BUTTON_DOWN
50 #define BJACK_RIGHT BUTTON_RIGHT
51 #define BJACK_LEFT BUTTON_LEFT
53 #elif CONFIG_KEYPAD == ONDIO_PAD
54 #define BJACK_START BUTTON_MENU
55 #define BJACK_QUIT BUTTON_OFF
56 #define BJACK_MAX (BUTTON_MENU|BUTTON_UP)
57 #define BJACK_MIN (BUTTON_MENU|BUTTON_DOWN)
58 #define BJACK_HIT BUTTON_LEFT
59 #define BJACK_STAY BUTTON_RIGHT
60 #define BJACK_DOUBLEDOWN BUTTON_UP
61 #define BJACK_SCORES BUTTON_UP
62 #define BJACK_RESUME BUTTON_DOWN
63 #define BJACK_UP BUTTON_UP
64 #define BJACK_DOWN BUTTON_DOWN
65 #define BJACK_RIGHT BUTTON_RIGHT
66 #define BJACK_LEFT BUTTON_LEFT
68 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
69 #define BJACK_START BUTTON_PLAY
70 #define BJACK_QUIT BUTTON_POWER
71 #define BJACK_MAX (BUTTON_PLAY|BUTTON_SCROLL_UP)
72 #define BJACK_MIN (BUTTON_PLAY|BUTTON_SCROLL_DOWN)
73 #define BJACK_HIT BUTTON_PLAY
74 #define BJACK_STAY BUTTON_FF
75 #define BJACK_DOUBLEDOWN BUTTON_REW
76 #define BJACK_SCORES BUTTON_LEFT
77 #define BJACK_RESUME BUTTON_RIGHT
78 #define BJACK_UP BUTTON_SCROLL_UP
79 #define BJACK_DOWN BUTTON_SCROLL_DOWN
80 #define BJACK_RIGHT BUTTON_RIGHT
81 #define BJACK_LEFT BUTTON_LEFT
83 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
84 (CONFIG_KEYPAD == IRIVER_H300_PAD)
85 #define BJACK_START BUTTON_ON
86 #define BJACK_QUIT BUTTON_OFF
87 #define BJACK_MAX (BUTTON_ON|BUTTON_UP)
88 #define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
89 #define BJACK_HIT BUTTON_ON
90 #define BJACK_STAY BUTTON_REC
91 #define BJACK_DOUBLEDOWN BUTTON_SELECT
92 #define BJACK_SCORES BUTTON_SELECT
93 #define BJACK_RESUME BUTTON_MODE
94 #define BJACK_UP BUTTON_UP
95 #define BJACK_DOWN BUTTON_DOWN
96 #define BJACK_RIGHT BUTTON_RIGHT
97 #define BJACK_LEFT BUTTON_LEFT
99 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
100 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
101 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
102 #define BJACK_START BUTTON_SELECT
103 #define BJACK_QUIT BUTTON_MENU
104 #define BJACK_MAX (BUTTON_SELECT|BUTTON_SCROLL_FWD)
105 #define BJACK_MIN (BUTTON_SELECT|BUTTON_SCROLL_BACK)
106 #define BJACK_HIT BUTTON_SELECT
107 #define BJACK_STAY BUTTON_RIGHT
108 #define BJACK_DOUBLEDOWN BUTTON_LEFT
109 #define BJACK_SCORES BUTTON_RIGHT
110 #define BJACK_RESUME BUTTON_PLAY
111 #define BJACK_UP BUTTON_SCROLL_FWD
112 #define BJACK_DOWN BUTTON_SCROLL_BACK
113 #define BJACK_RIGHT BUTTON_RIGHT
114 #define BJACK_LEFT BUTTON_LEFT
116 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
117 #define BJACK_START BUTTON_PLAY
118 #define BJACK_QUIT BUTTON_POWER
119 #define BJACK_MAX (BUTTON_PLAY|BUTTON_UP)
120 #define BJACK_MIN (BUTTON_PLAY|BUTTON_DOWN)
121 #define BJACK_HIT BUTTON_SELECT
122 #define BJACK_STAY BUTTON_REC
123 #define BJACK_DOUBLEDOWN BUTTON_PLAY
124 #define BJACK_SCORES BUTTON_RIGHT
125 #define BJACK_RESUME BUTTON_DOWN
126 #define BJACK_UP BUTTON_UP
127 #define BJACK_DOWN BUTTON_DOWN
128 #define BJACK_RIGHT BUTTON_RIGHT
129 #define BJACK_LEFT BUTTON_LEFT
131 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
132 #define BJACK_START BUTTON_MODE
133 #define BJACK_QUIT BUTTON_PLAY
134 #define BJACK_MAX (BUTTON_EQ|BUTTON_UP)
135 #define BJACK_MIN (BUTTON_EQ|BUTTON_DOWN)
136 #define BJACK_HIT BUTTON_EQ
137 #define BJACK_STAY BUTTON_MODE
138 #define BJACK_DOUBLEDOWN BUTTON_SELECT
139 #define BJACK_SCORES BUTTON_SELECT
140 #define BJACK_RESUME (BUTTON_EQ|BUTTON_MODE)
141 #define BJACK_UP BUTTON_UP
142 #define BJACK_DOWN BUTTON_DOWN
143 #define BJACK_RIGHT BUTTON_RIGHT
144 #define BJACK_LEFT BUTTON_LEFT
146 #elif CONFIG_KEYPAD == GIGABEAT_PAD
147 #define BJACK_START BUTTON_POWER
148 #define BJACK_QUIT BUTTON_A
149 #define BJACK_MAX BUTTON_VOL_UP
150 #define BJACK_MIN BUTTON_VOL_DOWN
151 #define BJACK_HIT BUTTON_VOL_UP
152 #define BJACK_STAY BUTTON_VOL_DOWN
153 #define BJACK_DOUBLEDOWN BUTTON_SELECT
154 #define BJACK_SCORES BUTTON_RIGHT
155 #define BJACK_RESUME BUTTON_MENU
156 #define BJACK_UP BUTTON_UP
157 #define BJACK_DOWN BUTTON_DOWN
158 #define BJACK_RIGHT BUTTON_RIGHT
159 #define BJACK_LEFT BUTTON_LEFT
161 #elif CONFIG_KEYPAD == SANSA_E200_PAD
162 #define BJACK_START BUTTON_SELECT
163 #define BJACK_QUIT BUTTON_POWER
164 #define BJACK_MAX (BUTTON_REC|BUTTON_UP)
165 #define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
166 #define BJACK_HIT BUTTON_SELECT
167 #define BJACK_STAY BUTTON_RIGHT
168 #define BJACK_DOUBLEDOWN BUTTON_LEFT
169 #define BJACK_SCORES BUTTON_UP
170 #define BJACK_RESUME BUTTON_REC
171 #define BJACK_UP BUTTON_SCROLL_FWD
172 #define BJACK_DOWN BUTTON_SCROLL_BACK
173 #define BJACK_RIGHT BUTTON_RIGHT
174 #define BJACK_LEFT BUTTON_LEFT
176 #elif CONFIG_KEYPAD == SANSA_C200_PAD
177 #define BJACK_START BUTTON_SELECT
178 #define BJACK_QUIT BUTTON_POWER
179 #define BJACK_MAX BUTTON_VOL_UP
180 #define BJACK_MIN BUTTON_VOL_DOWN
181 #define BJACK_HIT BUTTON_SELECT
182 #define BJACK_STAY BUTTON_RIGHT
183 #define BJACK_DOUBLEDOWN BUTTON_LEFT
184 #define BJACK_SCORES BUTTON_REC
185 #define BJACK_RESUME BUTTON_DOWN
186 #define BJACK_UP BUTTON_UP
187 #define BJACK_DOWN BUTTON_DOWN
188 #define BJACK_RIGHT BUTTON_RIGHT
189 #define BJACK_LEFT BUTTON_LEFT
191 #elif CONFIG_KEYPAD == ELIO_TPJ1022_PAD
192 #define BJACK_START BUTTON_MAIN
193 #define BJACK_QUIT BUTTON_POWER
194 #define BJACK_MAX (BUTTON_REC|BUTTON_UP)
195 #define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
196 #define BJACK_HIT BUTTON_MAIN
197 #define BJACK_STAY BUTTON_MENU
198 #define BJACK_DOUBLEDOWN BUTTON_DOWN
199 #define BJACK_SCORES BUTTON_UP
200 #define BJACK_RESUME BUTTON_FF
201 #define BJACK_UP BUTTON_UP
202 #define BJACK_DOWN BUTTON_DOWN
203 #define BJACK_RIGHT BUTTON_RIGHT
204 #define BJACK_LEFT BUTTON_LEFT
206 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
207 #define BJACK_START BUTTON_PLAY
208 #define BJACK_QUIT BUTTON_BACK
209 #define BJACK_MAX BUTTON_VOL_UP
210 #define BJACK_MIN BUTTON_VOL_DOWN
211 #define BJACK_HIT BUTTON_VOL_UP
212 #define BJACK_STAY BUTTON_VOL_DOWN
213 #define BJACK_DOUBLEDOWN BUTTON_SELECT
214 #define BJACK_SCORES BUTTON_RIGHT
215 #define BJACK_RESUME BUTTON_MENU
216 #define BJACK_UP BUTTON_UP
217 #define BJACK_DOWN BUTTON_DOWN
218 #define BJACK_RIGHT BUTTON_RIGHT
219 #define BJACK_LEFT BUTTON_LEFT
221 #elif CONFIG_KEYPAD == MROBE100_PAD
223 #define BJACK_START BUTTON_SELECT
224 #define BJACK_QUIT BUTTON_POWER
225 #define BJACK_MAX BUTTON_MENU
226 #define BJACK_MIN BUTTON_DISPLAY
227 #define BJACK_HIT BUTTON_MENU
228 #define BJACK_STAY BUTTON_DISPLAY
229 #define BJACK_DOUBLEDOWN BUTTON_SELECT
230 #define BJACK_SCORES BUTTON_RIGHT
231 #define BJACK_RESUME BUTTON_PLAY
232 #define BJACK_UP BUTTON_UP
233 #define BJACK_DOWN BUTTON_DOWN
234 #define BJACK_RIGHT BUTTON_RIGHT
235 #define BJACK_LEFT BUTTON_LEFT
237 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
239 #define BJACK_START BUTTON_RC_PLAY
240 #define BJACK_QUIT BUTTON_RC_REC
241 #define BJACK_MAX (BUTTON_RC_PLAY|BUTTON_RC_VOL_UP)
242 #define BJACK_MIN (BUTTON_RC_PLAY|BUTTON_RC_VOL_DOWN)
243 #define BJACK_HIT BUTTON_RC_PLAY
244 #define BJACK_STAY BUTTON_RC_FF
245 #define BJACK_DOUBLEDOWN BUTTON_RC_REW
246 #define BJACK_SCORES BUTTON_RC_MENU
247 #define BJACK_RESUME BUTTON_RC_MODE
248 #define BJACK_UP BUTTON_RC_VOL_UP
249 #define BJACK_DOWN BUTTON_RC_VOL_DOWN
250 #define BJACK_RIGHT BUTTON_RC_FF
251 #define BJACK_LEFT BUTTON_RC_REW
253 #elif CONFIG_KEYPAD == COWOND2_PAD
254 #define BJACK_QUIT BUTTON_POWER
255 #define BJACK_DOUBLEDOWN BUTTON_MINUS
256 #define BJACK_SCORES BUTTON_MENU
259 #error No keymap defined!
264 #define BJACK_START BUTTON_CENTER
267 #define BJACK_HIT BUTTON_CENTER
270 #define BJACK_MAX BUTTON_TOPRIGHT
273 #define BJACK_MIN BUTTON_TOPLEFT
276 #define BJACK_RESUME BUTTON_BOTTOMRIGHT
279 #define BJACK_STAY BUTTON_BOTTOMLEFT
282 #define BJACK_UP BUTTON_TOPMIDDLE
285 #define BJACK_DOWN BUTTON_BOTTOMMIDDLE
288 #define BJACK_RIGHT BUTTON_MIDRIGHT
291 #define BJACK_LEFT BUTTON_MIDLEFT
296 #ifdef HAVE_LCD_COLOR
297 #define BG_COLOR LCD_RGBPACK(0,157,0)
298 #define FG_COLOR LCD_WHITE
300 #define BG_COLOR LCD_WHITE
301 #define FG_COLOR LCD_BLACK
304 #define CARD_WIDTH BMPWIDTH_card_back
305 #define CARD_HEIGHT BMPHEIGHT_card_back
307 /* This is the max amount of cards onscreen before condensing */
308 #define MAX_CARDS LCD_WIDTH/(CARD_WIDTH+4)
310 extern const fb_data card_deck
[];
311 extern const fb_data card_back
[];
313 #define NEXT_CARD bj->player_cards[done][bj->num_player_cards[done]]
315 /* global rockbox api */
316 static const struct plugin_api
* rb
;
318 MEM_FUNCTION_WRAPPERS(rb
);
320 /* dealer and player card positions */
321 unsigned int dealer_x
, dealer_y
, player_x
, player_y
;
323 typedef struct card
{
324 unsigned int value
; /* Card's value in Blackjack */
325 unsigned int num
; /* Value on card face 0-12 (0=Ace, 1=2, 11=Q) */
326 unsigned int suit
; /* 0:Spades, 1:Hearts, 2: Clubs; 3: Diamonds */
330 typedef struct game_context
{
331 struct card player_cards
[2][22]; /* 22 Cards means the deal was all aces */
332 struct card dealer_cards
[22]; /* That is the worst-case scenario */
333 unsigned int player_total
;
334 unsigned int dealer_total
;
335 signed int player_money
;
336 unsigned int num_player_cards
[2];
337 unsigned int num_dealer_cards
;
338 unsigned int current_bet
;
339 unsigned int split_status
; /* 0 = split hasn't been asked, *
340 * 1 = split did not occur *
341 * 2 = split occurred *
342 * 3 = split occurred and 1st hand done */
345 bool asked_insurance
;
346 signed short highscores
[NUM_SCORES
];
351 /*****************************************************************************
352 * blackjack_init() initializes blackjack data structures.
353 ******************************************************************************/
354 static void blackjack_init(struct game_context
* bj
) {
355 /* seed the rand generator */
356 rb
->srand(*rb
->current_tick
);
358 /* reset card positions */
360 dealer_y
= LCD_HEIGHT
/4 - CARD_HEIGHT
/2;
362 player_y
= LCD_HEIGHT
- LCD_HEIGHT
/4 - CARD_HEIGHT
/2;
364 /* check for resumed game */
365 if(bj
->resume
) return;
368 bj
->player_total
= 0;
369 bj
->dealer_total
= 0;
370 bj
->num_player_cards
[0] = 2;
371 bj
->num_player_cards
[1] = 0;
372 bj
->num_dealer_cards
= 2;
373 bj
->end_hand
= false;
374 bj
->split_status
= 0;
375 bj
->is_blackjack
= false;
376 bj
->asked_insurance
= false;
379 /*****************************************************************************
380 * blackjack_drawtable() draws the table and some text.
381 ******************************************************************************/
382 static void blackjack_drawtable(struct game_context
* bj
) {
383 unsigned int w
, h
, y_loc
;
387 rb
->lcd_getstringsize("Bet", &w
, &h
);
388 rb
->lcd_putsxy(LCD_WIDTH
- w
, 2*h
+ 1, "Bet");
389 rb
->snprintf(str
, 9, "$%d", bj
->current_bet
);
390 rb
->lcd_getstringsize(str
, &w
, &h
);
391 rb
->lcd_putsxy(LCD_WIDTH
- w
, 3*h
+ 1, str
);
392 y_loc
= LCD_HEIGHT
/2;
394 rb
->lcd_getstringsize("Bet", &w
, &h
);
395 rb
->lcd_putsxy(LCD_WIDTH
- w
, 5*h
/ 2, "Bet");
396 rb
->snprintf(str
, 9, "$%d", bj
->current_bet
);
397 rb
->lcd_getstringsize(str
, &w
, &h
);
398 rb
->lcd_putsxy(LCD_WIDTH
- w
, 7*h
/ 2, str
);
399 rb
->lcd_hline(0, LCD_WIDTH
, LCD_HEIGHT
/2);
400 y_loc
= LCD_HEIGHT
/2 + h
;
403 rb
->lcd_putsxy(0,0, "Dealer");
404 rb
->lcd_getstringsize("Player", &w
, &h
);
405 rb
->lcd_putsxy(0, y_loc
, "Player");
406 rb
->lcd_getstringsize("Total", &w
, &h
);
407 rb
->lcd_putsxy(LCD_WIDTH
- w
, y_loc
, "Total");
408 rb
->lcd_getstringsize("Money", &w
, &h
);
409 rb
->lcd_putsxy(LCD_WIDTH
- w
, 0, "Money");
410 rb
->snprintf(str
, 9, "$%d", bj
->player_money
- bj
->current_bet
);
411 rb
->lcd_getstringsize(str
, &w
, &h
);
412 rb
->lcd_putsxy(LCD_WIDTH
- w
, h
+ 1, str
);
413 rb
->snprintf(str
, 3, "%d", bj
->player_total
);
414 rb
->lcd_getstringsize(str
, &w
, &h
);
415 rb
->lcd_putsxy(LCD_WIDTH
- w
, y_loc
+ h
, str
);
418 /*****************************************************************************
419 * find_value() is passed a card and returns its blackjack value.
420 ******************************************************************************/
421 static unsigned int find_value(unsigned int number
) {
422 unsigned int thisValue
;
424 thisValue
= 11; /* Aces get a value of 11 at first */
425 else if (number
< 10)
426 thisValue
= number
+ 1;
428 thisValue
= 10; /* Anything 10 or higher gets a value of 10 */
433 /*****************************************************************************
434 * draw_card() draws a card to the screen.
435 ******************************************************************************/
436 static void draw_card(struct card temp_card
, bool shown
, unsigned int x
,
439 rb
->lcd_bitmap_part(card_deck
, CARD_WIDTH
*temp_card
.num
,
440 CARD_HEIGHT
*temp_card
.suit
, BMPWIDTH_card_deck
,
441 x
+1, y
+1, CARD_WIDTH
, CARD_HEIGHT
);
443 rb
->lcd_bitmap(card_back
, x
+1, y
+1,CARD_WIDTH
, CARD_HEIGHT
);
445 rb
->lcd_set_foreground(LCD_BLACK
);
450 rb
->lcd_hline(x
+2, x
+CARD_WIDTH
-1, y
);
451 rb
->lcd_hline(x
+2, x
+CARD_WIDTH
-1, y
+CARD_HEIGHT
+1);
452 rb
->lcd_vline(x
, y
+2, y
+CARD_HEIGHT
-3);
453 rb
->lcd_vline(x
+CARD_WIDTH
+1, y
+2, y
+CARD_HEIGHT
-1);
454 rb
->lcd_drawpixel(x
+1, y
+1);
455 rb
->lcd_drawpixel(x
+1, y
+CARD_HEIGHT
);
456 rb
->lcd_drawpixel(x
+CARD_WIDTH
, y
+1);
457 rb
->lcd_drawpixel(x
+CARD_WIDTH
, y
+CARD_HEIGHT
);
459 rb
->lcd_hline(x
+1, x
+CARD_WIDTH
, y
);
460 rb
->lcd_hline(x
+1, x
+CARD_WIDTH
, y
+CARD_HEIGHT
+1);
461 rb
->lcd_vline(x
, y
+1, y
+CARD_HEIGHT
);
462 rb
->lcd_vline(x
+CARD_WIDTH
+1, y
+1, y
+CARD_HEIGHT
);
466 rb
->lcd_set_foreground(FG_COLOR
);
470 /*****************************************************************************
471 * new_card() initializes a new card and gives it values.
472 ******************************************************************************/
473 static struct card
new_card(void) {
474 struct card new_card
;
475 new_card
.suit
= rb
->rand()%4; /* Random number 0-3 */
476 new_card
.num
= rb
->rand()%13; /* Random number 0-12 */
477 new_card
.value
= find_value(new_card
.num
);
478 new_card
.is_soft_ace
= new_card
.num
== 0 ? true : false;
482 /*****************************************************************************
483 * deal_init_card() deals and draws to the screen the player's and dealer's
485 ******************************************************************************/
486 static void deal_init_cards(struct game_context
* bj
) {
487 bj
->dealer_cards
[0] = new_card();
488 bj
->dealer_total
+= bj
->dealer_cards
[0].value
;
490 draw_card(bj
->dealer_cards
[0], false, dealer_x
, dealer_y
);
492 bj
->dealer_cards
[1] = new_card();
493 bj
->dealer_total
+= bj
->dealer_cards
[1].value
;
494 draw_card(bj
->dealer_cards
[1], true, dealer_x
+ CARD_WIDTH
+ 4, dealer_y
);
496 bj
->player_cards
[0][0] = new_card();
497 bj
->player_total
+= bj
->player_cards
[0][0].value
;
498 draw_card(bj
->player_cards
[0][0], true, player_x
, player_y
);
499 player_x
+= CARD_WIDTH
+ 4;
501 bj
->player_cards
[0][1] = new_card();
502 bj
->player_total
+= bj
->player_cards
[0][1].value
;
503 draw_card(bj
->player_cards
[0][1], true, player_x
, player_y
);
504 player_x
+= CARD_WIDTH
+ 4;
507 /*****************************************************************************
508 * redraw_board() redraws all the cards and the board
509 ******************************************************************************/
510 static void redraw_board(struct game_context
* bj
) {
511 unsigned int i
, n
, upper_bound
;
512 rb
->lcd_clear_display();
514 blackjack_drawtable(bj
);
517 upper_bound
= bj
->split_status
> 1 ? 2 : 1;
519 for (i
= 0; i
< bj
->num_dealer_cards
; i
++) {
521 draw_card(bj
->dealer_cards
[0], false, dealer_x
, dealer_y
);
523 /* increment i so the dealer's first card isn't displayed */
525 dealer_x
+= CARD_WIDTH
+ 4;
527 draw_card(bj
->dealer_cards
[i
], true, dealer_x
, dealer_y
);
529 if (bj
->num_dealer_cards
> MAX_CARDS
-1)
532 dealer_x
+= CARD_WIDTH
+ 4;
535 for (n
= 0; n
< upper_bound
; n
++) {
536 for (i
= 0; i
< bj
->num_player_cards
[n
]; i
++) {
537 draw_card(bj
->player_cards
[n
][i
], true, player_x
, player_y
);
538 if (bj
->split_status
>1 || bj
->num_player_cards
[n
]>MAX_CARDS
)
541 player_x
+= CARD_WIDTH
+ 4;
543 if (bj
->split_status
> 1)
544 player_x
= LCD_WIDTH
/2 + 4;
548 /*****************************************************************************
549 * update_total updates the player's total
550 ******************************************************************************/
551 static void update_total(struct game_context
* bj
) {
554 rb
->snprintf(total
, 3, "%d", bj
->player_total
);
555 rb
->lcd_getstringsize(total
, &w
, &h
);
559 rb
->lcd_putsxy(LCD_WIDTH
- w
, LCD_HEIGHT
/2 + h
, total
);
560 rb
->lcd_update_rect(LCD_WIDTH
- w
, LCD_HEIGHT
/2 + h
, w
, h
);
564 /*****************************************************************************
565 * check_for_aces() is passed an array of cards and returns where an ace is
566 * located. Otherwise, returns -1.
567 ******************************************************************************/
568 static signed int check_for_aces(struct card temp_cards
[],
571 for(i
= 0; i
< size
; i
++) {
572 if (temp_cards
[i
].is_soft_ace
== true)
578 /*****************************************************************************
579 * check_totals() compares player and dealer totals.
580 * 0: bust 1: loss, 2: push, 3: win, 4: blackjack, 5: something's not right...
581 ******************************************************************************/
582 static unsigned int check_totals(struct game_context
* bj
)
585 if (bj
->player_total
> 21)
587 else if (bj
->player_total
== 21 && bj
->is_blackjack
)
588 if (bj
->dealer_total
== 21 && bj
->num_dealer_cards
== 2)
592 else if (bj
->player_total
== bj
->dealer_total
)
594 else if (bj
->dealer_total
> 21 && bj
->player_total
< 22)
596 else if (bj
->dealer_total
> bj
->player_total
)
598 else if (bj
->player_total
> bj
->dealer_total
)
606 /*****************************************************************************
607 * finish_dealer() draws cards for the dealer until he has 17 or more.
608 ******************************************************************************/
609 static void finish_dealer(struct game_context
* bj
) {
612 if (bj
->dealer_total
> 16 && bj
->dealer_total
< 22)
615 while (bj
->dealer_total
< 17) {
616 bj
->dealer_cards
[bj
->num_dealer_cards
] = new_card();
617 bj
->dealer_total
+= bj
->dealer_cards
[bj
->num_dealer_cards
].value
;
618 bj
->num_dealer_cards
++;
621 while (bj
->dealer_total
> 21) {
622 temp
= check_for_aces(bj
->dealer_cards
, bj
->num_dealer_cards
);
624 bj
->dealer_cards
[temp
].is_soft_ace
= false;
625 bj
->dealer_total
-= 10;
632 /*****************************************************************************
633 * finish_game() completes the game once player's turn is over.
634 ******************************************************************************/
635 static void finish_game(struct game_context
* bj
) {
636 unsigned int rValue
, w
, h
;
641 } while (bj
->dealer_total
< 17);
644 rValue
= check_totals(bj
);
647 rb
->snprintf(str
, sizeof(str
), " Bust! ");
648 bj
->player_money
-= bj
->current_bet
;
650 else if (rValue
== 1) {
651 rb
->snprintf(str
, sizeof(str
), " Sorry, you lost. ");
652 bj
->player_money
-= bj
->current_bet
;
654 else if (rValue
== 2) {
655 rb
->snprintf(str
, sizeof(str
), " Push ");
657 else if (rValue
== 3) {
658 rb
->snprintf(str
, sizeof(str
), " You won! ");
659 bj
->player_money
+= bj
->current_bet
;
662 rb
->snprintf(str
, sizeof(str
), " Blackjack! ");
663 bj
->player_money
+= bj
->current_bet
* 3 / 2;
665 rb
->lcd_getstringsize(str
, &w
, &h
);
668 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
669 rb
->lcd_fillrect(0, LCD_HEIGHT
/2, LCD_WIDTH
, LCD_HEIGHT
/2);
670 rb
->lcd_set_drawmode(DRMODE_SOLID
);
671 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + h
, str
);
672 rb
->snprintf(str
, 12, "You have %d", bj
->player_total
);
673 rb
->lcd_getstringsize(str
, &w
, &h
);
674 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2, str
);
676 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 - h
/2, str
);
681 /*****************************************************************************
682 * blackjack_recordscore() inserts a high score into the high scores list and
683 * returns the high score position.
684 ******************************************************************************/
685 static unsigned int blackjack_recordscore(struct game_context
* bj
) {
687 unsigned int position
= 0;
688 signed short current
, temp
;
690 /* calculate total score */
691 current
= bj
->player_money
;
692 if(current
<= 10) return 0;
694 /* insert the current score into the high scores */
695 for(i
=0; i
<NUM_SCORES
; i
++) {
696 if(current
>= bj
->highscores
[i
]) {
701 temp
= bj
->highscores
[i
];
702 bj
->highscores
[i
] = current
;
710 /*****************************************************************************
711 * blackjack_loadscores() loads the high scores saved file.
712 ******************************************************************************/
713 static void blackjack_loadscores(struct game_context
* bj
) {
718 /* clear high scores */
719 rb
->memset(bj
->highscores
, 0, sizeof(bj
->highscores
));
721 /* open scores file */
722 fd
= rb
->open(SCORE_FILE
, O_RDONLY
);
725 /* read in high scores */
726 if(rb
->read(fd
, bj
->highscores
, sizeof(bj
->highscores
)) <= 0) {
727 /* scores are bad, reset */
728 rb
->memset(bj
->highscores
, 0, sizeof(bj
->highscores
));
734 /*****************************************************************************
735 * blackjack_savescores() saves the high scores saved file.
736 ******************************************************************************/
737 static void blackjack_savescores(struct game_context
* bj
) {
740 /* write out the high scores to the save file */
741 fd
= rb
->open(SCORE_FILE
, O_WRONLY
|O_CREAT
);
742 rb
->write(fd
, bj
->highscores
, sizeof(bj
->highscores
));
747 /*****************************************************************************
748 * blackjack_loadgame() loads the saved game and returns load success.
749 ******************************************************************************/
750 static bool blackjack_loadgame(struct game_context
* bj
) {
755 fd
= rb
->open(SAVE_FILE
, O_RDONLY
);
756 if(fd
< 0) return loaded
;
758 /* read in saved game */
760 if(rb
->read(fd
, &bj
->player_money
, sizeof(bj
->player_money
)) <= 0) break;
761 if(rb
->read(fd
, &bj
->player_total
, sizeof(bj
->player_total
)) <= 0) break;
762 if(rb
->read(fd
, &bj
->dealer_total
, sizeof(bj
->dealer_total
)) <= 0) break;
763 if(rb
->read(fd
, &bj
->num_player_cards
, sizeof(bj
->num_player_cards
))<=0)
765 if(rb
->read(fd
, &bj
->num_dealer_cards
, sizeof(bj
->num_dealer_cards
))<=0)
767 if(rb
->read(fd
, &bj
->current_bet
, sizeof(bj
->current_bet
)) <= 0) break;
768 if(rb
->read(fd
, &bj
->is_blackjack
, sizeof(bj
->is_blackjack
)) <= 0) break;
769 if(rb
->read(fd
, &bj
->split_status
, sizeof(bj
->split_status
)) <= 0) break;
770 if(rb
->read(fd
, &bj
->asked_insurance
, sizeof(bj
->asked_insurance
)) <= 0)
772 if(rb
->read(fd
, &bj
->end_hand
, sizeof(bj
->end_hand
)) <= 0) break;
773 if(rb
->read(fd
, &bj
->player_cards
, sizeof(bj
->player_cards
)) <= 0) break;
774 if(rb
->read(fd
, &bj
->dealer_cards
, sizeof(bj
->dealer_cards
)) <= 0) break;
782 /* delete saved file */
783 rb
->remove(SAVE_FILE
);
787 /*****************************************************************************
788 * blackjack_savegame() saves the current game state.
789 ******************************************************************************/
790 static void blackjack_savegame(struct game_context
* bj
) {
793 /* write out the game state to the save file */
794 fd
= rb
->open(SAVE_FILE
, O_WRONLY
|O_CREAT
);
795 rb
->write(fd
, &bj
->player_money
, sizeof(bj
->player_money
));
796 rb
->write(fd
, &bj
->player_total
, sizeof(bj
->player_total
));
797 rb
->write(fd
, &bj
->dealer_total
, sizeof(bj
->dealer_total
));
798 rb
->write(fd
, &bj
->num_player_cards
, sizeof(bj
->num_player_cards
));
799 rb
->write(fd
, &bj
->num_dealer_cards
, sizeof(bj
->num_dealer_cards
));
800 rb
->write(fd
, &bj
->current_bet
, sizeof(bj
->current_bet
));
801 rb
->write(fd
, &bj
->is_blackjack
, sizeof(bj
->is_blackjack
));
802 rb
->write(fd
, &bj
->split_status
, sizeof(bj
->split_status
));
803 rb
->write(fd
, &bj
->asked_insurance
, sizeof(bj
->asked_insurance
));
804 rb
->write(fd
, &bj
->end_hand
, sizeof(bj
->end_hand
));
805 rb
->write(fd
, &bj
->player_cards
, sizeof(bj
->player_cards
));
806 rb
->write(fd
, &bj
->dealer_cards
, sizeof(bj
->dealer_cards
));
812 /*****************************************************************************
813 * blackjack_callback() is the default event handler callback which is called
814 * on usb connect and shutdown.
815 ******************************************************************************/
816 static void blackjack_callback(void* param
) {
817 struct game_context
* bj
= (struct game_context
*) param
;
819 rb
->splash(HZ
, "Saving high scores...");
820 blackjack_savescores(bj
);
824 /*****************************************************************************
825 * blackjack_get_yes_no() gets a yes/no answer from the user
826 ******************************************************************************/
827 static unsigned int blackjack_get_yes_no(char message
[20]) {
829 unsigned int w
, h
, b
, choice
= 0;
830 bool breakout
= false;
831 char message_yes
[24], message_no
[24];
833 rb
->strcpy(message_yes
, message
);
834 rb
->strcpy(message_no
, message
);
835 rb
->strcat(message_yes
, " Yes");
836 rb
->strcat(message_no
, " No");
837 rb
->lcd_getstringsize(message_yes
, &w
, &h
);
838 const char *stg
[] = {message_yes
, message_no
};
846 #ifdef HAVE_LCD_COLOR
847 rb
->lcd_fillrect(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
, w
+1, h
+3);
848 rb
->lcd_set_foreground(LCD_BLACK
);
849 rb
->lcd_set_background(LCD_WHITE
);
851 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
852 rb
->lcd_fillrect(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
, w
+1, h
+3);
853 rb
->lcd_set_drawmode(DRMODE_SOLID
);
855 rb
->lcd_drawrect(LCD_WIDTH
/2 - w
/2 - 1, LCD_HEIGHT
/2 + b
- 1, w
+3, h
+4);
858 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
+1, stg
[choice
]);
859 rb
->lcd_update_rect(LCD_WIDTH
/2 - w
/2 - 1, LCD_HEIGHT
/2 + b
-1,
861 button
= rb
->button_get(true);
865 case (BJACK_LEFT
|BUTTON_REPEAT
):
867 case (BJACK_RIGHT
|BUTTON_REPEAT
):
870 case BJACK_START
: breakout
= true;
872 case BJACK_QUIT
: breakout
= true;
879 rb
->lcd_set_foreground(FG_COLOR
);
880 rb
->lcd_set_background(BG_COLOR
);
885 /*****************************************************************************
886 * blackjack_get_amount() gets an amount from the player to be used
887 ******************************************************************************/
888 static signed int blackjack_get_amount(char message
[20], signed int lower_limit
,
889 signed int upper_limit
,
893 bool changed
= false;
897 rb
->lcd_getstringsize("A", &w
, &h
); /* find the size of one character */
899 if (start
> upper_limit
)
900 amount
= upper_limit
;
901 else if (start
< lower_limit
)
902 amount
= lower_limit
;
907 rb
->lcd_set_background(LCD_WHITE
);
908 rb
->lcd_set_foreground(LCD_BLACK
);
912 rb
->lcd_clear_display();
913 rb
->lcd_puts(0, 1, message
);
914 rb
->snprintf(str
, 9, "$%d", amount
);
915 rb
->lcd_puts(0, 2, str
);
916 rb
->lcd_puts(0, 3, "RIGHT: +1");
917 rb
->lcd_puts(0, 4, "LEFT: -1");
918 rb
->lcd_puts(0, 5, "UP: +10");
919 rb
->lcd_puts(0, 6, "DOWN: -10");
922 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
923 rb
->lcd_fillrect(LCD_WIDTH
/2 - 9*w
- 1, LCD_HEIGHT
/2 - 4*h
- 3, 37*w
/ 2,
925 rb
->lcd_set_drawmode(DRMODE_SOLID
);
926 rb
->lcd_drawrect(LCD_WIDTH
/2 - 9*w
- 1, LCD_HEIGHT
/2 - 4*h
- 3, 37*w
/ 2,
928 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 4*h
- 1, message
);
929 rb
->snprintf(str
, 9, "$%d", amount
);
930 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, str
);
931 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
932 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
933 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
934 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, " >>|: +1");
935 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, " |<<: -1");
936 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "SCROLL+: +10");
937 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "SCROLL-: -10");
938 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
939 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, "RIGHT: +1");
940 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, "LEFT: -1");
941 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "SCROLL+: +10");
942 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "SCROLL-: -10");
944 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, "RIGHT: +1");
945 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, "LEFT: -1");
946 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "UP: +10");
947 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "DOWN: -10");
949 rb
->lcd_update_rect(LCD_WIDTH
/2 - 9*w
- 2, LCD_HEIGHT
/2 - 9*h
/2, 37*w
/2 + 1,
954 button
= rb
->button_get(true);
958 case (BJACK_UP
|BUTTON_REPEAT
):
959 if (amount
+ 10 < upper_limit
+ 1) {
965 case (BJACK_DOWN
|BUTTON_REPEAT
):
966 if (amount
- 10 > lower_limit
- 1) {
972 case (BJACK_RIGHT
|BUTTON_REPEAT
):
973 if (amount
+ 1 < upper_limit
+ 1) {
979 case (BJACK_LEFT
|BUTTON_REPEAT
):
980 if (amount
- 1 > lower_limit
- 1) {
986 amount
= upper_limit
;
990 amount
= lower_limit
;
997 rb
->lcd_set_foreground(FG_COLOR
);
998 rb
->lcd_set_background(BG_COLOR
);
1000 rb
->lcd_clear_display();
1005 rb
->snprintf(str
, 9, "$%d", amount
);
1006 #if LCD_HEIGHT <= 64
1007 rb
->lcd_puts(0, 2, str
);
1010 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
1011 rb
->lcd_fillrect(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, 5*w
, h
);
1012 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1013 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, str
);
1014 rb
->lcd_update_rect(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, 5*w
, h
);
1021 /*****************************************************************************
1022 * blackjack_get_bet() gets the player's bet.
1023 ******************************************************************************/
1024 static void blackjack_get_bet(struct game_context
* bj
) {
1025 bj
->current_bet
= blackjack_get_amount("Please enter a bet", 10,
1026 bj
->player_money
, bj
->current_bet
);
1029 /*****************************************************************************
1030 * double_down() returns one final card then finishes the game
1031 ******************************************************************************/
1032 static void double_down(struct game_context
* bj
) {
1033 bj
->current_bet
*= 2;
1034 bj
->player_cards
[0][bj
->num_player_cards
[0]] = new_card();
1035 bj
->player_total
+= bj
->player_cards
[0][bj
->num_player_cards
[0]].value
;
1036 bj
->num_player_cards
[0]++;
1039 /*****************************************************************************
1040 * split() checks if the player wants to split and acts accordingly.
1041 * When bj->split_status is 1, no split occurred. 2 means the player split and 3
1042 * means a split has already occurred and the first hand is done.
1043 ******************************************************************************/
1044 static void split(struct game_context
* bj
) {
1045 if (blackjack_get_yes_no("Split?") == 1)
1046 bj
->split_status
= 1;
1048 bj
->split_status
= 2;
1049 bj
->current_bet
*= 2;
1050 bj
->num_player_cards
[0] = 1;
1051 bj
->num_player_cards
[1] = 1;
1052 bj
->player_cards
[1][0] = bj
->player_cards
[0][1];
1053 bj
->player_total
= bj
->player_cards
[0][0].value
;
1057 /*****************************************************************************
1058 * insurance() see if the player wants to buy insurance and how much.
1059 ******************************************************************************/
1060 static unsigned int insurance(struct game_context
* bj
) {
1061 unsigned int insurance
, max_amount
;
1063 insurance
= blackjack_get_yes_no("Buy Insurance?");
1064 bj
->asked_insurance
= true;
1065 max_amount
= bj
->current_bet
< (unsigned int)bj
->player_money
?
1066 bj
->current_bet
/2 : (unsigned int)bj
->player_money
;
1067 if (insurance
== 1) return 0;
1069 insurance
= blackjack_get_amount("How much?", 0, max_amount
, 0);
1074 /*****************************************************************************
1075 * play_again() checks to see if the player wants to keep playing.
1076 ******************************************************************************/
1077 static unsigned int play_again(void) {
1078 return blackjack_get_yes_no("Play Again?");
1081 /*****************************************************************************
1082 * blackjack_menu() is the initial menu at the start of the game.
1083 ******************************************************************************/
1084 static unsigned int blackjack_menu(struct game_context
* bj
) {
1086 char *title
= "Blackjack";
1088 unsigned int i
, w
, h
;
1089 bool breakout
= false;
1090 bool showscores
= false;
1094 rb
->lcd_set_background(BG_COLOR
);
1095 rb
->lcd_set_foreground(FG_COLOR
);
1097 rb
->lcd_clear_display();
1100 /* welcome screen to display key bindings */
1101 rb
->lcd_getstringsize(title
, &w
, &h
);
1102 rb
->lcd_putsxy((LCD_WIDTH
-w
)/2, 0, title
);
1104 #if CONFIG_KEYPAD == RECORDER_PAD
1105 rb
->lcd_puts(0, 1, "ON: start");
1106 rb
->lcd_puts(0, 2, "OFF: exit");
1107 rb
->lcd_puts(0, 3, "F1: hit");
1108 rb
->lcd_puts(0, 4, "F2: stay");
1109 rb
->lcd_puts(0, 5, "F3: double down");
1110 rb
->lcd_puts(0, 6, "PLAY: save/resume");
1111 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1112 rb
->lcd_puts(0, 7, str
);
1113 #elif CONFIG_KEYPAD == ONDIO_PAD
1114 rb
->lcd_puts(0, 1, "MENU: start");
1115 rb
->lcd_puts(0, 2, "OFF: exit");
1116 rb
->lcd_puts(0, 3, "LEFT: hit");
1117 rb
->lcd_puts(0, 4, "RIGHT: stay");
1118 rb
->lcd_puts(0, 5, "UP: double down");
1119 rb
->lcd_puts(0, 6, "DOWN: save/resume");
1120 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1121 rb
->lcd_puts(0, 7, str
);
1122 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
1123 rb
->lcd_puts(0, 2, "PLAY to start & to hit");
1124 rb
->lcd_puts(0, 3, "STOP to exit");
1125 rb
->lcd_puts(0, 4, "REC to stay");
1126 rb
->lcd_puts(0, 5, "NAVI to double down ");
1127 rb
->lcd_puts(0, 6, " & to view highscores");
1128 rb
->lcd_puts(0, 7, "AB to save/resume");
1129 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1130 rb
->lcd_puts(0, 8, str
);
1131 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
1132 rb
->lcd_puts(0, 2, "PLAY to start & hit");
1133 rb
->lcd_puts(0, 3, "POWER to exit");
1134 rb
->lcd_puts(0, 4, ">>| to stay");
1135 rb
->lcd_puts(0, 5, "|<< to double down");
1136 rb
->lcd_puts(0, 6, "LEFT to view scores");
1137 rb
->lcd_puts(0, 7, "RIGHT to save/resume");
1138 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1139 rb
->lcd_puts(0, 8, str
);
1141 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
1142 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
1143 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
1145 rb
->lcd_puts(0, 2, "SELECT to start & to hit");
1146 rb
->lcd_puts(0, 3, "MENU to exit");
1147 rb
->lcd_puts(0, 4, ">>| to stay & to view highscores");
1148 rb
->lcd_puts(0, 5, "|<< to double down");
1149 rb
->lcd_puts(0, 6, "PLAY to save/resume");
1150 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1151 rb
->lcd_puts(0, 7, str
);
1153 rb
->lcd_puts(0, 2, "SELECT to start & to ");
1154 rb
->lcd_puts(0, 3, " hit");
1155 rb
->lcd_puts(0, 4, "MENU to exit");
1156 rb
->lcd_puts(0, 5, ">>| to stay & to view ");
1157 rb
->lcd_puts(0, 6, " highscores");
1158 rb
->lcd_puts(0, 7, "|<< to double down");
1159 rb
->lcd_puts(0, 8, "PLAY to save/resume");
1160 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1161 rb
->lcd_puts(0, 9, str
);
1163 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
1164 rb
->lcd_puts(0, 2, "PLAY to start to hit");
1165 rb
->lcd_puts(0, 3, "POWER to exit");
1166 rb
->lcd_puts(0, 4, "SELECT to hit");
1167 rb
->lcd_puts(0, 5, "REC to stay");
1168 rb
->lcd_puts(0, 6, "PLAY to double down");
1169 rb
->lcd_puts(0, 7, "RIGHT to view highscores ");
1170 rb
->lcd_puts(0, 8, "DOWN to save/resume");
1171 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1172 rb
->lcd_puts(0, 9, str
);
1173 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
1174 rb
->lcd_puts(0, 2, "AB to start & to");
1175 rb
->lcd_puts(0, 3, " stay");
1176 rb
->lcd_puts(0, 4, "EQ to hit");
1177 rb
->lcd_puts(0, 5, "PLAY to exit");
1178 rb
->lcd_puts(0, 6, "CLICK to double down");
1179 rb
->lcd_puts(0, 7, "& to view highscores");
1180 rb
->lcd_puts(0, 8, "AB+EQ to save/resume");
1181 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1182 rb
->lcd_puts(0, 9, str
);
1183 #elif CONFIG_KEYPAD == GIGABEAT_PAD
1184 rb
->lcd_puts(0, 2, "POWER to start");
1185 rb
->lcd_puts(0, 3, "A to exit");
1186 rb
->lcd_puts(0, 4, "VOL+ to hit");
1187 rb
->lcd_puts(0, 5, "VOL- to stay");
1188 rb
->lcd_puts(0, 6, "CENTER to double down");
1189 rb
->lcd_puts(0, 6, "RIGHT to view highscores ");
1190 rb
->lcd_puts(0, 8, "MENU to save/resume");
1191 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1192 rb
->lcd_puts(0, 9, str
);
1193 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
1194 rb
->lcd_puts(0, 2, "SELECT to start & to hit");
1195 rb
->lcd_puts(0, 3, "POWER to exit");
1196 rb
->lcd_puts(0, 4, "RIGHT to stay");
1197 rb
->lcd_puts(0, 5, "LEFT to double down");
1198 rb
->lcd_puts(0, 6, "REC to save/resume");
1199 rb
->lcd_puts(0, 7, "UP to view scores");
1200 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1201 rb
->lcd_puts(0, 8, str
);
1202 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
1203 rb
->lcd_puts(0, 2, "SELECT to start & to hit");
1204 rb
->lcd_puts(0, 3, "POWER to exit");
1205 rb
->lcd_puts(0, 4, "RIGHT to stay");
1206 rb
->lcd_puts(0, 5, "LEFT to double down");
1207 rb
->lcd_puts(0, 6, "DOWN to save/resume");
1208 rb
->lcd_puts(0, 7, "REC to view scores");
1209 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1210 rb
->lcd_puts(0, 9, str
);
1211 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
1212 rb
->lcd_puts(0, 2, "PLAY to start & to");
1213 rb
->lcd_puts(0, 3, " hit");
1214 rb
->lcd_puts(0, 4, "REC to exit");
1215 rb
->lcd_puts(0, 5, "FF to stay");
1216 rb
->lcd_puts(0, 6, "REW to double down");
1217 rb
->lcd_puts(0, 7, "MODE to save/resume");
1218 rb
->lcd_puts(0, 8, "MENU to view scores");
1219 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1220 rb
->lcd_puts(0, 10, str
);
1221 #elif CONFIG_KEYPAD == COWOND2_PAD
1222 rb
->lcd_puts(0, 6, "POWER to exit");
1223 rb
->lcd_puts(0, 7, "MINUS to double down");
1224 rb
->lcd_puts(0, 8, "MENU to view scores");
1225 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1226 rb
->lcd_puts(0, 10, str
);
1229 #ifdef HAVE_TOUCHPAD
1230 rb
->lcd_puts(0, 2, "LCD CENTRE to start & to hit");
1231 rb
->lcd_puts(0, 3, "LCD BOTTOMLEFT to stay");
1232 rb
->lcd_puts(0, 4, "LCD BOTTOMRIGHT to save/resume");
1235 rb
->snprintf(str
, 12, "%s", "High Scores");
1236 rb
->lcd_getstringsize(str
, &w
, &h
);
1237 rb
->lcd_putsxy((LCD_WIDTH
-w
)/2, 0, str
);
1239 /* print high scores */
1240 for(i
=0; i
<NUM_SCORES
; i
++) {
1241 rb
->snprintf(str
, 14, "#%02d: $%d", i
+1, bj
->highscores
[i
]);
1242 rb
->lcd_puts(0, i
+1, str
);
1248 /* handle menu button presses */
1249 button
= rb
->button_get(true);
1252 case BJACK_START
: /* start playing */
1256 case BJACK_QUIT
: /* quit program */
1263 case BJACK_RESUME
:/* resume game */
1264 if(!blackjack_loadgame(bj
)) {
1265 rb
->splash(HZ
*2, "Nothing to resume");
1267 rb
->splash(HZ
*2, "Loading...");
1272 case BJACK_SCORES
:/* toggle high scores */
1273 showscores
= !showscores
;
1277 if(rb
->default_event_handler_ex(button
, blackjack_callback
,
1278 (void*) bj
) == SYS_USB_CONNECTED
)
1289 /*****************************************************************************
1290 * blackjack() is the main game subroutine, it returns the final game status.
1291 ******************************************************************************/
1292 static int blackjack(struct game_context
* bj
) {
1294 unsigned int w
, h
, temp_var
, done
= 0, todo
= 1;
1296 bool breakout
= false;
1297 bool dbl_down
= false;
1299 /* don't resume by default */
1302 /********************
1304 ********************/
1305 temp_var
= blackjack_menu(bj
);
1306 if (temp_var
== BJ_QUIT
|| temp_var
== BJ_USB
)
1310 /********************
1312 ********************/
1316 /********************
1318 ********************/
1320 /* check for resumed game */
1324 if (bj
->split_status
== 2) {
1326 player_x
= bj
->num_player_cards
[0] * 10 + 4;
1328 else if (bj
->split_status
== 3) {
1329 player_x
= bj
->num_player_cards
[1] * 10 + LCD_WIDTH
/2 + 4;
1336 bj
->player_money
= 1000;
1337 blackjack_get_bet(bj
);
1338 if (bj
->current_bet
== 0)
1340 rb
->lcd_clear_display();
1341 deal_init_cards(bj
);
1342 blackjack_drawtable(bj
);
1350 if(bj
->player_total
== 21 && bj
->num_player_cards
[0] == 2) {
1351 bj
->is_blackjack
= true;
1352 bj
->end_hand
= true;
1355 else if(bj
->dealer_cards
[1].is_soft_ace
&& !breakout
&&
1356 !bj
->asked_insurance
) {
1357 temp_var
= insurance(bj
);
1358 if (bj
->dealer_total
== 21) {
1359 rb
->splash(HZ
, "Dealer has blackjack");
1360 bj
->player_money
+= temp_var
;
1361 bj
->end_hand
= true;
1367 rb
->splash(HZ
, "Dealer does not have blackjack");
1368 bj
->player_money
-= temp_var
;
1374 if(bj
->split_status
== 0 &&
1375 bj
->player_cards
[0][0].num
== bj
->player_cards
[0][1].num
) {
1378 rb
->lcd_update_rect(0, LCD_HEIGHT
/2, LCD_WIDTH
, LCD_HEIGHT
/2);
1379 if (bj
->split_status
== 2) {
1381 player_x
= bj
->num_player_cards
[0] * 10 + 4;
1385 while(done
< todo
) {
1386 button
= rb
->button_get(true);
1390 NEXT_CARD
= new_card();
1391 bj
->player_total
+= NEXT_CARD
.value
;
1392 draw_card(NEXT_CARD
, true, player_x
, player_y
);
1393 bj
->num_player_cards
[done
]++;
1394 if (bj
->num_player_cards
[done
] == MAX_CARDS
+ 1) {
1396 rb
->lcd_update_rect(0, LCD_HEIGHT
/2, LCD_WIDTH
,
1399 else if (bj
->num_player_cards
[done
]>MAX_CARDS
|| todo
> 1) {
1400 rb
->lcd_update_rect(player_x
, player_y
, CARD_WIDTH
+2,
1405 rb
->lcd_update_rect(player_x
, player_y
, CARD_WIDTH
+2,
1407 player_x
+= CARD_WIDTH
+ 4;
1413 bj
->end_hand
= true;
1415 case BJACK_DOUBLEDOWN
:
1416 if ((signed int)bj
->current_bet
* 2 < bj
->player_money
+ 1 &&
1417 bj
->num_player_cards
[0]==2 && todo
==1) {
1420 if (bj
->player_total
< 22) {
1421 bj
->end_hand
= true;
1425 else if((signed int)bj
->current_bet
* 2 > bj
->player_money
) {
1426 rb
->splash(HZ
, "Not enough money to double down.");
1431 case BJACK_RESUME
: /* save and end game */
1432 rb
->splash(HZ
, "Saving game...");
1433 blackjack_savegame(bj
);
1434 /* fall through to BJACK_QUIT */
1440 while (bj
->player_total
> 21 && !bj
->end_hand
) {
1441 temp
= check_for_aces(bj
->player_cards
[done
],
1442 bj
->num_player_cards
[done
]);
1444 bj
->player_cards
[done
][temp
].is_soft_ace
= false;
1445 bj
->player_total
-= 10;
1448 bj
->end_hand
= true;
1453 bj
->end_hand
= true;
1460 temp
= bj
->player_total
;
1461 bj
->player_total
= temp_var
;
1464 rb
->lcd_getstringsize(" Split 1 ", &w
, &h
);
1465 rb
->lcd_putsxy(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1467 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1469 bj
->current_bet
/= 2;
1470 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1473 bj
->player_total
= temp_var
;
1475 rb
->lcd_getstringsize(" Split 2 ", &w
, &h
);
1476 rb
->lcd_putsxy(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1478 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1483 bj
->end_hand
= false;
1484 bj
->split_status
= 3;
1485 temp_var
= bj
->player_total
;
1486 bj
->player_total
= bj
->player_cards
[1][0].value
;
1498 if (bj
->player_money
< 10) {
1503 if (bj
->end_hand
) { /* If hand is over */
1504 if (play_again() != 0) /* User wants to quit */
1506 else { /* User keeps playing */
1510 bj
->current_bet
/= 2;
1516 blackjack_get_bet(bj
);
1517 if (bj
->current_bet
== 0)
1519 deal_init_cards(bj
);
1520 blackjack_drawtable(bj
);
1529 /*****************************************************************************
1530 * plugin entry point.
1531 ******************************************************************************/
1532 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
1534 struct game_context bj
;
1536 unsigned int position
;
1543 rb
->lcd_set_backdrop(NULL
);
1546 /* load high scores */
1547 blackjack_loadscores(&bj
);
1549 rb
->lcd_setfont(FONT_SYSFIXED
);
1552 switch(blackjack(&bj
)){
1554 rb
->splash(HZ
, "Not enough money to continue");
1555 /* fall through to BJ_END */
1559 if((position
= blackjack_recordscore(&bj
))) {
1560 rb
->snprintf(str
, 19, "New high score #%d!", position
);
1561 rb
->splash(HZ
*2, str
);
1567 rb
->lcd_setfont(FONT_UI
);
1568 return PLUGIN_USB_CONNECTED
;
1572 rb
->splash(HZ
, "Saving high scores...");
1573 blackjack_savescores(&bj
);
1583 rb
->lcd_setfont(FONT_UI
);