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"
25 #include "lib/display_text.h"
26 #include "lib/highscore.h"
27 #include "lib/playback_control.h"
32 #define SCORE_FILE PLUGIN_GAMES_DIR "/blackjack.score"
33 #define SAVE_FILE PLUGIN_GAMES_DIR "/blackjack.save"
36 /* final game return status */
39 BJ_QUIT_WITHOUT_SAVING
,
45 #if CONFIG_KEYPAD == RECORDER_PAD
46 #define BJACK_SELECT_NAME "PLAY"
47 #define BJACK_STAY_NAME "F1"
48 #define BJACK_QUIT_NAME "OFF"
49 #define BJACK_DOUBLE_NAME "F2"
50 #define BJACK_SELECT BUTTON_PLAY
51 #define BJACK_QUIT BUTTON_OFF
52 #define BJACK_MAX (BUTTON_ON|BUTTON_UP)
53 #define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
54 #define BJACK_STAY BUTTON_F1
55 #define BJACK_DOUBLEDOWN BUTTON_F2
56 #define BJACK_UP BUTTON_UP
57 #define BJACK_DOWN BUTTON_DOWN
58 #define BJACK_RIGHT BUTTON_RIGHT
59 #define BJACK_LEFT BUTTON_LEFT
61 #elif CONFIG_KEYPAD == ONDIO_PAD
62 #define BJACK_SELECT_NAME "MENU"
63 #define BJACK_STAY_NAME "RIGHT"
64 #define BJACK_QUIT_NAME "OFF"
65 #define BJACK_DOUBLE_NAME "UP"
66 #define BJACK_SELECT BUTTON_MENU
67 #define BJACK_QUIT BUTTON_OFF
68 #define BJACK_STAY BUTTON_RIGHT
69 #define BJACK_DOUBLEDOWN BUTTON_UP
70 #define BJACK_UP BUTTON_UP
71 #define BJACK_DOWN BUTTON_DOWN
72 #define BJACK_RIGHT BUTTON_RIGHT
73 #define BJACK_LEFT BUTTON_LEFT
75 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
76 #define BJACK_SELECT_NAME "PLAY"
77 #define BJACK_STAY_NAME ">>|"
78 #define BJACK_QUIT_NAME "POWER"
79 #define BJACK_DOUBLE_NAME "|<<"
80 #define BJACK_SELECT BUTTON_PLAY
81 #define BJACK_QUIT BUTTON_POWER
82 #define BJACK_STAY BUTTON_FF
83 #define BJACK_DOUBLEDOWN BUTTON_REW
84 #define BJACK_UP BUTTON_SCROLL_UP
85 #define BJACK_DOWN BUTTON_SCROLL_DOWN
86 #define BJACK_RIGHT BUTTON_RIGHT
87 #define BJACK_LEFT BUTTON_LEFT
89 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
90 (CONFIG_KEYPAD == IRIVER_H300_PAD)
91 #define BJACK_SELECT_NAME "ON"
92 #define BJACK_STAY_NAME "REC"
93 #define BJACK_QUIT_NAME "OFF"
94 #define BJACK_DOUBLE_NAME "SELECT"
95 #define BJACK_SELECT BUTTON_ON
96 #define BJACK_QUIT BUTTON_OFF
97 #define BJACK_STAY BUTTON_REC
98 #define BJACK_DOUBLEDOWN BUTTON_SELECT
99 #define BJACK_UP BUTTON_UP
100 #define BJACK_DOWN BUTTON_DOWN
101 #define BJACK_RIGHT BUTTON_RIGHT
102 #define BJACK_LEFT BUTTON_LEFT
104 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
105 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
106 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
107 #define BJACK_SELECT_NAME "SELECT"
108 #define BJACK_STAY_NAME "RIGHT"
109 #define BJACK_RESUME_NAME "PLAY"
110 #define BJACK_QUIT_NAME "MENU"
111 #define BJACK_DOUBLE_NAME "LEFT"
112 #define BJACK_SELECT BUTTON_SELECT
113 #define BJACK_QUIT BUTTON_MENU
114 #define BJACK_STAY BUTTON_RIGHT
115 #define BJACK_DOUBLEDOWN BUTTON_LEFT
116 #define BJACK_UP BUTTON_SCROLL_FWD
117 #define BJACK_DOWN BUTTON_SCROLL_BACK
118 #define BJACK_RIGHT BUTTON_RIGHT
119 #define BJACK_LEFT BUTTON_LEFT
121 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
122 #define BJACK_SELECT_NAME "SELECT"
123 #define BJACK_STAY_NAME "REC"
124 #define BJACK_QUIT_NAME "POWER"
125 #define BJACK_DOUBLE_NAME "PLAY"
126 #define BJACK_SELECT BUTTON_SELECT
127 #define BJACK_QUIT BUTTON_POWER
128 #define BJACK_MAX (BUTTON_PLAY|BUTTON_UP)
129 #define BJACK_MIN (BUTTON_PLAY|BUTTON_DOWN)
130 #define BJACK_STAY BUTTON_REC
131 #define BJACK_DOUBLEDOWN BUTTON_PLAY
132 #define BJACK_UP BUTTON_UP
133 #define BJACK_DOWN BUTTON_DOWN
134 #define BJACK_RIGHT BUTTON_RIGHT
135 #define BJACK_LEFT BUTTON_LEFT
137 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
138 #define BJACK_SELECT_NAME "MODE"
139 #define BJACK_STAY_NAME "MODE"
140 #define BJACK_QUIT_NAME "PLAY"
141 #define BJACK_DOUBLE_NAME "SELECT"
142 #define BJACK_SELECT BUTTON_MODE
143 #define BJACK_QUIT BUTTON_PLAY
144 #define BJACK_MAX (BUTTON_EQ|BUTTON_UP)
145 #define BJACK_MIN (BUTTON_EQ|BUTTON_DOWN)
146 #define BJACK_STAY BUTTON_MODE
147 #define BJACK_DOUBLEDOWN BUTTON_SELECT
148 #define BJACK_UP BUTTON_UP
149 #define BJACK_DOWN BUTTON_DOWN
150 #define BJACK_RIGHT BUTTON_RIGHT
151 #define BJACK_LEFT BUTTON_LEFT
153 #elif CONFIG_KEYPAD == GIGABEAT_PAD
154 #define BJACK_SELECT_NAME "SELECT"
155 #define BJACK_STAY_NAME "VOL-"
156 #define BJACK_QUIT_NAME "POWER"
157 #define BJACK_DOUBLE_NAME "A"
158 #define BJACK_SELECT BUTTON_SELECT
159 #define BJACK_QUIT BUTTON_POWER
160 #define BJACK_MAX BUTTON_VOL_UP
161 #define BJACK_MIN BUTTON_VOL_DOWN
162 #define BJACK_STAY BUTTON_VOL_DOWN
163 #define BJACK_DOUBLEDOWN BUTTON_A
164 #define BJACK_UP BUTTON_UP
165 #define BJACK_DOWN BUTTON_DOWN
166 #define BJACK_RIGHT BUTTON_RIGHT
167 #define BJACK_LEFT BUTTON_LEFT
169 #elif CONFIG_KEYPAD == SANSA_E200_PAD
170 #define BJACK_SELECT_NAME "SELECT"
171 #define BJACK_STAY_NAME "RIGHT"
172 #define BJACK_QUIT_NAME "POWER"
173 #define BJACK_DOUBLE_NAME "LEFT"
174 #define BJACK_SELECT BUTTON_SELECT
175 #define BJACK_QUIT BUTTON_POWER
176 #define BJACK_MAX BUTTON_UP
177 #define BJACK_MIN BUTTON_DOWN
178 #define BJACK_STAY BUTTON_RIGHT
179 #define BJACK_DOUBLEDOWN BUTTON_LEFT
180 #define BJACK_UP BUTTON_SCROLL_FWD
181 #define BJACK_DOWN BUTTON_SCROLL_BACK
182 #define BJACK_RIGHT BUTTON_RIGHT
183 #define BJACK_LEFT BUTTON_LEFT
185 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
186 #define BJACK_SELECT_NAME "SELECT"
187 #define BJACK_STAY_NAME "RIGHT"
188 #define BJACK_QUIT_NAME "HOME"
189 #define BJACK_DOUBLE_NAME "LEFT"
190 #define BJACK_SELECT BUTTON_SELECT
191 #define BJACK_QUIT (BUTTON_HOME|BUTTON_REPEAT)
192 #define BJACK_MAX BUTTON_UP
193 #define BJACK_MIN BUTTON_DOWN
194 #define BJACK_STAY BUTTON_RIGHT
195 #define BJACK_DOUBLEDOWN BUTTON_LEFT
196 #define BJACK_UP BUTTON_SCROLL_FWD
197 #define BJACK_DOWN BUTTON_SCROLL_BACK
198 #define BJACK_RIGHT BUTTON_RIGHT
199 #define BJACK_LEFT BUTTON_LEFT
201 #elif CONFIG_KEYPAD == SANSA_C200_PAD
202 #define BJACK_SELECT_NAME "SELECT"
203 #define BJACK_STAY_NAME "RIGHT"
204 #define BJACK_QUIT_NAME "POWER"
205 #define BJACK_DOUBLE_NAME "LEFT"
206 #define BJACK_SELECT BUTTON_SELECT
207 #define BJACK_QUIT BUTTON_POWER
208 #define BJACK_MAX BUTTON_VOL_UP
209 #define BJACK_MIN BUTTON_VOL_DOWN
210 #define BJACK_STAY BUTTON_RIGHT
211 #define BJACK_DOUBLEDOWN BUTTON_LEFT
212 #define BJACK_UP BUTTON_UP
213 #define BJACK_DOWN BUTTON_DOWN
214 #define BJACK_RIGHT BUTTON_RIGHT
215 #define BJACK_LEFT BUTTON_LEFT
217 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
218 #define BJACK_SELECT_NAME "SELECT"
219 #define BJACK_STAY_NAME "RIGHT"
220 #define BJACK_QUIT_NAME "POWER"
221 #define BJACK_DOUBLE_NAME "LEFT"
222 #define BJACK_SELECT BUTTON_SELECT
223 #define BJACK_QUIT BUTTON_POWER
224 #define BJACK_MAX BUTTON_VOL_UP
225 #define BJACK_MIN BUTTON_VOL_DOWN
226 #define BJACK_STAY BUTTON_RIGHT
227 #define BJACK_DOUBLEDOWN BUTTON_LEFT
228 #define BJACK_UP BUTTON_UP
229 #define BJACK_DOWN BUTTON_DOWN
230 #define BJACK_RIGHT BUTTON_RIGHT
231 #define BJACK_LEFT BUTTON_LEFT
233 #elif CONFIG_KEYPAD == SANSA_M200_PAD
234 #define BJACK_SELECT_NAME "SELECT"
235 #define BJACK_STAY_NAME "RIGHT"
236 #define BJACK_QUIT_NAME "POWER"
237 #define BJACK_DOUBLE_NAME "LEFT"
238 #define BJACK_SELECT (BUTTON_SELECT | BUTTON_REL)
239 #define BJACK_QUIT BUTTON_POWER
240 #define BJACK_MAX BUTTON_VOL_UP
241 #define BJACK_MIN BUTTON_VOL_DOWN
242 #define BJACK_STAY BUTTON_RIGHT
243 #define BJACK_DOUBLEDOWN BUTTON_LEFT
244 #define BJACK_UP BUTTON_UP
245 #define BJACK_DOWN BUTTON_DOWN
246 #define BJACK_RIGHT BUTTON_RIGHT
247 #define BJACK_LEFT BUTTON_LEFT
249 #elif CONFIG_KEYPAD == TATUNG_TPJ1022_PAD
250 #define BJACK_SELECT_NAME "MAIN"
251 #define BJACK_STAY_NAME "MENU"
252 #define BJACK_QUIT_NAME "POWER"
253 #define BJACK_DOUBLE_NAME "DOWN"
254 #define BJACK_SELECT BUTTON_MAIN
255 #define BJACK_QUIT BUTTON_POWER
256 #define BJACK_MAX (BUTTON_REC|BUTTON_UP)
257 #define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
258 #define BJACK_STAY BUTTON_MENU
259 #define BJACK_DOUBLEDOWN BUTTON_DOWN
260 #define BJACK_UP BUTTON_UP
261 #define BJACK_DOWN BUTTON_DOWN
262 #define BJACK_RIGHT BUTTON_RIGHT
263 #define BJACK_LEFT BUTTON_LEFT
265 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
266 #define BJACK_SELECT_NAME "PLAY"
267 #define BJACK_STAY_NAME "VOL-"
268 #define BJACK_QUIT_NAME "BACK"
269 #define BJACK_DOUBLE_NAME "SELECT"
270 #define BJACK_SELECT BUTTON_PLAY
271 #define BJACK_QUIT BUTTON_BACK
272 #define BJACK_MAX BUTTON_VOL_UP
273 #define BJACK_MIN BUTTON_VOL_DOWN
274 #define BJACK_STAY BUTTON_VOL_DOWN
275 #define BJACK_DOUBLEDOWN BUTTON_SELECT
276 #define BJACK_UP BUTTON_UP
277 #define BJACK_DOWN BUTTON_DOWN
278 #define BJACK_RIGHT BUTTON_RIGHT
279 #define BJACK_LEFT BUTTON_LEFT
281 #elif CONFIG_KEYPAD == MROBE100_PAD
282 #define BJACK_SELECT_NAME "SELECT"
283 #define BJACK_STAY_NAME "DISPLAY"
284 #define BJACK_QUIT_NAME "POWER"
285 #define BJACK_DOUBLE_NAME "DOWN"
286 #define BJACK_SELECT BUTTON_SELECT
287 #define BJACK_QUIT BUTTON_POWER
288 #define BJACK_MAX BUTTON_MENU
289 #define BJACK_MIN BUTTON_DISPLAY
290 #define BJACK_STAY BUTTON_DISPLAY
291 #define BJACK_DOUBLEDOWN BUTTON_DOWN
292 #define BJACK_UP BUTTON_UP
293 #define BJACK_DOWN BUTTON_DOWN
294 #define BJACK_RIGHT BUTTON_RIGHT
295 #define BJACK_LEFT BUTTON_LEFT
297 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
298 #define BJACK_SELECT_NAME "RC","PLAY"
299 #define BJACK_STAY_NAME "RC", ">>|"
300 #define BJACK_QUIT_NAME "RC_REC"
301 #define BJACK_DOUBLE_NAME "RC_REW"
302 #define BJACK_SELECT BUTTON_RC_PLAY
303 #define BJACK_QUIT BUTTON_RC_REC
304 #define BJACK_STAY BUTTON_RC_FF
305 #define BJACK_DOUBLEDOWN BUTTON_RC_REW
306 #define BJACK_UP BUTTON_RC_VOL_UP
307 #define BJACK_DOWN BUTTON_RC_VOL_DOWN
308 #define BJACK_RIGHT BUTTON_RC_FF
309 #define BJACK_LEFT BUTTON_RC_REW
311 #elif CONFIG_KEYPAD == COWON_D2_PAD
312 #define BJACK_QUIT_NAME "POWER"
313 #define BJACK_DOUBLE_NAME "-"
314 #define BJACK_QUIT BUTTON_POWER
315 #define BJACK_DOUBLEDOWN BUTTON_MINUS
317 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
318 #define BJACK_SELECT_NAME "SELECT"
319 #define BJACK_STAY_NAME "PLAY"
320 #define BJACK_QUIT_NAME "POWER"
321 #define BJACK_DOUBLE_NAME "CUSTOM"
322 #define BJACK_SELECT BUTTON_SELECT
323 #define BJACK_QUIT BUTTON_POWER
324 #define BJACK_STAY BUTTON_PLAY
325 #define BJACK_MAX (BUTTON_CUSTOM|BUTTON_UP)
326 #define BJACK_MIN (BUTTON_CUSTOM|BUTTON_DOWN)
327 #define BJACK_DOUBLEDOWN BUTTON_CUSTOM
328 #define BJACK_UP BUTTON_UP
329 #define BJACK_DOWN BUTTON_DOWN
330 #define BJACK_RIGHT BUTTON_RIGHT
331 #define BJACK_LEFT BUTTON_LEFT
333 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
334 #define BJACK_SELECT_NAME "SELECT"
335 #define BJACK_STAY_NAME "VOL-"
336 #define BJACK_QUIT_NAME "POWER"
337 #define BJACK_DOUBLE_NAME "MENU"
338 #define BJACK_SELECT BUTTON_SELECT
339 #define BJACK_QUIT BUTTON_POWER
340 #define BJACK_MAX BUTTON_VOL_UP
341 #define BJACK_MIN BUTTON_VOL_DOWN
342 #define BJACK_STAY BUTTON_VOL_DOWN
343 #define BJACK_DOUBLEDOWN BUTTON_MENU
344 #define BJACK_UP BUTTON_UP
345 #define BJACK_DOWN BUTTON_DOWN
346 #define BJACK_RIGHT BUTTON_RIGHT
347 #define BJACK_LEFT BUTTON_LEFT
349 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
350 #define BJACK_SELECT_NAME "MENU"
351 #define BJACK_STAY_NAME "VOL-"
352 #define BJACK_QUIT_NAME "POWER"
353 #define BJACK_DOUBLE_NAME "PLAY"
354 #define BJACK_SELECT BUTTON_MENU
355 #define BJACK_QUIT BUTTON_POWER
356 #define BJACK_MAX BUTTON_VOL_UP
357 #define BJACK_MIN BUTTON_VOL_DOWN
358 #define BJACK_STAY BUTTON_VOL_DOWN
359 #define BJACK_DOUBLEDOWN BUTTON_PLAY
360 #define BJACK_UP BUTTON_UP
361 #define BJACK_DOWN BUTTON_DOWN
362 #define BJACK_RIGHT BUTTON_NEXT
363 #define BJACK_LEFT BUTTON_PREV
365 #elif CONFIG_KEYPAD == ONDAVX747_PAD
366 #define BJACK_QUIT_NAME "POWER"
367 #define BJACK_DOUBLE_NAME "Vol-"
368 #define BJACK_QUIT BUTTON_POWER
369 #define BJACK_DOUBLEDOWN BUTTON_VOL_DOWN
371 #elif CONFIG_KEYPAD == ONDAVX777_PAD
372 #define BJACK_QUIT_NAME "POWER"
373 #define BJACK_QUIT BUTTON_POWER
375 #elif CONFIG_KEYPAD == MROBE500_PAD
376 #define BJACK_QUIT_NAME "POWER"
377 #define BJACK_QUIT BUTTON_POWER
379 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
380 #define BJACK_SELECT_NAME "PLAY"
381 #define BJACK_STAY_NAME "RECORD"
382 #define BJACK_QUIT_NAME "REW"
383 #define BJACK_DOUBLE_NAME "FFWD"
384 #define BJACK_SELECT BUTTON_PLAY
385 #define BJACK_QUIT BUTTON_REW
386 #define BJACK_STAY BUTTON_REC
387 #define BJACK_DOUBLEDOWN BUTTON_FFWD
388 #define BJACK_UP BUTTON_UP
389 #define BJACK_DOWN BUTTON_DOWN
390 #define BJACK_RIGHT BUTTON_RIGHT
391 #define BJACK_LEFT BUTTON_LEFT
393 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
394 #define BJACK_SELECT_NAME "OK"
395 #define BJACK_STAY_NAME "CANCEL"
396 #define BJACK_QUIT_NAME "REC"
397 #define BJACK_DOUBLE_NAME "MENU"
398 #define BJACK_SELECT BUTTON_OK
399 #define BJACK_QUIT BUTTON_REC
400 #define BJACK_STAY BUTTON_CANCEL
401 #define BJACK_DOUBLEDOWN BUTTON_MENU
402 #define BJACK_UP BUTTON_UP
403 #define BJACK_DOWN BUTTON_DOWN
404 #define BJACK_RIGHT BUTTON_NEXT
405 #define BJACK_LEFT BUTTON_PREV
407 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
408 #define BJACK_SELECT_NAME "SELECT"
409 #define BJACK_STAY_NAME "RIGHT"
410 #define BJACK_RESUME_NAME "PLAY"
411 #define BJACK_QUIT_NAME "REC+PLAY"
412 #define BJACK_DOUBLE_NAME "LEFT"
413 #define BJACK_SELECT BUTTON_SELECT
414 #define BJACK_QUIT (BUTTON_REC|BUTTON_PLAY)
415 #define BJACK_STAY BUTTON_VOL_UP
416 #define BJACK_DOUBLEDOWN BUTTON_VOL_DOWN
417 #define BJACK_UP BUTTON_PREV
418 #define BJACK_DOWN BUTTON_NEXT
419 #define BJACK_RIGHT BUTTON_VOL_UP
420 #define BJACK_LEFT BUTTON_VOL_DOWN
423 #error No keymap defined!
426 #ifdef HAVE_TOUCHSCREEN
427 #ifndef BJACK_DOUBLEDOWN
428 #define BJACK_DOUBLEDOWN BUTTON_MIDLEFT
429 #define BJACK_DOUBLE_NAME "BUTTON_MIDLEFT"
432 #define BJACK_SELECT BUTTON_CENTER
433 #define BJACK_SELECT_NAME "BUTTON_CENTER"
436 #define BJACK_MAX BUTTON_TOPRIGHT
439 #define BJACK_MIN BUTTON_TOPLEFT
442 #define BJACK_STAY BUTTON_BOTTOMLEFT
443 #define BJACK_STAY_NAME "BUTTON_BOTTOMLEFT"
446 #define BJACK_UP BUTTON_TOPMIDDLE
449 #define BJACK_DOWN BUTTON_BOTTOMMIDDLE
452 #define BJACK_RIGHT BUTTON_MIDRIGHT
455 #define BJACK_LEFT BUTTON_MIDLEFT
460 #ifdef HAVE_LCD_COLOR
461 #define BG_COLOR LCD_RGBPACK(0,157,0)
462 #define FG_COLOR LCD_WHITE
464 #define BG_COLOR LCD_WHITE
465 #define FG_COLOR LCD_BLACK
468 #define CARD_WIDTH BMPWIDTH_card_back
469 #define CARD_HEIGHT BMPHEIGHT_card_back
471 /* This is the max amount of cards onscreen before condensing */
472 #define MAX_CARDS LCD_WIDTH/(CARD_WIDTH+4)
474 extern const fb_data card_deck
[];
475 extern const fb_data card_back
[];
477 #define NEXT_CARD bj->player_cards[done][bj->num_player_cards[done]]
479 /* dealer and player card positions */
480 unsigned int dealer_x
, dealer_y
, player_x
, player_y
;
482 typedef struct card
{
483 unsigned int value
; /* Card's value in Blackjack */
484 unsigned int num
; /* Value on card face 0-12 (0=Ace, 1=2, 11=Q) */
485 unsigned int suit
; /* 0:Spades, 1:Hearts, 2: Clubs; 3: Diamonds */
489 typedef struct game_context
{
490 struct card player_cards
[2][22]; /* 22 Cards means the deal was all aces */
491 struct card dealer_cards
[22]; /* That is the worst-case scenario */
492 unsigned int player_total
;
493 unsigned int dealer_total
;
494 signed int player_money
;
495 unsigned int num_player_cards
[2];
496 unsigned int num_dealer_cards
;
497 unsigned int current_bet
;
498 unsigned int split_status
; /* 0 = split hasn't been asked, *
499 * 1 = split did not occur *
500 * 2 = split occurred *
501 * 3 = split occurred and 1st hand done */
504 bool asked_insurance
;
507 static bool resume
= false;
508 static bool resume_file
= false;
509 static struct highscore highscores
[NUM_SCORES
];
511 /*****************************************************************************
512 * blackjack_init() initializes blackjack data structures.
513 ******************************************************************************/
514 static void blackjack_init(struct game_context
* bj
) {
515 /* seed the rand generator */
516 rb
->srand(*rb
->current_tick
);
518 /* reset card positions */
520 dealer_y
= LCD_HEIGHT
/4 - CARD_HEIGHT
/2;
522 player_y
= LCD_HEIGHT
- LCD_HEIGHT
/4 - CARD_HEIGHT
/2;
524 /* check for resumed game */
528 bj
->player_total
= 0;
529 bj
->dealer_total
= 0;
530 bj
->num_player_cards
[0] = 2;
531 bj
->num_player_cards
[1] = 0;
532 bj
->num_dealer_cards
= 2;
533 bj
->end_hand
= false;
534 bj
->split_status
= 0;
535 bj
->is_blackjack
= false;
536 bj
->asked_insurance
= false;
539 /*****************************************************************************
540 * blackjack_drawtable() draws the table and some text.
541 ******************************************************************************/
542 static void blackjack_drawtable(struct game_context
* bj
) {
543 unsigned int w
, h
, y_loc
;
547 rb
->lcd_getstringsize("Bet", &w
, &h
);
548 rb
->lcd_putsxy(LCD_WIDTH
- w
, 2*h
+ 1, "Bet");
549 rb
->snprintf(str
, 9, "$%d", bj
->current_bet
);
550 rb
->lcd_getstringsize(str
, &w
, &h
);
551 rb
->lcd_putsxy(LCD_WIDTH
- w
, 3*h
+ 1, str
);
552 y_loc
= LCD_HEIGHT
/2;
554 rb
->lcd_getstringsize("Bet", &w
, &h
);
555 rb
->lcd_putsxy(LCD_WIDTH
- w
, 5*h
/ 2, "Bet");
556 rb
->snprintf(str
, 9, "$%d", bj
->current_bet
);
557 rb
->lcd_getstringsize(str
, &w
, &h
);
558 rb
->lcd_putsxy(LCD_WIDTH
- w
, 7*h
/ 2, str
);
559 rb
->lcd_hline(0, LCD_WIDTH
, LCD_HEIGHT
/2);
560 y_loc
= LCD_HEIGHT
/2 + h
;
563 rb
->lcd_putsxy(0,0, "Dealer");
564 rb
->lcd_getstringsize("Player", &w
, &h
);
565 rb
->lcd_putsxy(0, y_loc
, "Player");
566 rb
->lcd_getstringsize("Total", &w
, &h
);
567 rb
->lcd_putsxy(LCD_WIDTH
- w
, y_loc
, "Total");
568 rb
->lcd_getstringsize("Money", &w
, &h
);
569 rb
->lcd_putsxy(LCD_WIDTH
- w
, 0, "Money");
570 rb
->snprintf(str
, 9, "$%d", bj
->player_money
- bj
->current_bet
);
571 rb
->lcd_getstringsize(str
, &w
, &h
);
572 rb
->lcd_putsxy(LCD_WIDTH
- w
, h
+ 1, str
);
573 rb
->snprintf(str
, 3, "%d", bj
->player_total
);
574 rb
->lcd_getstringsize(str
, &w
, &h
);
575 rb
->lcd_putsxy(LCD_WIDTH
- w
, y_loc
+ h
, str
);
578 /*****************************************************************************
579 * find_value() is passed a card and returns its blackjack value.
580 ******************************************************************************/
581 static unsigned int find_value(unsigned int number
) {
582 unsigned int thisValue
;
584 thisValue
= 11; /* Aces get a value of 11 at first */
585 else if (number
< 10)
586 thisValue
= number
+ 1;
588 thisValue
= 10; /* Anything 10 or higher gets a value of 10 */
593 /*****************************************************************************
594 * draw_card() draws a card to the screen.
595 ******************************************************************************/
596 static void draw_card(struct card temp_card
, bool shown
,
597 unsigned int x
, unsigned int y
) {
599 rb
->lcd_bitmap_part(card_deck
, CARD_WIDTH
*temp_card
.num
,
600 CARD_HEIGHT
*temp_card
.suit
,
601 STRIDE( SCREEN_MAIN
, BMPWIDTH_card_deck
,
602 BMPHEIGHT_card_deck
),
603 x
+1, y
+1, CARD_WIDTH
, CARD_HEIGHT
);
605 rb
->lcd_bitmap(card_back
, x
+1, y
+1,CARD_WIDTH
, CARD_HEIGHT
);
607 rb
->lcd_set_foreground(LCD_BLACK
);
612 rb
->lcd_hline(x
+2, x
+CARD_WIDTH
-1, y
);
613 rb
->lcd_hline(x
+2, x
+CARD_WIDTH
-1, y
+CARD_HEIGHT
+1);
614 rb
->lcd_vline(x
, y
+2, y
+CARD_HEIGHT
-3);
615 rb
->lcd_vline(x
+CARD_WIDTH
+1, y
+2, y
+CARD_HEIGHT
-1);
616 rb
->lcd_drawpixel(x
+1, y
+1);
617 rb
->lcd_drawpixel(x
+1, y
+CARD_HEIGHT
);
618 rb
->lcd_drawpixel(x
+CARD_WIDTH
, y
+1);
619 rb
->lcd_drawpixel(x
+CARD_WIDTH
, y
+CARD_HEIGHT
);
621 rb
->lcd_hline(x
+1, x
+CARD_WIDTH
, y
);
622 rb
->lcd_hline(x
+1, x
+CARD_WIDTH
, y
+CARD_HEIGHT
+1);
623 rb
->lcd_vline(x
, y
+1, y
+CARD_HEIGHT
);
624 rb
->lcd_vline(x
+CARD_WIDTH
+1, y
+1, y
+CARD_HEIGHT
);
628 rb
->lcd_set_foreground(FG_COLOR
);
632 /*****************************************************************************
633 * new_card() initializes a new card and gives it values.
634 ******************************************************************************/
635 static struct card
new_card(void) {
636 struct card new_card
;
637 new_card
.suit
= rb
->rand()%4; /* Random number 0-3 */
638 new_card
.num
= rb
->rand()%13; /* Random number 0-12 */
639 new_card
.value
= find_value(new_card
.num
);
640 new_card
.is_soft_ace
= (new_card
.num
== 0);
644 /*****************************************************************************
645 * deal_init_card() deals and draws to the screen the player's and dealer's
647 ******************************************************************************/
648 static void deal_init_cards(struct game_context
* bj
) {
649 bj
->dealer_cards
[0] = new_card();
650 bj
->dealer_total
+= bj
->dealer_cards
[0].value
;
652 draw_card(bj
->dealer_cards
[0], false, dealer_x
, dealer_y
);
654 bj
->dealer_cards
[1] = new_card();
655 bj
->dealer_total
+= bj
->dealer_cards
[1].value
;
656 draw_card(bj
->dealer_cards
[1], true, dealer_x
+ CARD_WIDTH
+ 4, dealer_y
);
658 bj
->player_cards
[0][0] = new_card();
659 bj
->player_total
+= bj
->player_cards
[0][0].value
;
660 draw_card(bj
->player_cards
[0][0], true, player_x
, player_y
);
661 player_x
+= CARD_WIDTH
+ 4;
663 bj
->player_cards
[0][1] = new_card();
664 bj
->player_total
+= bj
->player_cards
[0][1].value
;
665 draw_card(bj
->player_cards
[0][1], true, player_x
, player_y
);
666 player_x
+= CARD_WIDTH
+ 4;
669 /*****************************************************************************
670 * redraw_board() redraws all the cards and the board
671 ******************************************************************************/
672 static void redraw_board(struct game_context
* bj
) {
673 unsigned int i
, n
, upper_bound
;
674 rb
->lcd_clear_display();
676 blackjack_drawtable(bj
);
679 upper_bound
= bj
->split_status
> 1 ? 2 : 1;
681 for (i
= 0; i
< bj
->num_dealer_cards
; i
++) {
683 draw_card(bj
->dealer_cards
[0], false, dealer_x
, dealer_y
);
685 /* increment i so the dealer's first card isn't displayed */
687 dealer_x
+= CARD_WIDTH
+ 4;
689 draw_card(bj
->dealer_cards
[i
], true, dealer_x
, dealer_y
);
691 if (bj
->num_dealer_cards
> MAX_CARDS
-1)
694 dealer_x
+= CARD_WIDTH
+ 4;
697 for (n
= 0; n
< upper_bound
; n
++) {
698 for (i
= 0; i
< bj
->num_player_cards
[n
]; i
++) {
699 draw_card(bj
->player_cards
[n
][i
], true, player_x
, player_y
);
700 if (bj
->split_status
>1 || bj
->num_player_cards
[n
]>MAX_CARDS
)
703 player_x
+= CARD_WIDTH
+ 4;
705 if (bj
->split_status
> 1)
706 player_x
= LCD_WIDTH
/2 + 4;
710 /*****************************************************************************
711 * update_total updates the player's total
712 ******************************************************************************/
713 static void update_total(struct game_context
* bj
) {
716 rb
->snprintf(total
, 3, "%d", bj
->player_total
);
717 rb
->lcd_getstringsize(total
, &w
, &h
);
721 rb
->lcd_putsxy(LCD_WIDTH
- w
, LCD_HEIGHT
/2 + h
, total
);
722 rb
->lcd_update_rect(LCD_WIDTH
- w
, LCD_HEIGHT
/2 + h
, w
, h
);
726 /*****************************************************************************
727 * check_for_aces() is passed an array of cards and returns where an ace is
728 * located. Otherwise, returns -1.
729 ******************************************************************************/
730 static signed int check_for_aces(struct card temp_cards
[], unsigned int size
) {
732 for(i
= 0; i
< size
; i
++) {
733 if (temp_cards
[i
].is_soft_ace
)
739 /*****************************************************************************
740 * check_totals() compares player and dealer totals.
741 * 0: bust 1: loss, 2: push, 3: win, 4: blackjack, 5: something's not right...
742 ******************************************************************************/
743 static unsigned int check_totals(struct game_context
* bj
) {
745 if (bj
->player_total
> 21)
747 else if (bj
->player_total
== 21 && bj
->is_blackjack
) {
748 if (bj
->dealer_total
== 21 && bj
->num_dealer_cards
== 2)
753 else if (bj
->player_total
== bj
->dealer_total
)
755 else if (bj
->dealer_total
> 21 && bj
->player_total
< 22)
757 else if (bj
->dealer_total
> bj
->player_total
)
759 else if (bj
->player_total
> bj
->dealer_total
)
767 /*****************************************************************************
768 * finish_dealer() draws cards for the dealer until he has 17 or more.
769 ******************************************************************************/
770 static void finish_dealer(struct game_context
* bj
) {
773 if (bj
->dealer_total
> 16 && bj
->dealer_total
< 22)
776 while (bj
->dealer_total
< 17) {
777 bj
->dealer_cards
[bj
->num_dealer_cards
] = new_card();
778 bj
->dealer_total
+= bj
->dealer_cards
[bj
->num_dealer_cards
].value
;
779 bj
->num_dealer_cards
++;
782 while (bj
->dealer_total
> 21) {
783 temp
= check_for_aces(bj
->dealer_cards
, bj
->num_dealer_cards
);
785 bj
->dealer_cards
[temp
].is_soft_ace
= false;
786 bj
->dealer_total
-= 10;
793 /*****************************************************************************
794 * finish_game() completes the game once player's turn is over.
795 ******************************************************************************/
796 static void finish_game(struct game_context
* bj
) {
797 unsigned int rValue
, w
, h
;
802 } while (bj
->dealer_total
< 17);
805 rValue
= check_totals(bj
);
808 rb
->snprintf(str
, sizeof(str
), " Bust! ");
809 bj
->player_money
-= bj
->current_bet
;
811 else if (rValue
== 1) {
812 rb
->snprintf(str
, sizeof(str
), " Sorry, you lost. ");
813 bj
->player_money
-= bj
->current_bet
;
815 else if (rValue
== 2) {
816 rb
->snprintf(str
, sizeof(str
), " Push ");
818 else if (rValue
== 3) {
819 rb
->snprintf(str
, sizeof(str
), " You won! ");
820 bj
->player_money
+= bj
->current_bet
;
823 rb
->snprintf(str
, sizeof(str
), " Blackjack! ");
824 bj
->player_money
+= bj
->current_bet
* 3 / 2;
826 rb
->lcd_getstringsize(str
, &w
, &h
);
829 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
830 rb
->lcd_fillrect(0, LCD_HEIGHT
/2, LCD_WIDTH
, LCD_HEIGHT
/2);
831 rb
->lcd_set_drawmode(DRMODE_SOLID
);
832 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + h
, str
);
833 rb
->snprintf(str
, 12, "You have %d", bj
->player_total
);
834 rb
->lcd_getstringsize(str
, &w
, &h
);
835 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2, str
);
837 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 - h
/2, str
);
842 /*****************************************************************************
843 * blackjack_loadgame() loads the saved game and returns load success.
844 ******************************************************************************/
845 static bool blackjack_loadgame(struct game_context
* bj
) {
850 fd
= rb
->open(SAVE_FILE
, O_RDONLY
);
851 if(fd
< 0) return false;
853 /* read in saved game */
854 if(rb
->read(fd
, bj
, sizeof(struct game_context
))
855 == (long)sizeof(struct game_context
))
865 /*****************************************************************************
866 * blackjack_savegame() saves the current game state.
867 ******************************************************************************/
868 static void blackjack_savegame(struct game_context
* bj
) {
873 /* write out the game state to the save file */
874 fd
= rb
->open(SAVE_FILE
, O_WRONLY
|O_CREAT
, 0666);
877 rb
->write(fd
, bj
, sizeof(struct game_context
));
881 /*****************************************************************************
882 * blackjack_get_yes_no() gets a yes/no answer from the user
883 ******************************************************************************/
884 static unsigned int blackjack_get_yes_no(char message
[20]) {
886 unsigned int w
, h
, b
, choice
= 0;
887 bool breakout
= false;
888 char message_yes
[24], message_no
[24];
890 rb
->strcpy(message_yes
, message
);
891 rb
->strcpy(message_no
, message
);
892 rb
->strcat(message_yes
, " Yes");
893 rb
->strcat(message_no
, " No");
894 rb
->lcd_getstringsize(message_yes
, &w
, &h
);
895 const char *stg
[] = {message_yes
, message_no
};
903 #ifdef HAVE_LCD_COLOR
904 rb
->lcd_fillrect(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
, w
+1, h
+3);
905 rb
->lcd_set_foreground(LCD_BLACK
);
906 rb
->lcd_set_background(LCD_WHITE
);
908 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
909 rb
->lcd_fillrect(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
, w
+1, h
+3);
910 rb
->lcd_set_drawmode(DRMODE_SOLID
);
912 rb
->lcd_drawrect(LCD_WIDTH
/2 - w
/2 - 1, LCD_HEIGHT
/2 + b
- 1, w
+3, h
+4);
915 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + b
+1, stg
[choice
]);
916 rb
->lcd_update_rect(LCD_WIDTH
/2 - w
/2 - 1, LCD_HEIGHT
/2 + b
-1,
918 button
= rb
->button_get(true);
922 case (BJACK_LEFT
|BUTTON_REPEAT
):
924 case (BJACK_RIGHT
|BUTTON_REPEAT
):
927 case BJACK_SELECT
: breakout
= true;
929 case BJACK_QUIT
: breakout
= true;
936 rb
->lcd_set_foreground(FG_COLOR
);
937 rb
->lcd_set_background(BG_COLOR
);
942 /*****************************************************************************
943 * blackjack_get_amount() gets an amount from the player to be used
944 ******************************************************************************/
945 static signed int blackjack_get_amount(char message
[20], signed int lower_limit
,
946 signed int upper_limit
,
950 bool breakout
= false, changed
= false;
954 rb
->lcd_getstringsize("A", &w
, &h
); /* find the size of one character */
956 if (start
> upper_limit
)
957 amount
= upper_limit
;
958 else if (start
< lower_limit
)
959 amount
= lower_limit
;
964 rb
->lcd_set_background(LCD_WHITE
);
965 rb
->lcd_set_foreground(LCD_BLACK
);
969 rb
->lcd_clear_display();
970 rb
->lcd_puts(0, 1, message
);
971 rb
->snprintf(str
, 9, "$%d", amount
);
972 rb
->lcd_puts(0, 2, str
);
973 rb
->lcd_puts(0, 3, "RIGHT: +1");
974 rb
->lcd_puts(0, 4, "LEFT: -1");
975 rb
->lcd_puts(0, 5, "UP: +10");
976 rb
->lcd_puts(0, 6, "DOWN: -10");
979 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
980 rb
->lcd_fillrect(LCD_WIDTH
/2 - 9*w
- 1, LCD_HEIGHT
/2 - 4*h
- 3,
982 rb
->lcd_set_drawmode(DRMODE_SOLID
);
983 rb
->lcd_drawrect(LCD_WIDTH
/2 - 9*w
- 1, LCD_HEIGHT
/2 - 4*h
- 3,
985 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 4*h
- 1, message
);
986 rb
->snprintf(str
, 9, "$%d", amount
);
987 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, str
);
988 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
989 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
990 (CONFIG_KEYPAD == IPOD_1G2G_PAD) || \
991 (CONFIG_KEYPAD == SANSA_E200_PAD) || \
992 (CONFIG_KEYPAD == SANSA_FUZE_PAD)
993 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, " >>|: +1");
994 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, " |<<: -1");
995 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "SCROLL+: +10");
996 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "SCROLL-: -10");
997 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
998 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, "RIGHT: +1");
999 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, "LEFT: -1");
1000 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "SCROLL+: +10");
1001 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "SCROLL-: -10");
1003 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - h
-2, "RIGHT: +1");
1004 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 1, "LEFT: -1");
1005 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + h
, "UP: +10");
1006 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 + 2*h
+ 1, "DOWN: -10");
1008 rb
->lcd_update_rect(LCD_WIDTH
/2 - 9*w
- 1, LCD_HEIGHT
/2 - 4*h
- 3,
1013 button
= rb
->button_get(true);
1017 case (BJACK_UP
|BUTTON_REPEAT
):
1018 if (amount
+ 10 < upper_limit
+ 1) {
1024 case (BJACK_DOWN
|BUTTON_REPEAT
):
1025 if (amount
- 10 > lower_limit
- 1) {
1031 case (BJACK_RIGHT
|BUTTON_REPEAT
):
1032 if (amount
+ 1 < upper_limit
+ 1) {
1038 case (BJACK_LEFT
|BUTTON_REPEAT
):
1039 if (amount
- 1 > lower_limit
- 1) {
1046 amount
= upper_limit
;
1052 amount
= lower_limit
;
1066 rb
->snprintf(str
, 9, "$%d", amount
);
1067 #if LCD_HEIGHT <= 64
1068 rb
->lcd_puts(0, 2, str
);
1071 rb
->lcd_set_drawmode(DRMODE_BG
+DRMODE_INVERSEVID
);
1072 rb
->lcd_fillrect(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, 5*w
, h
);
1073 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1074 rb
->lcd_putsxy(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, str
);
1075 rb
->lcd_update_rect(LCD_WIDTH
/2 - 9*w
, LCD_HEIGHT
/2 - 3*h
, 5*w
, h
);
1082 rb
->lcd_set_foreground(FG_COLOR
);
1083 rb
->lcd_set_background(BG_COLOR
);
1085 rb
->lcd_clear_display();
1089 /*****************************************************************************
1090 * blackjack_get_bet() gets the player's bet.
1091 ******************************************************************************/
1092 static void blackjack_get_bet(struct game_context
* bj
) {
1093 bj
->current_bet
= blackjack_get_amount("Please enter a bet", 10,
1094 bj
->player_money
, bj
->current_bet
);
1097 /*****************************************************************************
1098 * double_down() returns one final card then finishes the game
1099 ******************************************************************************/
1100 static void double_down(struct game_context
* bj
) {
1101 bj
->current_bet
*= 2;
1102 bj
->player_cards
[0][bj
->num_player_cards
[0]] = new_card();
1103 bj
->player_total
+= bj
->player_cards
[0][bj
->num_player_cards
[0]].value
;
1104 bj
->num_player_cards
[0]++;
1107 /*****************************************************************************
1108 * split() checks if the player wants to split and acts accordingly.
1109 * When bj->split_status is 1, no split occurred. 2 means the player split and 3
1110 * means a split has already occurred and the first hand is done.
1111 ******************************************************************************/
1112 static void split(struct game_context
* bj
) {
1113 if (blackjack_get_yes_no("Split?") != 0)
1114 bj
->split_status
= 1;
1116 bj
->split_status
= 2;
1117 bj
->current_bet
*= 2;
1118 bj
->num_player_cards
[0] = 1;
1119 bj
->num_player_cards
[1] = 1;
1120 bj
->player_cards
[1][0] = bj
->player_cards
[0][1];
1121 bj
->player_total
= bj
->player_cards
[0][0].value
;
1125 /*****************************************************************************
1126 * insurance() see if the player wants to buy insurance and how much.
1127 ******************************************************************************/
1128 static unsigned int insurance(struct game_context
* bj
) {
1129 unsigned int insurance
, max_amount
;
1131 insurance
= blackjack_get_yes_no("Buy Insurance?");
1132 bj
->asked_insurance
= true;
1133 max_amount
= bj
->current_bet
< (unsigned int)bj
->player_money
?
1134 bj
->current_bet
/2 : (unsigned int)bj
->player_money
;
1135 if (insurance
!= 0) return 0;
1137 insurance
= blackjack_get_amount("How much?", 0, max_amount
, 0);
1142 /*****************************************************************************
1143 * play_again() checks to see if the player wants to keep playing.
1144 ******************************************************************************/
1145 static unsigned int play_again(void) {
1146 return blackjack_get_yes_no("Play Again?");
1149 /*****************************************************************************
1150 * blackjack_help() displays help text.
1151 ******************************************************************************/
1152 static bool blackjack_help(void) {
1153 static char *help_text
[] = {
1154 "Blackjack", "", "Aim", "",
1155 "Try", "to", "get", "as", "close", "to", "21", "without", "going",
1156 "over", "or", "simply", "beat", "out", "the", "dealer", "for", "the",
1157 "best", "hand.", "", "",
1159 BJACK_SELECT_NAME
, ":", "hit", "/", "select", "",
1160 BJACK_STAY_NAME
, ":", "stay", "",
1161 BJACK_DOUBLE_NAME
, ":", "double", "down", "",
1162 BJACK_QUIT_NAME
, ":", "go", "to", "menu", "",
1164 static struct style_text formation
[]={
1165 { 0, TEXT_CENTER
|TEXT_UNDERLINE
},
1171 rb
->lcd_setfont(FONT_UI
);
1172 #ifdef HAVE_LCD_COLOR
1173 rb
->lcd_set_background(LCD_BLACK
);
1174 rb
->lcd_set_foreground(LCD_WHITE
);
1176 if (display_text(ARRAYLEN(help_text
), help_text
, formation
, NULL
, true))
1178 rb
->lcd_setfont(FONT_SYSFIXED
);
1183 static int blackjack_menu_cb(int action
, const struct menu_item_ex
*this_item
)
1185 int i
= ((intptr_t)this_item
);
1186 if(action
== ACTION_REQUEST_MENUITEM
1187 && !resume
&& (i
==0 || i
==5))
1188 return ACTION_EXIT_MENUITEM
;
1192 /*****************************************************************************
1193 * blackjack_menu() is the initial menu at the start of the game.
1194 ******************************************************************************/
1195 static unsigned int blackjack_menu(void) {
1197 bool breakout
= false;
1199 MENUITEM_STRINGLIST(menu
, "BlackJack Menu", blackjack_menu_cb
,
1200 "Resume Game", "Start New Game",
1201 "High Scores", "Help",
1203 "Quit without Saving", "Quit");
1206 switch(rb
->do_menu(&menu
, &selection
, NULL
, false)) {
1210 rb
->remove(SAVE_FILE
);
1211 resume_file
= false;
1218 highscore_show(-1, highscores
, NUM_SCORES
, false);
1221 if(blackjack_help())
1225 if (playback_control(NULL
))
1229 return BJ_QUIT_WITHOUT_SAVING
;
1234 return BJ_QUIT_WITHOUT_SAVING
;
1236 case MENU_ATTACHED_USB
:
1247 /*****************************************************************************
1248 * blackjack() is the main game subroutine, it returns the final game status.
1249 ******************************************************************************/
1250 static int blackjack(struct game_context
* bj
) {
1252 unsigned int w
, h
, temp_var
, done
= 0, todo
= 1;
1254 bool breakout
= false;
1255 bool dbl_down
= false;
1257 /********************
1259 ********************/
1260 temp_var
= blackjack_menu();
1265 rb
->lcd_set_background(BG_COLOR
);
1266 rb
->lcd_set_foreground(FG_COLOR
);
1269 /********************
1271 ********************/
1274 /********************
1276 ********************/
1278 resume_file
= false;
1279 /* check for resumed game */
1283 if (bj
->split_status
== 2) {
1285 player_x
= bj
->num_player_cards
[0] * 10 + 4;
1287 else if (bj
->split_status
== 3) {
1288 player_x
= bj
->num_player_cards
[1] * 10 + LCD_WIDTH
/2 + 4;
1294 bj
->player_money
= 1000;
1295 bj
->current_bet
= 10;
1296 blackjack_get_bet(bj
);
1297 if (bj
->current_bet
== 0)
1299 rb
->lcd_clear_display();
1300 deal_init_cards(bj
);
1301 blackjack_drawtable(bj
);
1309 if(bj
->player_total
== 21 && bj
->num_player_cards
[0] == 2) {
1310 bj
->is_blackjack
= true;
1311 bj
->end_hand
= true;
1314 else if(bj
->dealer_cards
[1].is_soft_ace
&& !breakout
&&
1315 !bj
->asked_insurance
) {
1316 temp_var
= insurance(bj
);
1317 if (bj
->dealer_total
== 21) {
1318 rb
->splash(HZ
, "Dealer has blackjack");
1319 bj
->player_money
+= temp_var
;
1320 bj
->end_hand
= true;
1326 rb
->splash(HZ
, "Dealer does not have blackjack");
1327 bj
->player_money
-= temp_var
;
1333 if(!bj
->end_hand
&& bj
->split_status
== 0 &&
1334 bj
->player_cards
[0][0].num
== bj
->player_cards
[0][1].num
) {
1337 rb
->lcd_update_rect(0, LCD_HEIGHT
/2, LCD_WIDTH
, LCD_HEIGHT
/2);
1338 if (bj
->split_status
== 2) {
1340 player_x
= bj
->num_player_cards
[0] * 10 + 4;
1344 while(!bj
->end_hand
&& done
< todo
) {
1345 button
= rb
->button_get(true);
1349 NEXT_CARD
= new_card();
1350 bj
->player_total
+= NEXT_CARD
.value
;
1351 draw_card(NEXT_CARD
, true, player_x
, player_y
);
1352 bj
->num_player_cards
[done
]++;
1353 if (bj
->num_player_cards
[done
] == MAX_CARDS
+ 1) {
1355 rb
->lcd_update_rect(0, LCD_HEIGHT
/2, LCD_WIDTH
,
1358 else if (bj
->num_player_cards
[done
]>MAX_CARDS
|| todo
> 1) {
1359 rb
->lcd_update_rect(player_x
, player_y
, CARD_WIDTH
+2,
1364 rb
->lcd_update_rect(player_x
, player_y
, CARD_WIDTH
+2,
1366 player_x
+= CARD_WIDTH
+ 4;
1372 bj
->end_hand
= true;
1374 case BJACK_DOUBLEDOWN
:
1375 if ((signed int)bj
->current_bet
* 2 <
1376 bj
->player_money
+ 1 &&
1377 bj
->num_player_cards
[0]==2 && todo
==1) {
1380 if (bj
->player_total
< 22) {
1381 bj
->end_hand
= true;
1385 else if((signed int)bj
->current_bet
* 2 >
1387 rb
->splash(HZ
, "Not enough money to double down.");
1398 while (bj
->player_total
> 21 && !bj
->end_hand
) {
1399 temp
= check_for_aces(bj
->player_cards
[done
],
1400 bj
->num_player_cards
[done
]);
1402 bj
->player_cards
[done
][temp
].is_soft_ace
= false;
1403 bj
->player_total
-= 10;
1406 bj
->end_hand
= true;
1411 bj
->end_hand
= true;
1418 temp
= bj
->player_total
;
1419 bj
->player_total
= temp_var
;
1422 rb
->lcd_getstringsize(" Split 1 ", &w
, &h
);
1423 rb
->lcd_putsxy(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1425 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1427 bj
->current_bet
/= 2;
1428 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1431 bj
->player_total
= temp_var
;
1433 rb
->lcd_getstringsize(" Split 2 ", &w
, &h
);
1434 rb
->lcd_putsxy(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1436 rb
->lcd_update_rect(LCD_WIDTH
/2-w
/2, LCD_HEIGHT
/2-3*h
/2,
1441 bj
->end_hand
= false;
1442 bj
->split_status
= 3;
1443 temp_var
= bj
->player_total
;
1444 bj
->player_total
= bj
->player_cards
[1][0].value
;
1456 if (bj
->player_money
< 10) {
1461 if (bj
->end_hand
) { /* If hand is over */
1462 if (play_again() != 0) /* User wants to quit */
1464 else { /* User keeps playing */
1466 temp
= bj
->current_bet
;
1467 bj
->current_bet
= 0;
1470 bj
->current_bet
= temp
;
1472 bj
->current_bet
/= 2;
1478 blackjack_get_bet(bj
);
1479 if (bj
->current_bet
== 0)
1481 deal_init_cards(bj
);
1482 blackjack_drawtable(bj
);
1491 /*****************************************************************************
1492 * plugin entry point.
1493 ******************************************************************************/
1494 enum plugin_status
plugin_start(const void* parameter
)
1496 struct game_context bj
;
1502 rb
->lcd_set_backdrop(NULL
);
1505 /* load high scores */
1506 highscore_load(SCORE_FILE
, highscores
, NUM_SCORES
);
1507 resume
= blackjack_loadgame(&bj
);
1508 resume_file
= resume
;
1510 rb
->lcd_setfont(FONT_SYSFIXED
);
1513 switch(blackjack(&bj
)){
1515 rb
->splash(HZ
, "Not enough money to continue");
1516 /* fall through to BJ_END */
1519 if(!resume
&& bj
.player_money
> 10) {
1520 /* There is no level, so store -1 to blank column */
1521 int position
= highscore_update(bj
.player_money
, -1, "",
1522 highscores
, NUM_SCORES
);
1526 rb
->splash(HZ
*2, "New High Score");
1527 highscore_show(position
, highscores
, NUM_SCORES
, false);
1533 highscore_save(SCORE_FILE
, highscores
, NUM_SCORES
);
1534 return PLUGIN_USB_CONNECTED
;
1537 rb
->splash(HZ
*1, "Saving game...");
1538 blackjack_savegame(&bj
);
1541 case BJ_QUIT_WITHOUT_SAVING
:
1549 highscore_save(SCORE_FILE
, highscores
, NUM_SCORES
);