1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Tom Ross
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include "pluginbitmaps/card_deck.h"
24 #include "pluginbitmaps/card_back.h"
29 #define SCORE_FILE PLUGIN_GAMES_DIR "/blackjack.score"
30 #define SAVE_FILE PLUGIN_GAMES_DIR "/blackjack.save"
32 #define NUM_SCORES LCD_HEIGHT/8-2
34 /* final game return status */
40 #if CONFIG_KEYPAD == RECORDER_PAD
41 #define BJACK_START BUTTON_ON
42 #define BJACK_QUIT BUTTON_OFF
43 #define BJACK_MAX (BUTTON_ON|BUTTON_UP)
44 #define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
45 #define BJACK_HIT BUTTON_F1
46 #define BJACK_STAY BUTTON_F2
47 #define BJACK_DOUBLEDOWN BUTTON_F3
48 #define BJACK_SCORES BUTTON_RIGHT
49 #define BJACK_RESUME BUTTON_PLAY
50 #define BJACK_UP BUTTON_UP
51 #define BJACK_DOWN BUTTON_DOWN
52 #define BJACK_RIGHT BUTTON_RIGHT
53 #define BJACK_LEFT BUTTON_LEFT
55 #elif CONFIG_KEYPAD == ONDIO_PAD
56 #define BJACK_START BUTTON_MENU
57 #define BJACK_QUIT BUTTON_OFF
58 #define BJACK_MAX (BUTTON_MENU|BUTTON_UP)
59 #define BJACK_MIN (BUTTON_MENU|BUTTON_DOWN)
60 #define BJACK_HIT BUTTON_LEFT
61 #define BJACK_STAY BUTTON_RIGHT
62 #define BJACK_DOUBLEDOWN BUTTON_UP
63 #define BJACK_SCORES BUTTON_UP
64 #define BJACK_RESUME BUTTON_DOWN
65 #define BJACK_UP BUTTON_UP
66 #define BJACK_DOWN BUTTON_DOWN
67 #define BJACK_RIGHT BUTTON_RIGHT
68 #define BJACK_LEFT BUTTON_LEFT
70 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
71 #define BJACK_START BUTTON_PLAY
72 #define BJACK_QUIT BUTTON_POWER
73 #define BJACK_MAX (BUTTON_PLAY|BUTTON_SCROLL_UP)
74 #define BJACK_MIN (BUTTON_PLAY|BUTTON_SCROLL_DOWN)
75 #define BJACK_HIT BUTTON_PLAY
76 #define BJACK_STAY BUTTON_FF
77 #define BJACK_DOUBLEDOWN BUTTON_REW
78 #define BJACK_SCORES BUTTON_LEFT
79 #define BJACK_RESUME BUTTON_RIGHT
80 #define BJACK_UP BUTTON_SCROLL_UP
81 #define BJACK_DOWN BUTTON_SCROLL_DOWN
82 #define BJACK_RIGHT BUTTON_RIGHT
83 #define BJACK_LEFT BUTTON_LEFT
85 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
86 (CONFIG_KEYPAD == IRIVER_H300_PAD)
87 #define BJACK_START BUTTON_ON
88 #define BJACK_QUIT BUTTON_OFF
89 #define BJACK_MAX (BUTTON_ON|BUTTON_UP)
90 #define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
91 #define BJACK_HIT BUTTON_ON
92 #define BJACK_STAY BUTTON_REC
93 #define BJACK_DOUBLEDOWN BUTTON_SELECT
94 #define BJACK_SCORES BUTTON_SELECT
95 #define BJACK_RESUME BUTTON_MODE
96 #define BJACK_UP BUTTON_UP
97 #define BJACK_DOWN BUTTON_DOWN
98 #define BJACK_RIGHT BUTTON_RIGHT
99 #define BJACK_LEFT BUTTON_LEFT
101 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
102 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
103 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
104 #define BJACK_START BUTTON_SELECT
105 #define BJACK_QUIT BUTTON_MENU
106 #define BJACK_MAX (BUTTON_SELECT|BUTTON_SCROLL_FWD)
107 #define BJACK_MIN (BUTTON_SELECT|BUTTON_SCROLL_BACK)
108 #define BJACK_HIT BUTTON_SELECT
109 #define BJACK_STAY BUTTON_RIGHT
110 #define BJACK_DOUBLEDOWN BUTTON_LEFT
111 #define BJACK_SCORES BUTTON_RIGHT
112 #define BJACK_RESUME BUTTON_PLAY
113 #define BJACK_UP BUTTON_SCROLL_FWD
114 #define BJACK_DOWN BUTTON_SCROLL_BACK
115 #define BJACK_RIGHT BUTTON_RIGHT
116 #define BJACK_LEFT BUTTON_LEFT
118 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
119 #define BJACK_START BUTTON_PLAY
120 #define BJACK_QUIT BUTTON_POWER
121 #define BJACK_MAX (BUTTON_PLAY|BUTTON_UP)
122 #define BJACK_MIN (BUTTON_PLAY|BUTTON_DOWN)
123 #define BJACK_HIT BUTTON_SELECT
124 #define BJACK_STAY BUTTON_REC
125 #define BJACK_DOUBLEDOWN BUTTON_PLAY
126 #define BJACK_SCORES BUTTON_RIGHT
127 #define BJACK_RESUME BUTTON_DOWN
128 #define BJACK_UP BUTTON_UP
129 #define BJACK_DOWN BUTTON_DOWN
130 #define BJACK_RIGHT BUTTON_RIGHT
131 #define BJACK_LEFT BUTTON_LEFT
133 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
134 #define BJACK_START BUTTON_MODE
135 #define BJACK_QUIT BUTTON_PLAY
136 #define BJACK_MAX (BUTTON_EQ|BUTTON_UP)
137 #define BJACK_MIN (BUTTON_EQ|BUTTON_DOWN)
138 #define BJACK_HIT BUTTON_EQ
139 #define BJACK_STAY BUTTON_MODE
140 #define BJACK_DOUBLEDOWN BUTTON_SELECT
141 #define BJACK_SCORES BUTTON_SELECT
142 #define BJACK_RESUME (BUTTON_EQ|BUTTON_MODE)
143 #define BJACK_UP BUTTON_UP
144 #define BJACK_DOWN BUTTON_DOWN
145 #define BJACK_RIGHT BUTTON_RIGHT
146 #define BJACK_LEFT BUTTON_LEFT
148 #elif CONFIG_KEYPAD == GIGABEAT_PAD
149 #define BJACK_START BUTTON_A
150 #define BJACK_QUIT BUTTON_POWER
151 #define BJACK_MAX BUTTON_VOL_UP
152 #define BJACK_MIN BUTTON_VOL_DOWN
153 #define BJACK_HIT BUTTON_VOL_UP
154 #define BJACK_STAY BUTTON_VOL_DOWN
155 #define BJACK_DOUBLEDOWN BUTTON_SELECT
156 #define BJACK_SCORES BUTTON_RIGHT
157 #define BJACK_RESUME BUTTON_MENU
158 #define BJACK_UP BUTTON_UP
159 #define BJACK_DOWN BUTTON_DOWN
160 #define BJACK_RIGHT BUTTON_RIGHT
161 #define BJACK_LEFT BUTTON_LEFT
163 #elif CONFIG_KEYPAD == SANSA_E200_PAD
164 #define BJACK_START BUTTON_SELECT
165 #define BJACK_QUIT BUTTON_POWER
166 #define BJACK_MAX (BUTTON_REC|BUTTON_UP)
167 #define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
168 #define BJACK_HIT BUTTON_SELECT
169 #define BJACK_STAY BUTTON_RIGHT
170 #define BJACK_DOUBLEDOWN BUTTON_LEFT
171 #define BJACK_SCORES BUTTON_UP
172 #define BJACK_RESUME BUTTON_DOWN
173 #define BJACK_UP BUTTON_SCROLL_FWD
174 #define BJACK_DOWN BUTTON_SCROLL_BACK
175 #define BJACK_RIGHT BUTTON_RIGHT
176 #define BJACK_LEFT BUTTON_LEFT
178 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
179 #define BJACK_START BUTTON_SELECT
180 #define BJACK_QUIT (BUTTON_HOME|BUTTON_REPEAT)
181 #define BJACK_MAX (BUTTON_SELECT|BUTTON_UP)
182 #define BJACK_MIN (BUTTON_SELECT|BUTTON_DOWN)
183 #define BJACK_HIT (BUTTON_SELECT|BUTTON_REL)
184 #define BJACK_STAY BUTTON_RIGHT
185 #define BJACK_DOUBLEDOWN BUTTON_LEFT
186 #define BJACK_SCORES BUTTON_DOWN
187 #define BJACK_RESUME BUTTON_UP
188 #define BJACK_UP BUTTON_SCROLL_FWD
189 #define BJACK_DOWN BUTTON_SCROLL_BACK
190 #define BJACK_RIGHT BUTTON_RIGHT
191 #define BJACK_LEFT BUTTON_LEFT
194 #elif CONFIG_KEYPAD == SANSA_C200_PAD
195 #define BJACK_START BUTTON_SELECT
196 #define BJACK_QUIT BUTTON_POWER
197 #define BJACK_MAX BUTTON_VOL_UP
198 #define BJACK_MIN BUTTON_VOL_DOWN
199 #define BJACK_HIT BUTTON_SELECT
200 #define BJACK_STAY BUTTON_RIGHT
201 #define BJACK_DOUBLEDOWN BUTTON_LEFT
202 #define BJACK_SCORES BUTTON_REC
203 #define BJACK_RESUME BUTTON_DOWN
204 #define BJACK_UP BUTTON_UP
205 #define BJACK_DOWN BUTTON_DOWN
206 #define BJACK_RIGHT BUTTON_RIGHT
207 #define BJACK_LEFT BUTTON_LEFT
209 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
210 #define BJACK_START BUTTON_SELECT
211 #define BJACK_QUIT BUTTON_POWER
212 #define BJACK_MAX BUTTON_VOL_UP
213 #define BJACK_MIN BUTTON_VOL_DOWN
214 #define BJACK_HIT BUTTON_SELECT
215 #define BJACK_STAY BUTTON_RIGHT
216 #define BJACK_DOUBLEDOWN BUTTON_LEFT
217 #define BJACK_SCORES BUTTON_HOME
218 #define BJACK_RESUME BUTTON_DOWN
219 #define BJACK_UP BUTTON_UP
220 #define BJACK_DOWN BUTTON_DOWN
221 #define BJACK_RIGHT BUTTON_RIGHT
222 #define BJACK_LEFT BUTTON_LEFT
224 #elif CONFIG_KEYPAD == SANSA_M200_PAD
225 #define BJACK_START (BUTTON_SELECT | BUTTON_REL)
226 #define BJACK_QUIT BUTTON_POWER
227 #define BJACK_MAX BUTTON_VOL_UP
228 #define BJACK_MIN BUTTON_VOL_DOWN
229 #define BJACK_HIT (BUTTON_SELECT | BUTTON_REL)
230 #define BJACK_STAY BUTTON_RIGHT
231 #define BJACK_DOUBLEDOWN BUTTON_LEFT
232 #define BJACK_SCORES (BUTTON_SELECT | BUTTON_UP)
233 #define BJACK_RESUME BUTTON_DOWN
234 #define BJACK_UP BUTTON_UP
235 #define BJACK_DOWN BUTTON_DOWN
236 #define BJACK_RIGHT BUTTON_RIGHT
237 #define BJACK_LEFT BUTTON_LEFT
239 #elif CONFIG_KEYPAD == ELIO_TPJ1022_PAD
240 #define BJACK_START BUTTON_MAIN
241 #define BJACK_QUIT BUTTON_POWER
242 #define BJACK_MAX (BUTTON_REC|BUTTON_UP)
243 #define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
244 #define BJACK_HIT BUTTON_MAIN
245 #define BJACK_STAY BUTTON_MENU
246 #define BJACK_DOUBLEDOWN BUTTON_DOWN
247 #define BJACK_SCORES BUTTON_UP
248 #define BJACK_RESUME BUTTON_FF
249 #define BJACK_UP BUTTON_UP
250 #define BJACK_DOWN BUTTON_DOWN
251 #define BJACK_RIGHT BUTTON_RIGHT
252 #define BJACK_LEFT BUTTON_LEFT
254 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
255 #define BJACK_START BUTTON_PLAY
256 #define BJACK_QUIT BUTTON_BACK
257 #define BJACK_MAX BUTTON_VOL_UP
258 #define BJACK_MIN BUTTON_VOL_DOWN
259 #define BJACK_HIT BUTTON_VOL_UP
260 #define BJACK_STAY BUTTON_VOL_DOWN
261 #define BJACK_DOUBLEDOWN BUTTON_SELECT
262 #define BJACK_SCORES BUTTON_RIGHT
263 #define BJACK_RESUME BUTTON_MENU
264 #define BJACK_UP BUTTON_UP
265 #define BJACK_DOWN BUTTON_DOWN
266 #define BJACK_RIGHT BUTTON_RIGHT
267 #define BJACK_LEFT BUTTON_LEFT
269 #elif CONFIG_KEYPAD == MROBE100_PAD
271 #define BJACK_START BUTTON_SELECT
272 #define BJACK_QUIT BUTTON_POWER
273 #define BJACK_MAX BUTTON_MENU
274 #define BJACK_MIN BUTTON_DISPLAY
275 #define BJACK_HIT BUTTON_MENU
276 #define BJACK_STAY BUTTON_DISPLAY
277 #define BJACK_DOUBLEDOWN BUTTON_DOWN
278 #define BJACK_SCORES BUTTON_RIGHT
279 #define BJACK_RESUME BUTTON_PLAY
280 #define BJACK_UP BUTTON_UP
281 #define BJACK_DOWN BUTTON_DOWN
282 #define BJACK_RIGHT BUTTON_RIGHT
283 #define BJACK_LEFT BUTTON_LEFT
285 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
287 #define BJACK_START BUTTON_RC_PLAY
288 #define BJACK_QUIT BUTTON_RC_REC
289 #define BJACK_MAX (BUTTON_RC_PLAY|BUTTON_RC_VOL_UP)
290 #define BJACK_MIN (BUTTON_RC_PLAY|BUTTON_RC_VOL_DOWN)
291 #define BJACK_HIT BUTTON_RC_PLAY
292 #define BJACK_STAY BUTTON_RC_FF
293 #define BJACK_DOUBLEDOWN BUTTON_RC_REW
294 #define BJACK_SCORES BUTTON_RC_MENU
295 #define BJACK_RESUME BUTTON_RC_MODE
296 #define BJACK_UP BUTTON_RC_VOL_UP
297 #define BJACK_DOWN BUTTON_RC_VOL_DOWN
298 #define BJACK_RIGHT BUTTON_RC_FF
299 #define BJACK_LEFT BUTTON_RC_REW
301 #elif CONFIG_KEYPAD == COWOND2_PAD
302 #define BJACK_QUIT BUTTON_POWER
303 #define BJACK_DOUBLEDOWN BUTTON_MINUS
304 #define BJACK_SCORES BUTTON_MENU
306 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
307 #define BJACK_START BUTTON_SELECT
308 #define BJACK_QUIT BUTTON_BACK
309 #define BJACK_MAX (BUTTON_CUSTOM|BUTTON_UP)
310 #define BJACK_MIN (BUTTON_CUSTOM|BUTTON_DOWN)
311 #define BJACK_HIT BUTTON_UP
312 #define BJACK_STAY BUTTON_DOWN
313 #define BJACK_DOUBLEDOWN BUTTON_CUSTOM
314 #define BJACK_SCORES BUTTON_RIGHT
315 #define BJACK_RESUME BUTTON_MENU
316 #define BJACK_UP BUTTON_UP
317 #define BJACK_DOWN BUTTON_DOWN
318 #define BJACK_RIGHT BUTTON_RIGHT
319 #define BJACK_LEFT BUTTON_LEFT
321 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
322 #define BJACK_START BUTTON_MENU
323 #define BJACK_QUIT BUTTON_POWER
324 #define BJACK_MAX BUTTON_VOL_UP
325 #define BJACK_MIN BUTTON_VOL_DOWN
326 #define BJACK_HIT BUTTON_VOL_UP
327 #define BJACK_STAY BUTTON_VOL_DOWN
328 #define BJACK_DOUBLEDOWN BUTTON_SELECT
329 #define BJACK_SCORES BUTTON_RIGHT
330 #define BJACK_RESUME BUTTON_VIEW
331 #define BJACK_UP BUTTON_UP
332 #define BJACK_DOWN BUTTON_DOWN
333 #define BJACK_RIGHT BUTTON_RIGHT
334 #define BJACK_LEFT BUTTON_LEFT
336 #elif CONFIG_KEYPAD == ONDAVX747_PAD
337 #define BJACK_QUIT BUTTON_POWER
338 #define BJACK_DOUBLEDOWN BUTTON_VOL_DOWN
339 #define BJACK_SCORES BUTTON_MENU
341 #elif CONFIG_KEYPAD == MROBE500_PAD
342 #define BJACK_QUIT BUTTON_POWER
345 #error No keymap defined!
348 #ifdef HAVE_TOUCHSCREEN
349 #ifndef BJACK_DOUBLEDOWN
350 #define BJACK_DOUBLEDOWN BUTTON_MIDLEFT
353 #define BJACK_SCORES BUTTON_MIDRIGHT
356 #define BJACK_START BUTTON_CENTER
359 #define BJACK_HIT BUTTON_CENTER
362 #define BJACK_MAX BUTTON_TOPRIGHT
365 #define BJACK_MIN BUTTON_TOPLEFT
368 #define BJACK_RESUME BUTTON_BOTTOMRIGHT
371 #define BJACK_STAY BUTTON_BOTTOMLEFT
374 #define BJACK_UP BUTTON_TOPMIDDLE
377 #define BJACK_DOWN BUTTON_BOTTOMMIDDLE
380 #define BJACK_RIGHT BUTTON_MIDRIGHT
383 #define BJACK_LEFT BUTTON_MIDLEFT
388 #ifdef HAVE_LCD_COLOR
389 #define BG_COLOR LCD_RGBPACK(0,157,0)
390 #define FG_COLOR LCD_WHITE
392 #define BG_COLOR LCD_WHITE
393 #define FG_COLOR LCD_BLACK
396 #define CARD_WIDTH BMPWIDTH_card_back
397 #define CARD_HEIGHT BMPHEIGHT_card_back
399 /* This is the max amount of cards onscreen before condensing */
400 #define MAX_CARDS LCD_WIDTH/(CARD_WIDTH+4)
402 extern const fb_data card_deck
[];
403 extern const fb_data card_back
[];
405 #define NEXT_CARD bj->player_cards[done][bj->num_player_cards[done]]
407 /* dealer and player card positions */
408 unsigned int dealer_x
, dealer_y
, player_x
, player_y
;
410 typedef struct card
{
411 unsigned int value
; /* Card's value in Blackjack */
412 unsigned int num
; /* Value on card face 0-12 (0=Ace, 1=2, 11=Q) */
413 unsigned int suit
; /* 0:Spades, 1:Hearts, 2: Clubs; 3: Diamonds */
417 typedef struct game_context
{
418 struct card player_cards
[2][22]; /* 22 Cards means the deal was all aces */
419 struct card dealer_cards
[22]; /* That is the worst-case scenario */
420 unsigned int player_total
;
421 unsigned int dealer_total
;
422 signed int player_money
;
423 unsigned int num_player_cards
[2];
424 unsigned int num_dealer_cards
;
425 unsigned int current_bet
;
426 unsigned int split_status
; /* 0 = split hasn't been asked, *
427 * 1 = split did not occur *
428 * 2 = split occurred *
429 * 3 = split occurred and 1st hand done */
432 bool asked_insurance
;
433 signed short highscores
[NUM_SCORES
];
438 /*****************************************************************************
439 * blackjack_init() initializes blackjack data structures.
440 ******************************************************************************/
441 static void blackjack_init(struct game_context
* bj
) {
442 /* seed the rand generator */
443 rb
->srand(*rb
->current_tick
);
445 /* reset card positions */
447 dealer_y
= LCD_HEIGHT
/4 - CARD_HEIGHT
/2;
449 player_y
= LCD_HEIGHT
- LCD_HEIGHT
/4 - CARD_HEIGHT
/2;
451 /* check for resumed game */
452 if(bj
->resume
) return;
455 bj
->player_total
= 0;
456 bj
->dealer_total
= 0;
457 bj
->num_player_cards
[0] = 2;
458 bj
->num_player_cards
[1] = 0;
459 bj
->num_dealer_cards
= 2;
460 bj
->end_hand
= false;
461 bj
->split_status
= 0;
462 bj
->is_blackjack
= false;
463 bj
->asked_insurance
= false;
466 /*****************************************************************************
467 * blackjack_drawtable() draws the table and some text.
468 ******************************************************************************/
469 static void blackjack_drawtable(struct game_context
* bj
) {
470 unsigned int w
, h
, y_loc
;
474 rb
->lcd_getstringsize("Bet", &w
, &h
);
475 rb
->lcd_putsxy(LCD_WIDTH
- w
, 2*h
+ 1, "Bet");
476 rb
->snprintf(str
, 9, "$%d", bj
->current_bet
);
477 rb
->lcd_getstringsize(str
, &w
, &h
);
478 rb
->lcd_putsxy(LCD_WIDTH
- w
, 3*h
+ 1, str
);
479 y_loc
= LCD_HEIGHT
/2;
481 rb
->lcd_getstringsize("Bet", &w
, &h
);
482 rb
->lcd_putsxy(LCD_WIDTH
- w
, 5*h
/ 2, "Bet");
483 rb
->snprintf(str
, 9, "$%d", bj
->current_bet
);
484 rb
->lcd_getstringsize(str
, &w
, &h
);
485 rb
->lcd_putsxy(LCD_WIDTH
- w
, 7*h
/ 2, str
);
486 rb
->lcd_hline(0, LCD_WIDTH
, LCD_HEIGHT
/2);
487 y_loc
= LCD_HEIGHT
/2 + h
;
490 rb
->lcd_putsxy(0,0, "Dealer");
491 rb
->lcd_getstringsize("Player", &w
, &h
);
492 rb
->lcd_putsxy(0, y_loc
, "Player");
493 rb
->lcd_getstringsize("Total", &w
, &h
);
494 rb
->lcd_putsxy(LCD_WIDTH
- w
, y_loc
, "Total");
495 rb
->lcd_getstringsize("Money", &w
, &h
);
496 rb
->lcd_putsxy(LCD_WIDTH
- w
, 0, "Money");
497 rb
->snprintf(str
, 9, "$%d", bj
->player_money
- bj
->current_bet
);
498 rb
->lcd_getstringsize(str
, &w
, &h
);
499 rb
->lcd_putsxy(LCD_WIDTH
- w
, h
+ 1, str
);
500 rb
->snprintf(str
, 3, "%d", bj
->player_total
);
501 rb
->lcd_getstringsize(str
, &w
, &h
);
502 rb
->lcd_putsxy(LCD_WIDTH
- w
, y_loc
+ h
, str
);
505 /*****************************************************************************
506 * find_value() is passed a card and returns its blackjack value.
507 ******************************************************************************/
508 static unsigned int find_value(unsigned int number
) {
509 unsigned int thisValue
;
511 thisValue
= 11; /* Aces get a value of 11 at first */
512 else if (number
< 10)
513 thisValue
= number
+ 1;
515 thisValue
= 10; /* Anything 10 or higher gets a value of 10 */
520 /*****************************************************************************
521 * draw_card() draws a card to the screen.
522 ******************************************************************************/
523 static void draw_card(struct card temp_card
, bool shown
, unsigned int x
,
526 rb
->lcd_bitmap_part(card_deck
, CARD_WIDTH
*temp_card
.num
,
527 CARD_HEIGHT
*temp_card
.suit
, BMPWIDTH_card_deck
,
528 x
+1, y
+1, CARD_WIDTH
, CARD_HEIGHT
);
530 rb
->lcd_bitmap(card_back
, x
+1, y
+1,CARD_WIDTH
, CARD_HEIGHT
);
532 rb
->lcd_set_foreground(LCD_BLACK
);
537 rb
->lcd_hline(x
+2, x
+CARD_WIDTH
-1, y
);
538 rb
->lcd_hline(x
+2, x
+CARD_WIDTH
-1, y
+CARD_HEIGHT
+1);
539 rb
->lcd_vline(x
, y
+2, y
+CARD_HEIGHT
-3);
540 rb
->lcd_vline(x
+CARD_WIDTH
+1, y
+2, y
+CARD_HEIGHT
-1);
541 rb
->lcd_drawpixel(x
+1, y
+1);
542 rb
->lcd_drawpixel(x
+1, y
+CARD_HEIGHT
);
543 rb
->lcd_drawpixel(x
+CARD_WIDTH
, y
+1);
544 rb
->lcd_drawpixel(x
+CARD_WIDTH
, y
+CARD_HEIGHT
);
546 rb
->lcd_hline(x
+1, x
+CARD_WIDTH
, y
);
547 rb
->lcd_hline(x
+1, x
+CARD_WIDTH
, y
+CARD_HEIGHT
+1);
548 rb
->lcd_vline(x
, y
+1, y
+CARD_HEIGHT
);
549 rb
->lcd_vline(x
+CARD_WIDTH
+1, y
+1, y
+CARD_HEIGHT
);
553 rb
->lcd_set_foreground(FG_COLOR
);
557 /*****************************************************************************
558 * new_card() initializes a new card and gives it values.
559 ******************************************************************************/
560 static struct card
new_card(void) {
561 struct card new_card
;
562 new_card
.suit
= rb
->rand()%4; /* Random number 0-3 */
563 new_card
.num
= rb
->rand()%13; /* Random number 0-12 */
564 new_card
.value
= find_value(new_card
.num
);
565 new_card
.is_soft_ace
= new_card
.num
== 0 ? true : false;
569 /*****************************************************************************
570 * deal_init_card() deals and draws to the screen the player's and dealer's
572 ******************************************************************************/
573 static void deal_init_cards(struct game_context
* bj
) {
574 bj
->dealer_cards
[0] = new_card();
575 bj
->dealer_total
+= bj
->dealer_cards
[0].value
;
577 draw_card(bj
->dealer_cards
[0], false, dealer_x
, dealer_y
);
579 bj
->dealer_cards
[1] = new_card();
580 bj
->dealer_total
+= bj
->dealer_cards
[1].value
;
581 draw_card(bj
->dealer_cards
[1], true, dealer_x
+ CARD_WIDTH
+ 4, dealer_y
);
583 bj
->player_cards
[0][0] = new_card();
584 bj
->player_total
+= bj
->player_cards
[0][0].value
;
585 draw_card(bj
->player_cards
[0][0], true, player_x
, player_y
);
586 player_x
+= CARD_WIDTH
+ 4;
588 bj
->player_cards
[0][1] = new_card();
589 bj
->player_total
+= bj
->player_cards
[0][1].value
;
590 draw_card(bj
->player_cards
[0][1], true, player_x
, player_y
);
591 player_x
+= CARD_WIDTH
+ 4;
594 /*****************************************************************************
595 * redraw_board() redraws all the cards and the board
596 ******************************************************************************/
597 static void redraw_board(struct game_context
* bj
) {
598 unsigned int i
, n
, upper_bound
;
599 rb
->lcd_clear_display();
601 blackjack_drawtable(bj
);
604 upper_bound
= bj
->split_status
> 1 ? 2 : 1;
606 for (i
= 0; i
< bj
->num_dealer_cards
; i
++) {
608 draw_card(bj
->dealer_cards
[0], false, dealer_x
, dealer_y
);
610 /* increment i so the dealer's first card isn't displayed */
612 dealer_x
+= CARD_WIDTH
+ 4;
614 draw_card(bj
->dealer_cards
[i
], true, dealer_x
, dealer_y
);
616 if (bj
->num_dealer_cards
> MAX_CARDS
-1)
619 dealer_x
+= CARD_WIDTH
+ 4;
622 for (n
= 0; n
< upper_bound
; n
++) {
623 for (i
= 0; i
< bj
->num_player_cards
[n
]; i
++) {
624 draw_card(bj
->player_cards
[n
][i
], true, player_x
, player_y
);
625 if (bj
->split_status
>1 || bj
->num_player_cards
[n
]>MAX_CARDS
)
628 player_x
+= CARD_WIDTH
+ 4;
630 if (bj
->split_status
> 1)
631 player_x
= LCD_WIDTH
/2 + 4;
635 /*****************************************************************************
636 * update_total updates the player's total
637 ******************************************************************************/
638 static void update_total(struct game_context
* bj
) {
641 rb
->snprintf(total
, 3, "%d", bj
->player_total
);
642 rb
->lcd_getstringsize(total
, &w
, &h
);
646 rb
->lcd_putsxy(LCD_WIDTH
- w
, LCD_HEIGHT
/2 + h
, total
);
647 rb
->lcd_update_rect(LCD_WIDTH
- w
, LCD_HEIGHT
/2 + h
, w
, h
);
651 /*****************************************************************************
652 * check_for_aces() is passed an array of cards and returns where an ace is
653 * located. Otherwise, returns -1.
654 ******************************************************************************/
655 static signed int check_for_aces(struct card temp_cards
[],
658 for(i
= 0; i
< size
; i
++) {
659 if (temp_cards
[i
].is_soft_ace
== true)
665 /*****************************************************************************
666 * check_totals() compares player and dealer totals.
667 * 0: bust 1: loss, 2: push, 3: win, 4: blackjack, 5: something's not right...
668 ******************************************************************************/
669 static unsigned int check_totals(struct game_context
* bj
)
672 if (bj
->player_total
> 21)
674 else if (bj
->player_total
== 21 && bj
->is_blackjack
)
675 if (bj
->dealer_total
== 21 && bj
->num_dealer_cards
== 2)
679 else if (bj
->player_total
== bj
->dealer_total
)
681 else if (bj
->dealer_total
> 21 && bj
->player_total
< 22)
683 else if (bj
->dealer_total
> bj
->player_total
)
685 else if (bj
->player_total
> bj
->dealer_total
)
693 /*****************************************************************************
694 * finish_dealer() draws cards for the dealer until he has 17 or more.
695 ******************************************************************************/
696 static void finish_dealer(struct game_context
* bj
) {
699 if (bj
->dealer_total
> 16 && bj
->dealer_total
< 22)
702 while (bj
->dealer_total
< 17) {
703 bj
->dealer_cards
[bj
->num_dealer_cards
] = new_card();
704 bj
->dealer_total
+= bj
->dealer_cards
[bj
->num_dealer_cards
].value
;
705 bj
->num_dealer_cards
++;
708 while (bj
->dealer_total
> 21) {
709 temp
= check_for_aces(bj
->dealer_cards
, bj
->num_dealer_cards
);
711 bj
->dealer_cards
[temp
].is_soft_ace
= false;
712 bj
->dealer_total
-= 10;
719 /*****************************************************************************
720 * finish_game() completes the game once player's turn is over.
721 ******************************************************************************/
722 static void finish_game(struct game_context
* bj
) {
723 unsigned int rValue
, w
, h
;
728 } while (bj
->dealer_total
< 17);
731 rValue
= check_totals(bj
);
734 rb
->snprintf(str
, sizeof(str
), " Bust! ");
735 bj
->player_money
-= bj
->current_bet
;
737 else if (rValue
== 1) {
738 rb
->snprintf(str
, sizeof(str
), " Sorry, you lost. ");
739 bj
->player_money
-= bj
->current_bet
;
741 else if (rValue
== 2) {
742 rb
->snprintf(str
, sizeof(str
), " Push ");
744 else if (rValue
== 3) {
745 rb
->snprintf(str
, sizeof(str
), " You won! ");
746 bj
->player_money
+= bj
->current_bet
;
749 rb
->snprintf(str
, sizeof(str
), " Blackjack! ");
750 bj
->player_money
+= bj
->current_bet
* 3 / 2;
752 rb
->lcd_getstringsize(str
, &w
, &h
);
755 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
756 rb
->lcd_fillrect(0, LCD_HEIGHT
/2, LCD_WIDTH
, LCD_HEIGHT
/2);
757 rb
->lcd_set_drawmode(DRMODE_SOLID
);
758 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + h
, str
);
759 rb
->snprintf(str
, 12, "You have %d", bj
->player_total
);
760 rb
->lcd_getstringsize(str
, &w
, &h
);
761 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2, str
);
763 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 - h
/2, str
);
768 /*****************************************************************************
769 * blackjack_recordscore() inserts a high score into the high scores list and
770 * returns the high score position.
771 ******************************************************************************/
772 static unsigned int blackjack_recordscore(struct game_context
* bj
) {
774 unsigned int position
= 0;
775 signed short current
, temp
;
777 /* calculate total score */
778 current
= bj
->player_money
;
779 if(current
<= 10) return 0;
781 /* insert the current score into the high scores */
782 for(i
=0; i
<NUM_SCORES
; i
++) {
783 if(current
>= bj
->highscores
[i
]) {
788 temp
= bj
->highscores
[i
];
789 bj
->highscores
[i
] = current
;
797 /*****************************************************************************
798 * blackjack_loadscores() loads the high scores saved file.
799 ******************************************************************************/
800 static void blackjack_loadscores(struct game_context
* bj
) {
805 /* clear high scores */
806 rb
->memset(bj
->highscores
, 0, sizeof(bj
->highscores
));
808 /* open scores file */
809 fd
= rb
->open(SCORE_FILE
, O_RDONLY
);
812 /* read in high scores */
813 if(rb
->read(fd
, bj
->highscores
, sizeof(bj
->highscores
)) <= 0) {
814 /* scores are bad, reset */
815 rb
->memset(bj
->highscores
, 0, sizeof(bj
->highscores
));
821 /*****************************************************************************
822 * blackjack_savescores() saves the high scores saved file.
823 ******************************************************************************/
824 static void blackjack_savescores(struct game_context
* bj
) {
827 /* write out the high scores to the save file */
828 fd
= rb
->open(SCORE_FILE
, O_WRONLY
|O_CREAT
);
829 rb
->write(fd
, bj
->highscores
, sizeof(bj
->highscores
));
834 /*****************************************************************************
835 * blackjack_loadgame() loads the saved game and returns load success.
836 ******************************************************************************/
837 static bool blackjack_loadgame(struct game_context
* bj
) {
842 fd
= rb
->open(SAVE_FILE
, O_RDONLY
);
843 if(fd
< 0) return loaded
;
845 /* read in saved game */
847 if(rb
->read(fd
, &bj
->player_money
, sizeof(bj
->player_money
)) <= 0) break;
848 if(rb
->read(fd
, &bj
->player_total
, sizeof(bj
->player_total
)) <= 0) break;
849 if(rb
->read(fd
, &bj
->dealer_total
, sizeof(bj
->dealer_total
)) <= 0) break;
850 if(rb
->read(fd
, &bj
->num_player_cards
, sizeof(bj
->num_player_cards
))<=0)
852 if(rb
->read(fd
, &bj
->num_dealer_cards
, sizeof(bj
->num_dealer_cards
))<=0)
854 if(rb
->read(fd
, &bj
->current_bet
, sizeof(bj
->current_bet
)) <= 0) break;
855 if(rb
->read(fd
, &bj
->is_blackjack
, sizeof(bj
->is_blackjack
)) <= 0) break;
856 if(rb
->read(fd
, &bj
->split_status
, sizeof(bj
->split_status
)) <= 0) break;
857 if(rb
->read(fd
, &bj
->asked_insurance
, sizeof(bj
->asked_insurance
)) <= 0)
859 if(rb
->read(fd
, &bj
->end_hand
, sizeof(bj
->end_hand
)) <= 0) break;
860 if(rb
->read(fd
, &bj
->player_cards
, sizeof(bj
->player_cards
)) <= 0) break;
861 if(rb
->read(fd
, &bj
->dealer_cards
, sizeof(bj
->dealer_cards
)) <= 0) break;
869 /* delete saved file */
870 rb
->remove(SAVE_FILE
);
874 /*****************************************************************************
875 * blackjack_savegame() saves the current game state.
876 ******************************************************************************/
877 static void blackjack_savegame(struct game_context
* bj
) {
880 /* write out the game state to the save file */
881 fd
= rb
->open(SAVE_FILE
, O_WRONLY
|O_CREAT
);
882 rb
->write(fd
, &bj
->player_money
, sizeof(bj
->player_money
));
883 rb
->write(fd
, &bj
->player_total
, sizeof(bj
->player_total
));
884 rb
->write(fd
, &bj
->dealer_total
, sizeof(bj
->dealer_total
));
885 rb
->write(fd
, &bj
->num_player_cards
, sizeof(bj
->num_player_cards
));
886 rb
->write(fd
, &bj
->num_dealer_cards
, sizeof(bj
->num_dealer_cards
));
887 rb
->write(fd
, &bj
->current_bet
, sizeof(bj
->current_bet
));
888 rb
->write(fd
, &bj
->is_blackjack
, sizeof(bj
->is_blackjack
));
889 rb
->write(fd
, &bj
->split_status
, sizeof(bj
->split_status
));
890 rb
->write(fd
, &bj
->asked_insurance
, sizeof(bj
->asked_insurance
));
891 rb
->write(fd
, &bj
->end_hand
, sizeof(bj
->end_hand
));
892 rb
->write(fd
, &bj
->player_cards
, sizeof(bj
->player_cards
));
893 rb
->write(fd
, &bj
->dealer_cards
, sizeof(bj
->dealer_cards
));
899 /*****************************************************************************
900 * blackjack_callback() is the default event handler callback which is called
901 * on usb connect and shutdown.
902 ******************************************************************************/
903 static void blackjack_callback(void* param
) {
904 struct game_context
* bj
= (struct game_context
*) param
;
906 rb
->splash(HZ
, "Saving high scores...");
907 blackjack_savescores(bj
);
911 /*****************************************************************************
912 * blackjack_get_yes_no() gets a yes/no answer from the user
913 ******************************************************************************/
914 static unsigned int blackjack_get_yes_no(char message
[20]) {
916 unsigned int w
, h
, b
, choice
= 0;
917 bool breakout
= false;
918 char message_yes
[24], message_no
[24];
920 rb
->strcpy(message_yes
, message
);
921 rb
->strcpy(message_no
, message
);
922 rb
->strcat(message_yes
, " Yes");
923 rb
->strcat(message_no
, " No");
924 rb
->lcd_getstringsize(message_yes
, &w
, &h
);
925 const char *stg
[] = {message_yes
, message_no
};
933 #ifdef HAVE_LCD_COLOR
934 rb
->lcd_fillrect(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
, w
+1, h
+3);
935 rb
->lcd_set_foreground(LCD_BLACK
);
936 rb
->lcd_set_background(LCD_WHITE
);
938 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
939 rb
->lcd_fillrect(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
, w
+1, h
+3);
940 rb
->lcd_set_drawmode(DRMODE_SOLID
);
942 rb
->lcd_drawrect(LCD_WIDTH
/2 - w
/2 - 1, LCD_HEIGHT
/2 + b
- 1, w
+3, h
+4);
945 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
+1, stg
[choice
]);
946 rb
->lcd_update_rect(LCD_WIDTH
/2 - w
/2 - 1, LCD_HEIGHT
/2 + b
-1,
948 button
= rb
->button_get(true);
952 case (BJACK_LEFT
|BUTTON_REPEAT
):
954 case (BJACK_RIGHT
|BUTTON_REPEAT
):
957 case BJACK_START
: breakout
= true;
959 case BJACK_QUIT
: breakout
= true;
966 rb
->lcd_set_foreground(FG_COLOR
);
967 rb
->lcd_set_background(BG_COLOR
);
972 /*****************************************************************************
973 * blackjack_get_amount() gets an amount from the player to be used
974 ******************************************************************************/
975 static signed int blackjack_get_amount(char message
[20], signed int lower_limit
,
976 signed int upper_limit
,
980 bool changed
= false;
984 rb
->lcd_getstringsize("A", &w
, &h
); /* find the size of one character */
986 if (start
> upper_limit
)
987 amount
= upper_limit
;
988 else if (start
< lower_limit
)
989 amount
= lower_limit
;
994 rb
->lcd_set_background(LCD_WHITE
);
995 rb
->lcd_set_foreground(LCD_BLACK
);
999 rb
->lcd_clear_display();
1000 rb
->lcd_puts(0, 1, message
);
1001 rb
->snprintf(str
, 9, "$%d", amount
);
1002 rb
->lcd_puts(0, 2, str
);
1003 rb
->lcd_puts(0, 3, "RIGHT: +1");
1004 rb
->lcd_puts(0, 4, "LEFT: -1");
1005 rb
->lcd_puts(0, 5, "UP: +10");
1006 rb
->lcd_puts(0, 6, "DOWN: -10");
1009 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
1010 rb
->lcd_fillrect(LCD_WIDTH
/2 - 9*w
- 1, LCD_HEIGHT
/2 - 4*h
- 3, 37*w
/ 2,
1012 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1013 rb
->lcd_drawrect(LCD_WIDTH
/2 - 9*w
- 1, LCD_HEIGHT
/2 - 4*h
- 3, 37*w
/ 2,
1015 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 4*h
- 1, message
);
1016 rb
->snprintf(str
, 9, "$%d", amount
);
1017 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, str
);
1018 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
1019 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
1020 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
1021 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, " >>|: +1");
1022 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, " |<<: -1");
1023 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "SCROLL+: +10");
1024 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "SCROLL-: -10");
1025 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
1026 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, "RIGHT: +1");
1027 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, "LEFT: -1");
1028 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "SCROLL+: +10");
1029 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "SCROLL-: -10");
1031 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, "RIGHT: +1");
1032 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, "LEFT: -1");
1033 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "UP: +10");
1034 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "DOWN: -10");
1036 rb
->lcd_update_rect(LCD_WIDTH
/2 - 9*w
- 2, LCD_HEIGHT
/2 - 9*h
/2, 37*w
/2 + 1,
1041 button
= rb
->button_get(true);
1045 case (BJACK_UP
|BUTTON_REPEAT
):
1046 if (amount
+ 10 < upper_limit
+ 1) {
1052 case (BJACK_DOWN
|BUTTON_REPEAT
):
1053 if (amount
- 10 > lower_limit
- 1) {
1059 case (BJACK_RIGHT
|BUTTON_REPEAT
):
1060 if (amount
+ 1 < upper_limit
+ 1) {
1066 case (BJACK_LEFT
|BUTTON_REPEAT
):
1067 if (amount
- 1 > lower_limit
- 1) {
1073 amount
= upper_limit
;
1077 amount
= lower_limit
;
1084 rb
->lcd_set_foreground(FG_COLOR
);
1085 rb
->lcd_set_background(BG_COLOR
);
1087 rb
->lcd_clear_display();
1092 rb
->snprintf(str
, 9, "$%d", amount
);
1093 #if LCD_HEIGHT <= 64
1094 rb
->lcd_puts(0, 2, str
);
1097 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
1098 rb
->lcd_fillrect(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, 5*w
, h
);
1099 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1100 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, str
);
1101 rb
->lcd_update_rect(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, 5*w
, h
);
1108 /*****************************************************************************
1109 * blackjack_get_bet() gets the player's bet.
1110 ******************************************************************************/
1111 static void blackjack_get_bet(struct game_context
* bj
) {
1112 bj
->current_bet
= blackjack_get_amount("Please enter a bet", 10,
1113 bj
->player_money
, bj
->current_bet
);
1116 /*****************************************************************************
1117 * double_down() returns one final card then finishes the game
1118 ******************************************************************************/
1119 static void double_down(struct game_context
* bj
) {
1120 bj
->current_bet
*= 2;
1121 bj
->player_cards
[0][bj
->num_player_cards
[0]] = new_card();
1122 bj
->player_total
+= bj
->player_cards
[0][bj
->num_player_cards
[0]].value
;
1123 bj
->num_player_cards
[0]++;
1126 /*****************************************************************************
1127 * split() checks if the player wants to split and acts accordingly.
1128 * When bj->split_status is 1, no split occurred. 2 means the player split and 3
1129 * means a split has already occurred and the first hand is done.
1130 ******************************************************************************/
1131 static void split(struct game_context
* bj
) {
1132 if (blackjack_get_yes_no("Split?") == 1)
1133 bj
->split_status
= 1;
1135 bj
->split_status
= 2;
1136 bj
->current_bet
*= 2;
1137 bj
->num_player_cards
[0] = 1;
1138 bj
->num_player_cards
[1] = 1;
1139 bj
->player_cards
[1][0] = bj
->player_cards
[0][1];
1140 bj
->player_total
= bj
->player_cards
[0][0].value
;
1144 /*****************************************************************************
1145 * insurance() see if the player wants to buy insurance and how much.
1146 ******************************************************************************/
1147 static unsigned int insurance(struct game_context
* bj
) {
1148 unsigned int insurance
, max_amount
;
1150 insurance
= blackjack_get_yes_no("Buy Insurance?");
1151 bj
->asked_insurance
= true;
1152 max_amount
= bj
->current_bet
< (unsigned int)bj
->player_money
?
1153 bj
->current_bet
/2 : (unsigned int)bj
->player_money
;
1154 if (insurance
== 1) return 0;
1156 insurance
= blackjack_get_amount("How much?", 0, max_amount
, 0);
1161 /*****************************************************************************
1162 * play_again() checks to see if the player wants to keep playing.
1163 ******************************************************************************/
1164 static unsigned int play_again(void) {
1165 return blackjack_get_yes_no("Play Again?");
1168 /*****************************************************************************
1169 * blackjack_menu() is the initial menu at the start of the game.
1170 ******************************************************************************/
1171 static unsigned int blackjack_menu(struct game_context
* bj
) {
1173 char *title
= "Blackjack";
1175 unsigned int i
, w
, h
;
1176 bool breakout
= false;
1177 bool showscores
= false;
1181 rb
->lcd_set_background(BG_COLOR
);
1182 rb
->lcd_set_foreground(FG_COLOR
);
1184 rb
->lcd_clear_display();
1187 /* welcome screen to display key bindings */
1188 rb
->lcd_getstringsize(title
, &w
, &h
);
1189 rb
->lcd_putsxy((LCD_WIDTH
-w
)/2, 0, title
);
1191 #if CONFIG_KEYPAD == RECORDER_PAD
1192 rb
->lcd_puts(0, 1, "ON: start");
1193 rb
->lcd_puts(0, 2, "OFF: exit");
1194 rb
->lcd_puts(0, 3, "F1: hit");
1195 rb
->lcd_puts(0, 4, "F2: stay");
1196 rb
->lcd_puts(0, 5, "F3: double down");
1197 rb
->lcd_puts(0, 6, "PLAY: save/resume");
1198 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1199 rb
->lcd_puts(0, 7, str
);
1200 #elif CONFIG_KEYPAD == ONDIO_PAD
1201 rb
->lcd_puts(0, 1, "MENU: start");
1202 rb
->lcd_puts(0, 2, "OFF: exit");
1203 rb
->lcd_puts(0, 3, "LEFT: hit");
1204 rb
->lcd_puts(0, 4, "RIGHT: stay");
1205 rb
->lcd_puts(0, 5, "UP: double down");
1206 rb
->lcd_puts(0, 6, "DOWN: save/resume");
1207 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1208 rb
->lcd_puts(0, 7, str
);
1209 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
1210 rb
->lcd_puts(0, 2, "PLAY to start & to hit");
1211 rb
->lcd_puts(0, 3, "STOP to exit");
1212 rb
->lcd_puts(0, 4, "REC to stay");
1213 rb
->lcd_puts(0, 5, "NAVI to double down ");
1214 rb
->lcd_puts(0, 6, " & to view highscores");
1215 rb
->lcd_puts(0, 7, "AB to save/resume");
1216 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1217 rb
->lcd_puts(0, 8, str
);
1218 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
1219 rb
->lcd_puts(0, 2, "PLAY to start & hit");
1220 rb
->lcd_puts(0, 3, "POWER to exit");
1221 rb
->lcd_puts(0, 4, ">>| to stay");
1222 rb
->lcd_puts(0, 5, "|<< to double down");
1223 rb
->lcd_puts(0, 6, "LEFT to view scores");
1224 rb
->lcd_puts(0, 7, "RIGHT to save/resume");
1225 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1226 rb
->lcd_puts(0, 8, str
);
1228 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
1229 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
1230 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
1232 rb
->lcd_puts(0, 2, "SELECT to start & to hit");
1233 rb
->lcd_puts(0, 3, "MENU to exit");
1234 rb
->lcd_puts(0, 4, ">>| to stay & to view highscores");
1235 rb
->lcd_puts(0, 5, "|<< to double down");
1236 rb
->lcd_puts(0, 6, "PLAY to save/resume");
1237 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1238 rb
->lcd_puts(0, 7, str
);
1240 rb
->lcd_puts(0, 2, "SELECT to start & to ");
1241 rb
->lcd_puts(0, 3, " hit");
1242 rb
->lcd_puts(0, 4, "MENU to exit");
1243 rb
->lcd_puts(0, 5, ">>| to stay & to view ");
1244 rb
->lcd_puts(0, 6, " highscores");
1245 rb
->lcd_puts(0, 7, "|<< to double down");
1246 rb
->lcd_puts(0, 8, "PLAY to save/resume");
1247 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1248 rb
->lcd_puts(0, 9, str
);
1250 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
1251 rb
->lcd_puts(0, 2, "PLAY to start to hit");
1252 rb
->lcd_puts(0, 3, "POWER to exit");
1253 rb
->lcd_puts(0, 4, "SELECT to hit");
1254 rb
->lcd_puts(0, 5, "REC to stay");
1255 rb
->lcd_puts(0, 6, "PLAY to double down");
1256 rb
->lcd_puts(0, 7, "RIGHT to view highscores ");
1257 rb
->lcd_puts(0, 8, "DOWN to save/resume");
1258 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1259 rb
->lcd_puts(0, 9, str
);
1260 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
1261 rb
->lcd_puts(0, 2, "AB to start & to");
1262 rb
->lcd_puts(0, 3, " stay");
1263 rb
->lcd_puts(0, 4, "EQ to hit");
1264 rb
->lcd_puts(0, 5, "PLAY to exit");
1265 rb
->lcd_puts(0, 6, "CLICK to double down");
1266 rb
->lcd_puts(0, 7, "& to view highscores");
1267 rb
->lcd_puts(0, 8, "AB+EQ to save/resume");
1268 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1269 rb
->lcd_puts(0, 9, str
);
1270 #elif CONFIG_KEYPAD == GIGABEAT_PAD
1271 rb
->lcd_puts(0, 2, "A to start");
1272 rb
->lcd_puts(0, 3, "POWER to exit");
1273 rb
->lcd_puts(0, 4, "VOL+ to hit");
1274 rb
->lcd_puts(0, 5, "VOL- to stay");
1275 rb
->lcd_puts(0, 6, "SELECT to double down");
1276 rb
->lcd_puts(0, 7, "RIGHT to view highscores ");
1277 rb
->lcd_puts(0, 8, "MENU to save/resume");
1278 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1279 rb
->lcd_puts(0, 9, str
);
1280 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
1281 rb
->lcd_puts(0, 2, "PLAY to start");
1282 rb
->lcd_puts(0, 3, "BACK to exit");
1283 rb
->lcd_puts(0, 4, "VOL+ to hit");
1284 rb
->lcd_puts(0, 5, "VOL- to stay");
1285 rb
->lcd_puts(0, 6, "SELECT to double down");
1286 rb
->lcd_puts(0, 7, "RIGHT to view highscores ");
1287 rb
->lcd_puts(0, 8, "MENU to save/resume");
1288 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1289 rb
->lcd_puts(0, 9, str
);
1290 #elif CONFIG_KEYPAD == MROBE100_PAD
1291 rb
->lcd_puts(0, 2, "SELECT to start");
1292 rb
->lcd_puts(0, 3, "POWER to exit");
1293 rb
->lcd_puts(0, 4, "MENU to hit");
1294 rb
->lcd_puts(0, 5, "DISPLAY to stay");
1295 rb
->lcd_puts(0, 6, "DOWN to double down");
1296 rb
->lcd_puts(0, 7, "RIGHT to view highscores ");
1297 rb
->lcd_puts(0, 8, "PLAY to save/resume");
1298 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1299 rb
->lcd_puts(0, 9, str
);
1300 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
1301 rb
->lcd_puts(0, 2, "SELECT to start & to hit");
1302 rb
->lcd_puts(0, 3, "POWER to exit");
1303 rb
->lcd_puts(0, 4, "RIGHT to stay");
1304 rb
->lcd_puts(0, 5, "LEFT to double down");
1305 rb
->lcd_puts(0, 6, "REC to save/resume");
1306 rb
->lcd_puts(0, 7, "UP to view scores");
1307 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1308 rb
->lcd_puts(0, 8, str
);
1309 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
1310 rb
->lcd_puts(0, 2, "SELECT to start & to hit");
1311 rb
->lcd_puts(0, 3, "POWER to exit");
1312 rb
->lcd_puts(0, 4, "RIGHT to stay");
1313 rb
->lcd_puts(0, 5, "LEFT to double down");
1314 rb
->lcd_puts(0, 6, "DOWN to save/resume");
1315 rb
->lcd_puts(0, 7, "REC to view scores");
1316 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1317 rb
->lcd_puts(0, 9, str
);
1318 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
1319 rb
->lcd_puts(0, 2, "PLAY to start & to");
1320 rb
->lcd_puts(0, 3, " hit");
1321 rb
->lcd_puts(0, 4, "REC to exit");
1322 rb
->lcd_puts(0, 5, "FF to stay");
1323 rb
->lcd_puts(0, 6, "REW to double down");
1324 rb
->lcd_puts(0, 7, "MODE to save/resume");
1325 rb
->lcd_puts(0, 8, "MENU to view scores");
1326 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1327 rb
->lcd_puts(0, 10, str
);
1328 #elif CONFIG_KEYPAD == COWOND2_PAD
1329 rb
->lcd_puts(0, 6, "POWER to exit");
1330 rb
->lcd_puts(0, 7, "MINUS to double down");
1331 rb
->lcd_puts(0, 8, "MENU to view scores");
1332 rb
->snprintf(str
, 21, "High Score: $%d", bj
->highscores
[0]);
1333 rb
->lcd_puts(0, 10, str
);
1336 #ifdef HAVE_TOUCHSCREEN
1337 rb
->lcd_puts(0, 2, "LCD CENTRE to start & to hit");
1338 rb
->lcd_puts(0, 3, "LCD BOTTOMLEFT to stay");
1339 rb
->lcd_puts(0, 4, "LCD BOTTOMRIGHT to save/resume");
1342 rb
->snprintf(str
, 12, "%s", "High Scores");
1343 rb
->lcd_getstringsize(str
, &w
, &h
);
1344 rb
->lcd_putsxy((LCD_WIDTH
-w
)/2, 0, str
);
1346 /* print high scores */
1347 for(i
=0; i
<NUM_SCORES
; i
++) {
1348 rb
->snprintf(str
, 14, "#%02d: $%d", i
+1, bj
->highscores
[i
]);
1349 rb
->lcd_puts(0, i
+1, str
);
1355 /* handle menu button presses */
1356 button
= rb
->button_get(true);
1359 case BJACK_START
: /* start playing */
1363 case BJACK_QUIT
: /* quit program */
1370 case BJACK_RESUME
:/* resume game */
1371 if(!blackjack_loadgame(bj
)) {
1372 rb
->splash(HZ
*2, "Nothing to resume");
1374 rb
->splash(HZ
*2, "Loading...");
1379 case BJACK_SCORES
:/* toggle high scores */
1380 showscores
= !showscores
;
1384 if(rb
->default_event_handler_ex(button
, blackjack_callback
,
1385 (void*) bj
) == SYS_USB_CONNECTED
)
1396 /*****************************************************************************
1397 * blackjack() is the main game subroutine, it returns the final game status.
1398 ******************************************************************************/
1399 static int blackjack(struct game_context
* bj
) {
1401 unsigned int w
, h
, temp_var
, done
= 0, todo
= 1;
1403 bool breakout
= false;
1404 bool dbl_down
= false;
1406 /* don't resume by default */
1409 /********************
1411 ********************/
1412 temp_var
= blackjack_menu(bj
);
1413 if (temp_var
== BJ_QUIT
|| temp_var
== BJ_USB
)
1417 /********************
1419 ********************/
1423 /********************
1425 ********************/
1427 /* check for resumed game */
1431 if (bj
->split_status
== 2) {
1433 player_x
= bj
->num_player_cards
[0] * 10 + 4;
1435 else if (bj
->split_status
== 3) {
1436 player_x
= bj
->num_player_cards
[1] * 10 + LCD_WIDTH
/2 + 4;
1443 bj
->player_money
= 1000;
1444 blackjack_get_bet(bj
);
1445 if (bj
->current_bet
== 0)
1447 rb
->lcd_clear_display();
1448 deal_init_cards(bj
);
1449 blackjack_drawtable(bj
);
1457 if(bj
->player_total
== 21 && bj
->num_player_cards
[0] == 2) {
1458 bj
->is_blackjack
= true;
1459 bj
->end_hand
= true;
1462 else if(bj
->dealer_cards
[1].is_soft_ace
&& !breakout
&&
1463 !bj
->asked_insurance
) {
1464 temp_var
= insurance(bj
);
1465 if (bj
->dealer_total
== 21) {
1466 rb
->splash(HZ
, "Dealer has blackjack");
1467 bj
->player_money
+= temp_var
;
1468 bj
->end_hand
= true;
1474 rb
->splash(HZ
, "Dealer does not have blackjack");
1475 bj
->player_money
-= temp_var
;
1481 if(!bj
->end_hand
&& bj
->split_status
== 0 &&
1482 bj
->player_cards
[0][0].num
== bj
->player_cards
[0][1].num
) {
1485 rb
->lcd_update_rect(0, LCD_HEIGHT
/2, LCD_WIDTH
, LCD_HEIGHT
/2);
1486 if (bj
->split_status
== 2) {
1488 player_x
= bj
->num_player_cards
[0] * 10 + 4;
1492 while(!bj
->end_hand
&& done
< todo
) {
1493 button
= rb
->button_get(true);
1497 NEXT_CARD
= new_card();
1498 bj
->player_total
+= NEXT_CARD
.value
;
1499 draw_card(NEXT_CARD
, true, player_x
, player_y
);
1500 bj
->num_player_cards
[done
]++;
1501 if (bj
->num_player_cards
[done
] == MAX_CARDS
+ 1) {
1503 rb
->lcd_update_rect(0, LCD_HEIGHT
/2, LCD_WIDTH
,
1506 else if (bj
->num_player_cards
[done
]>MAX_CARDS
|| todo
> 1) {
1507 rb
->lcd_update_rect(player_x
, player_y
, CARD_WIDTH
+2,
1512 rb
->lcd_update_rect(player_x
, player_y
, CARD_WIDTH
+2,
1514 player_x
+= CARD_WIDTH
+ 4;
1520 bj
->end_hand
= true;
1522 case BJACK_DOUBLEDOWN
:
1523 if ((signed int)bj
->current_bet
* 2 < bj
->player_money
+ 1 &&
1524 bj
->num_player_cards
[0]==2 && todo
==1) {
1527 if (bj
->player_total
< 22) {
1528 bj
->end_hand
= true;
1532 else if((signed int)bj
->current_bet
* 2 > bj
->player_money
) {
1533 rb
->splash(HZ
, "Not enough money to double down.");
1538 case BJACK_RESUME
: /* save and end game */
1539 rb
->splash(HZ
, "Saving game...");
1540 blackjack_savegame(bj
);
1541 /* fall through to BJACK_QUIT */
1547 while (bj
->player_total
> 21 && !bj
->end_hand
) {
1548 temp
= check_for_aces(bj
->player_cards
[done
],
1549 bj
->num_player_cards
[done
]);
1551 bj
->player_cards
[done
][temp
].is_soft_ace
= false;
1552 bj
->player_total
-= 10;
1555 bj
->end_hand
= true;
1560 bj
->end_hand
= true;
1567 temp
= bj
->player_total
;
1568 bj
->player_total
= temp_var
;
1571 rb
->lcd_getstringsize(" Split 1 ", &w
, &h
);
1572 rb
->lcd_putsxy(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1574 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1576 bj
->current_bet
/= 2;
1577 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1580 bj
->player_total
= temp_var
;
1582 rb
->lcd_getstringsize(" Split 2 ", &w
, &h
);
1583 rb
->lcd_putsxy(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1585 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1590 bj
->end_hand
= false;
1591 bj
->split_status
= 3;
1592 temp_var
= bj
->player_total
;
1593 bj
->player_total
= bj
->player_cards
[1][0].value
;
1605 if (bj
->player_money
< 10) {
1610 if (bj
->end_hand
) { /* If hand is over */
1611 if (play_again() != 0) /* User wants to quit */
1613 else { /* User keeps playing */
1617 bj
->current_bet
/= 2;
1623 blackjack_get_bet(bj
);
1624 if (bj
->current_bet
== 0)
1626 deal_init_cards(bj
);
1627 blackjack_drawtable(bj
);
1636 /*****************************************************************************
1637 * plugin entry point.
1638 ******************************************************************************/
1639 enum plugin_status
plugin_start(const void* parameter
)
1641 struct game_context bj
;
1643 unsigned int position
;
1649 rb
->lcd_set_backdrop(NULL
);
1652 /* load high scores */
1653 blackjack_loadscores(&bj
);
1655 rb
->lcd_setfont(FONT_SYSFIXED
);
1658 switch(blackjack(&bj
)){
1660 rb
->splash(HZ
, "Not enough money to continue");
1661 /* fall through to BJ_END */
1665 if((position
= blackjack_recordscore(&bj
))) {
1666 rb
->snprintf(str
, 19, "New high score #%d!", position
);
1667 rb
->splash(HZ
*2, str
);
1673 rb
->lcd_setfont(FONT_UI
);
1674 return PLUGIN_USB_CONNECTED
;
1678 rb
->splash(HZ
, "Saving high scores...");
1679 blackjack_savescores(&bj
);
1689 rb
->lcd_setfont(FONT_UI
);