Call the oddly labelled button on the gigabeat remote "Menu" in the
[kugel-rb.git] / apps / plugins / blackjack.c
blobf46d378f2bb3d82ce12e6291d6579b36fc926242
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "plugin.h"
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"
29 PLUGIN_HEADER
31 /* save files */
32 #define HIGH_SCORE PLUGIN_GAMES_DIR "/blackjack.score"
33 #define SAVE_FILE PLUGIN_GAMES_DIR "/blackjack.save"
34 #define NUM_SCORES 5
36 /* final game return status */
37 enum {
38 BJ_LOSE,
39 BJ_QUIT,
40 BJ_SAVE_AND_QUIT,
41 BJ_USB,
42 BJ_END,
45 #if CONFIG_KEYPAD == RECORDER_PAD
46 #define BJACK_SELECT_NAME "PLAY"
47 #define BJACK_STAY_NAME "F1"
48 #define BJACK_RESUME_NAME "F3"
49 #define BJACK_QUIT_NAME "OFF"
50 #define BJACK_DOUBLE_NAME "F2"
51 #define BJACK_SELECT BUTTON_PLAY
52 #define BJACK_QUIT BUTTON_OFF
53 #define BJACK_MAX (BUTTON_ON|BUTTON_UP)
54 #define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
55 #define BJACK_STAY BUTTON_F1
56 #define BJACK_DOUBLEDOWN BUTTON_F2
57 #define BJACK_RESUME BUTTON_F3
58 #define BJACK_UP BUTTON_UP
59 #define BJACK_DOWN BUTTON_DOWN
60 #define BJACK_RIGHT BUTTON_RIGHT
61 #define BJACK_LEFT BUTTON_LEFT
63 #elif CONFIG_KEYPAD == ONDIO_PAD
64 #define BJACK_SELECT_NAME "MENU"
65 #define BJACK_STAY_NAME "RIGHT"
66 #define BJACK_RESUME_NAME "DOWN"
67 #define BJACK_QUIT_NAME "OFF"
68 #define BJACK_DOUBLE_NAME "UP"
69 #define BJACK_SELECT BUTTON_MENU
70 #define BJACK_QUIT BUTTON_OFF
71 #define BJACK_MAX (BUTTON_MENU|BUTTON_UP)
72 #define BJACK_MIN (BUTTON_MENU|BUTTON_DOWN)
73 #define BJACK_STAY BUTTON_RIGHT
74 #define BJACK_DOUBLEDOWN BUTTON_UP
75 #define BJACK_RESUME BUTTON_DOWN
76 #define BJACK_UP BUTTON_UP
77 #define BJACK_DOWN BUTTON_DOWN
78 #define BJACK_RIGHT BUTTON_RIGHT
79 #define BJACK_LEFT BUTTON_LEFT
81 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
82 #define BJACK_SELECT_NAME "PLAY"
83 #define BJACK_STAY_NAME ">>|"
84 #define BJACK_RESUME_NAME "RIGHT"
85 #define BJACK_QUIT_NAME "POWER"
86 #define BJACK_DOUBLE_NAME "|<<"
87 #define BJACK_SELECT BUTTON_PLAY
88 #define BJACK_QUIT BUTTON_POWER
89 #define BJACK_MAX (BUTTON_PLAY|BUTTON_SCROLL_UP)
90 #define BJACK_MIN (BUTTON_PLAY|BUTTON_SCROLL_DOWN)
91 #define BJACK_STAY BUTTON_FF
92 #define BJACK_DOUBLEDOWN BUTTON_REW
93 #define BJACK_RESUME BUTTON_RIGHT
94 #define BJACK_UP BUTTON_SCROLL_UP
95 #define BJACK_DOWN BUTTON_SCROLL_DOWN
96 #define BJACK_RIGHT BUTTON_RIGHT
97 #define BJACK_LEFT BUTTON_LEFT
99 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
100 (CONFIG_KEYPAD == IRIVER_H300_PAD)
101 #define BJACK_SELECT_NAME "ON"
102 #define BJACK_STAY_NAME "REC"
103 #define BJACK_RESUME_NAME "MODE"
104 #define BJACK_QUIT_NAME "OFF"
105 #define BJACK_DOUBLE_NAME "SELECT"
106 #define BJACK_SELECT BUTTON_ON
107 #define BJACK_QUIT BUTTON_OFF
108 #define BJACK_MAX (BUTTON_ON|BUTTON_UP)
109 #define BJACK_MIN (BUTTON_ON|BUTTON_DOWN)
110 #define BJACK_STAY BUTTON_REC
111 #define BJACK_DOUBLEDOWN BUTTON_SELECT
112 #define BJACK_RESUME BUTTON_MODE
113 #define BJACK_UP BUTTON_UP
114 #define BJACK_DOWN BUTTON_DOWN
115 #define BJACK_RIGHT BUTTON_RIGHT
116 #define BJACK_LEFT BUTTON_LEFT
118 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
119 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
120 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
121 #define BJACK_SELECT_NAME "SELECT"
122 #define BJACK_STAY_NAME "RIGHT"
123 #define BJACK_RESUME_NAME "PLAY"
124 #define BJACK_QUIT_NAME "MENU"
125 #define BJACK_DOUBLE_NAME "LEFT"
126 #define BJACK_SELECT BUTTON_SELECT
127 #define BJACK_QUIT BUTTON_MENU
128 #define BJACK_MAX (BUTTON_SELECT|BUTTON_SCROLL_FWD)
129 #define BJACK_MIN (BUTTON_SELECT|BUTTON_SCROLL_BACK)
130 #define BJACK_STAY BUTTON_RIGHT
131 #define BJACK_DOUBLEDOWN BUTTON_LEFT
132 #define BJACK_RESUME BUTTON_PLAY
133 #define BJACK_UP BUTTON_SCROLL_FWD
134 #define BJACK_DOWN BUTTON_SCROLL_BACK
135 #define BJACK_RIGHT BUTTON_RIGHT
136 #define BJACK_LEFT BUTTON_LEFT
138 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
139 #define BJACK_SELECT_NAME "PLAY"
140 #define BJACK_STAY_NAME "REC"
141 #define BJACK_RESUME_NAME "DOWN"
142 #define BJACK_QUIT_NAME "POWER"
143 #define BJACK_DOUBLE_NAME "PLAY"
144 #define BJACK_SELECT BUTTON_SELECT
145 #define BJACK_QUIT BUTTON_POWER
146 #define BJACK_MAX (BUTTON_PLAY|BUTTON_UP)
147 #define BJACK_MIN (BUTTON_PLAY|BUTTON_DOWN)
148 #define BJACK_STAY BUTTON_REC
149 #define BJACK_DOUBLEDOWN BUTTON_PLAY
150 #define BJACK_RESUME BUTTON_DOWN
151 #define BJACK_UP BUTTON_UP
152 #define BJACK_DOWN BUTTON_DOWN
153 #define BJACK_RIGHT BUTTON_RIGHT
154 #define BJACK_LEFT BUTTON_LEFT
156 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
157 #define BJACK_SELECT_NAME "MODE"
158 #define BJACK_STAY_NAME "MODE"
159 #define BJACK_RESUME_NAME "EQ+MODE"
160 #define BJACK_QUIT_NAME "PLAY"
161 #define BJACK_DOUBLE_NAME "SELECT"
162 #define BJACK_SELECT BUTTON_MODE
163 #define BJACK_QUIT BUTTON_PLAY
164 #define BJACK_MAX (BUTTON_EQ|BUTTON_UP)
165 #define BJACK_MIN (BUTTON_EQ|BUTTON_DOWN)
166 #define BJACK_STAY BUTTON_MODE
167 #define BJACK_DOUBLEDOWN BUTTON_SELECT
168 #define BJACK_RESUME (BUTTON_EQ|BUTTON_MODE)
169 #define BJACK_UP BUTTON_UP
170 #define BJACK_DOWN BUTTON_DOWN
171 #define BJACK_RIGHT BUTTON_RIGHT
172 #define BJACK_LEFT BUTTON_LEFT
174 #elif CONFIG_KEYPAD == GIGABEAT_PAD
175 #define BJACK_SELECT_NAME "A"
176 #define BJACK_STAY_NAME "VOL-"
177 #define BJACK_RESUME_NAME "MENU"
178 #define BJACK_QUIT_NAME "POWER"
179 #define BJACK_DOUBLE_NAME "SELECT"
180 #define BJACK_SELECT BUTTON_A
181 #define BJACK_QUIT BUTTON_POWER
182 #define BJACK_MAX BUTTON_VOL_UP
183 #define BJACK_MIN BUTTON_VOL_DOWN
184 #define BJACK_STAY BUTTON_VOL_DOWN
185 #define BJACK_DOUBLEDOWN BUTTON_SELECT
186 #define BJACK_RESUME BUTTON_MENU
187 #define BJACK_UP BUTTON_UP
188 #define BJACK_DOWN BUTTON_DOWN
189 #define BJACK_RIGHT BUTTON_RIGHT
190 #define BJACK_LEFT BUTTON_LEFT
192 #elif CONFIG_KEYPAD == SANSA_E200_PAD
193 #define BJACK_SELECT_NAME "SELECT"
194 #define BJACK_STAY_NAME "RIGHT"
195 #define BJACK_RESUME_NAME "DOWN"
196 #define BJACK_QUIT_NAME "POWER"
197 #define BJACK_DOUBLE_NAME "LEFT"
198 #define BJACK_SELECT BUTTON_SELECT
199 #define BJACK_QUIT BUTTON_POWER
200 #define BJACK_MAX (BUTTON_REC|BUTTON_UP)
201 #define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
202 #define BJACK_STAY BUTTON_RIGHT
203 #define BJACK_DOUBLEDOWN BUTTON_LEFT
204 #define BJACK_RESUME BUTTON_DOWN
205 #define BJACK_UP BUTTON_SCROLL_FWD
206 #define BJACK_DOWN BUTTON_SCROLL_BACK
207 #define BJACK_RIGHT BUTTON_RIGHT
208 #define BJACK_LEFT BUTTON_LEFT
210 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
211 #define BJACK_SELECT_NAME "SELECT"
212 #define BJACK_STAY_NAME "RIGHT"
213 #define BJACK_RESUME_NAME "UP"
214 #define BJACK_QUIT_NAME "HOME"
215 #define BJACK_DOUBLE_NAME "LEFT"
216 #define BJACK_SELECT BUTTON_SELECT
217 #define BJACK_QUIT (BUTTON_HOME|BUTTON_REPEAT)
218 #define BJACK_MAX (BUTTON_SELECT|BUTTON_UP)
219 #define BJACK_MIN (BUTTON_SELECT|BUTTON_DOWN)
220 #define BJACK_STAY BUTTON_RIGHT
221 #define BJACK_DOUBLEDOWN BUTTON_LEFT
222 #define BJACK_RESUME BUTTON_UP
223 #define BJACK_UP BUTTON_SCROLL_FWD
224 #define BJACK_DOWN BUTTON_SCROLL_BACK
225 #define BJACK_RIGHT BUTTON_RIGHT
226 #define BJACK_LEFT BUTTON_LEFT
228 #elif CONFIG_KEYPAD == SANSA_C200_PAD
229 #define BJACK_SELECT_NAME "SELECT"
230 #define BJACK_STAY_NAME "RIGHT"
231 #define BJACK_RESUME_NAME "DOWN"
232 #define BJACK_QUIT_NAME "POWER"
233 #define BJACK_DOUBLE_NAME "LEFT"
234 #define BJACK_SELECT BUTTON_SELECT
235 #define BJACK_QUIT BUTTON_POWER
236 #define BJACK_MAX BUTTON_VOL_UP
237 #define BJACK_MIN BUTTON_VOL_DOWN
238 #define BJACK_STAY BUTTON_RIGHT
239 #define BJACK_DOUBLEDOWN BUTTON_LEFT
240 #define BJACK_RESUME BUTTON_DOWN
241 #define BJACK_UP BUTTON_UP
242 #define BJACK_DOWN BUTTON_DOWN
243 #define BJACK_RIGHT BUTTON_RIGHT
244 #define BJACK_LEFT BUTTON_LEFT
246 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
247 #define BJACK_SELECT_NAME "SELECT"
248 #define BJACK_STAY_NAME "RIGHT"
249 #define BJACK_RESUME_NAME "DOWN"
250 #define BJACK_QUIT_NAME "POWER"
251 #define BJACK_DOUBLE_NAME "LEFT"
252 #define BJACK_SELECT BUTTON_SELECT
253 #define BJACK_QUIT BUTTON_POWER
254 #define BJACK_MAX BUTTON_VOL_UP
255 #define BJACK_MIN BUTTON_VOL_DOWN
256 #define BJACK_STAY BUTTON_RIGHT
257 #define BJACK_DOUBLEDOWN BUTTON_LEFT
258 #define BJACK_RESUME BUTTON_DOWN
259 #define BJACK_UP BUTTON_UP
260 #define BJACK_DOWN BUTTON_DOWN
261 #define BJACK_RIGHT BUTTON_RIGHT
262 #define BJACK_LEFT BUTTON_LEFT
264 #elif CONFIG_KEYPAD == SANSA_M200_PAD
265 #define BJACK_SELECT_NAME "SELECT"
266 #define BJACK_STAY_NAME "RIGHT"
267 #define BJACK_RESUME_NAME "DOWN"
268 #define BJACK_QUIT_NAME "POWER"
269 #define BJACK_DOUBLE_NAME "LEFT"
270 #define BJACK_SELECT (BUTTON_SELECT | BUTTON_REL)
271 #define BJACK_QUIT BUTTON_POWER
272 #define BJACK_MAX BUTTON_VOL_UP
273 #define BJACK_MIN BUTTON_VOL_DOWN
274 #define BJACK_STAY BUTTON_RIGHT
275 #define BJACK_DOUBLEDOWN BUTTON_LEFT
276 #define BJACK_RESUME BUTTON_DOWN
277 #define BJACK_UP BUTTON_UP
278 #define BJACK_DOWN BUTTON_DOWN
279 #define BJACK_RIGHT BUTTON_RIGHT
280 #define BJACK_LEFT BUTTON_LEFT
282 #elif CONFIG_KEYPAD == ELIO_TPJ1022_PAD
283 #define BJACK_SELECT_NAME "MAIN"
284 #define BJACK_STAY_NAME "MENU"
285 #define BJACK_RESUME_NAME ">>|"
286 #define BJACK_QUIT_NAME "POWER"
287 #define BJACK_DOUBLE_NAME "DOWN"
288 #define BJACK_SELECT BUTTON_MAIN
289 #define BJACK_QUIT BUTTON_POWER
290 #define BJACK_MAX (BUTTON_REC|BUTTON_UP)
291 #define BJACK_MIN (BUTTON_REC|BUTTON_DOWN)
292 #define BJACK_STAY BUTTON_MENU
293 #define BJACK_DOUBLEDOWN BUTTON_DOWN
294 #define BJACK_RESUME BUTTON_FF
295 #define BJACK_UP BUTTON_UP
296 #define BJACK_DOWN BUTTON_DOWN
297 #define BJACK_RIGHT BUTTON_RIGHT
298 #define BJACK_LEFT BUTTON_LEFT
300 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
301 #define BJACK_SELECT_NAME "PLAY"
302 #define BJACK_STAY_NAME "VOL-"
303 #define BJACK_RESUME_NAME "MENU"
304 #define BJACK_QUIT_NAME "BACK"
305 #define BJACK_DOUBLE_NAME "SELECT"
306 #define BJACK_SELECT BUTTON_PLAY
307 #define BJACK_QUIT BUTTON_BACK
308 #define BJACK_MAX BUTTON_VOL_UP
309 #define BJACK_MIN BUTTON_VOL_DOWN
310 #define BJACK_STAY BUTTON_VOL_DOWN
311 #define BJACK_DOUBLEDOWN BUTTON_SELECT
312 #define BJACK_RESUME BUTTON_MENU
313 #define BJACK_UP BUTTON_UP
314 #define BJACK_DOWN BUTTON_DOWN
315 #define BJACK_RIGHT BUTTON_RIGHT
316 #define BJACK_LEFT BUTTON_LEFT
318 #elif CONFIG_KEYPAD == MROBE100_PAD
319 #define BJACK_SELECT_NAME "SELECT"
320 #define BJACK_STAY_NAME "DISPLAY"
321 #define BJACK_RESUME_NAME "PLAY"
322 #define BJACK_QUIT_NAME "POWER"
323 #define BJACK_DOUBLE_NAME "DOWN"
324 #define BJACK_SELECT BUTTON_SELECT
325 #define BJACK_QUIT BUTTON_POWER
326 #define BJACK_MAX BUTTON_MENU
327 #define BJACK_MIN BUTTON_DISPLAY
328 #define BJACK_STAY BUTTON_DISPLAY
329 #define BJACK_DOUBLEDOWN BUTTON_DOWN
330 #define BJACK_RESUME BUTTON_PLAY
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 == IAUDIO_M3_PAD
337 #define BJACK_SELECT_NAME "RC","PLAY"
338 #define BJACK_STAY_NAME "RC", ">>|"
339 #define BJACK_RESUME_NAME "RC_MODE"
340 #define BJACK_QUIT_NAME "RC_REC"
341 #define BJACK_DOUBLE_NAME "RC_REW"
342 #define BJACK_SELECT BUTTON_RC_PLAY
343 #define BJACK_QUIT BUTTON_RC_REC
344 #define BJACK_MAX (BUTTON_RC_PLAY|BUTTON_RC_VOL_UP)
345 #define BJACK_MIN (BUTTON_RC_PLAY|BUTTON_RC_VOL_DOWN)
346 #define BJACK_STAY BUTTON_RC_FF
347 #define BJACK_DOUBLEDOWN BUTTON_RC_REW
348 #define BJACK_RESUME BUTTON_RC_MODE
349 #define BJACK_UP BUTTON_RC_VOL_UP
350 #define BJACK_DOWN BUTTON_RC_VOL_DOWN
351 #define BJACK_RIGHT BUTTON_RC_FF
352 #define BJACK_LEFT BUTTON_RC_REW
354 #elif CONFIG_KEYPAD == COWOND2_PAD
355 #define BJACK_QUIT_NAME "POWER"
356 #define BJACK_DOUBLE_NAME "-"
357 #define BJACK_QUIT BUTTON_POWER
358 #define BJACK_DOUBLEDOWN BUTTON_MINUS
360 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
361 #define BJACK_SELECT_NAME "SELECT"
362 #define BJACK_STAY_NAME "PLAY"
363 #define BJACK_RESUME_NAME "MENU"
364 #define BJACK_QUIT_NAME "POWER"
365 #define BJACK_DOUBLE_NAME "CUSTOM"
366 #define BJACK_SELECT BUTTON_SELECT
367 #define BJACK_QUIT BUTTON_POWER
368 #define BJACK_STAY BUTTON_PLAY
369 #define BJACK_MAX (BUTTON_CUSTOM|BUTTON_UP)
370 #define BJACK_MIN (BUTTON_CUSTOM|BUTTON_DOWN)
371 #define BJACK_DOUBLEDOWN BUTTON_CUSTOM
372 #define BJACK_RESUME BUTTON_MENU
373 #define BJACK_UP BUTTON_UP
374 #define BJACK_DOWN BUTTON_DOWN
375 #define BJACK_RIGHT BUTTON_RIGHT
376 #define BJACK_LEFT BUTTON_LEFT
378 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
379 #define BJACK_SELECT_NAME "MENU"
380 #define BJACK_STAY_NAME "VOL-"
381 #define BJACK_RESUME_NAME "VIEW"
382 #define BJACK_QUIT_NAME "POWER"
383 #define BJACK_DOUBLE_NAME "SELECT"
384 #define BJACK_SELECT BUTTON_MENU
385 #define BJACK_QUIT BUTTON_POWER
386 #define BJACK_MAX BUTTON_VOL_UP
387 #define BJACK_MIN BUTTON_VOL_DOWN
388 #define BJACK_STAY BUTTON_VOL_DOWN
389 #define BJACK_DOUBLEDOWN BUTTON_SELECT
390 #define BJACK_RESUME BUTTON_VIEW
391 #define BJACK_UP BUTTON_UP
392 #define BJACK_DOWN BUTTON_DOWN
393 #define BJACK_RIGHT BUTTON_RIGHT
394 #define BJACK_LEFT BUTTON_LEFT
396 #elif CONFIG_KEYPAD == ONDAVX747_PAD
397 #define BJACK_QUIT_NAME "POWER"
398 #define BJACK_DOUBLE_NAME "Vol-"
399 #define BJACK_QUIT BUTTON_POWER
400 #define BJACK_DOUBLEDOWN BUTTON_VOL_DOWN
402 #elif CONFIG_KEYPAD == MROBE500_PAD
403 #define BJACK_QUIT_NAME "POWER"
404 #define BJACK_QUIT BUTTON_POWER
406 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
407 #define BJACK_SELECT_NAME ""
408 #define BJACK_STAY_NAME "RECORD"
409 #define BJACK_RESUME_NAME "PREVIOUS"
410 #define BJACK_QUIT_NAME "PLAY+LEFT"
411 #define BJACK_DOUBLE_NAME "FFWD"
412 #define BJACK_SELECT BUTTON_PLAY
413 #define BJACK_QUIT (BUTTON_PLAY|BUTTON_LEFT)
414 #define BJACK_MAX (BUTTON_PLAY|BUTTON_UP)
415 #define BJACK_MIN (BUTTON_PLAY|BUTTON_DOWN)
416 #define BJACK_STAY BUTTON_REC
417 #define BJACK_DOUBLEDOWN BUTTON_FFWD
418 #define BJACK_RESUME BUTTON_REW
419 #define BJACK_UP BUTTON_UP
420 #define BJACK_DOWN BUTTON_DOWN
421 #define BJACK_RIGHT BUTTON_RIGHT
422 #define BJACK_LEFT BUTTON_LEFT
424 #else
425 #error No keymap defined!
426 #endif
428 #ifdef HAVE_TOUCHSCREEN
429 #ifndef BJACK_DOUBLEDOWN
430 #define BJACK_DOUBLEDOWN BUTTON_MIDLEFT
431 #define BJACK_DOUBLE_NAME "BUTTON_MIDLEFT"
432 #endif
433 #ifndef BJACK_SELECT
434 #define BJACK_SELECT BUTTON_CENTER
435 #define BJACK_SELECT_NAME "BUTTON_CENTER"
436 #endif
437 #ifndef BJACK_MAX
438 #define BJACK_MAX BUTTON_TOPRIGHT
439 #endif
440 #ifndef BJACK_MIN
441 #define BJACK_MIN BUTTON_TOPLEFT
442 #endif
443 #ifndef BJACK_RESUME
444 #define BJACK_RESUME BUTTON_BOTTOMRIGHT
445 #define BJACK_RESUME_NAME "BUTTON_BOTTOMRIGHT"
446 #endif
447 #ifndef BJACK_STAY
448 #define BJACK_STAY BUTTON_BOTTOMLEFT
449 #define BJACK_STAY_NAME "BUTTON_BOTTOMLEFT"
450 #endif
451 #ifndef BJACK_UP
452 #define BJACK_UP BUTTON_TOPMIDDLE
453 #endif
454 #ifndef BJACK_DOWN
455 #define BJACK_DOWN BUTTON_BOTTOMMIDDLE
456 #endif
457 #ifndef BJACK_RIGHT
458 #define BJACK_RIGHT BUTTON_MIDRIGHT
459 #endif
460 #ifndef BJACK_LEFT
461 #define BJACK_LEFT BUTTON_MIDLEFT
462 #endif
464 #endif
466 #ifdef HAVE_LCD_COLOR
467 #define BG_COLOR LCD_RGBPACK(0,157,0)
468 #define FG_COLOR LCD_WHITE
469 #elif LCD_DEPTH > 1
470 #define BG_COLOR LCD_WHITE
471 #define FG_COLOR LCD_BLACK
472 #endif
474 #define CARD_WIDTH BMPWIDTH_card_back
475 #define CARD_HEIGHT BMPHEIGHT_card_back
477 /* This is the max amount of cards onscreen before condensing */
478 #define MAX_CARDS LCD_WIDTH/(CARD_WIDTH+4)
480 extern const fb_data card_deck[];
481 extern const fb_data card_back[];
483 #define NEXT_CARD bj->player_cards[done][bj->num_player_cards[done]]
485 /* dealer and player card positions */
486 unsigned int dealer_x, dealer_y, player_x, player_y;
488 typedef struct card {
489 unsigned int value; /* Card's value in Blackjack */
490 unsigned int num; /* Value on card face 0-12 (0=Ace, 1=2, 11=Q) */
491 unsigned int suit; /* 0:Spades, 1:Hearts, 2: Clubs; 3: Diamonds */
492 bool is_soft_ace;
493 } card;
495 typedef struct game_context {
496 struct card player_cards[2][22]; /* 22 Cards means the deal was all aces */
497 struct card dealer_cards[22]; /* That is the worst-case scenario */
498 unsigned int player_total;
499 unsigned int dealer_total;
500 signed int player_money;
501 unsigned int num_player_cards[2];
502 unsigned int num_dealer_cards;
503 unsigned int current_bet;
504 unsigned int split_status; /* 0 = split hasn't been asked, *
505 * 1 = split did not occur *
506 * 2 = split occurred *
507 * 3 = split occurred and 1st hand done */
508 bool is_blackjack;
509 bool end_hand;
510 bool asked_insurance;
511 } game_context;
513 static bool resume;
514 static struct highscore highest[NUM_SCORES];
516 /*****************************************************************************
517 * blackjack_init() initializes blackjack data structures.
518 ******************************************************************************/
519 static void blackjack_init(struct game_context* bj) {
520 /* seed the rand generator */
521 rb->srand(*rb->current_tick);
523 /* reset card positions */
524 dealer_x = 4;
525 dealer_y = LCD_HEIGHT/4 - CARD_HEIGHT/2;
526 player_x = 4;
527 player_y = LCD_HEIGHT - LCD_HEIGHT/4 - CARD_HEIGHT/2;
529 /* check for resumed game */
530 if(resume) return;
532 /* reset scoring */
533 bj->player_total = 0;
534 bj->dealer_total = 0;
535 bj->num_player_cards[0] = 2;
536 bj->num_player_cards[1] = 0;
537 bj->num_dealer_cards = 2;
538 bj->end_hand = false;
539 bj->split_status = 0;
540 bj->is_blackjack = false;
541 bj->asked_insurance = false;
544 /*****************************************************************************
545 * blackjack_drawtable() draws the table and some text.
546 ******************************************************************************/
547 static void blackjack_drawtable(struct game_context* bj) {
548 unsigned int w, h, y_loc;
549 char str[10];
551 #if LCD_HEIGHT <= 64
552 rb->lcd_getstringsize("Bet", &w, &h);
553 rb->lcd_putsxy(LCD_WIDTH - w, 2*h + 1, "Bet");
554 rb->snprintf(str, 9, "$%d", bj->current_bet);
555 rb->lcd_getstringsize(str, &w, &h);
556 rb->lcd_putsxy(LCD_WIDTH - w, 3*h + 1, str);
557 y_loc = LCD_HEIGHT/2;
558 #else
559 rb->lcd_getstringsize("Bet", &w, &h);
560 rb->lcd_putsxy(LCD_WIDTH - w, 5*h / 2, "Bet");
561 rb->snprintf(str, 9, "$%d", bj->current_bet);
562 rb->lcd_getstringsize(str, &w, &h);
563 rb->lcd_putsxy(LCD_WIDTH - w, 7*h / 2, str);
564 rb->lcd_hline(0, LCD_WIDTH, LCD_HEIGHT/2);
565 y_loc = LCD_HEIGHT/2 + h;
566 #endif
568 rb->lcd_putsxy(0,0, "Dealer");
569 rb->lcd_getstringsize("Player", &w, &h);
570 rb->lcd_putsxy(0, y_loc, "Player");
571 rb->lcd_getstringsize("Total", &w, &h);
572 rb->lcd_putsxy(LCD_WIDTH - w, y_loc, "Total");
573 rb->lcd_getstringsize("Money", &w, &h);
574 rb->lcd_putsxy(LCD_WIDTH - w, 0, "Money");
575 rb->snprintf(str, 9, "$%d", bj->player_money - bj->current_bet);
576 rb->lcd_getstringsize(str, &w, &h);
577 rb->lcd_putsxy(LCD_WIDTH - w, h + 1, str);
578 rb->snprintf(str, 3, "%d", bj->player_total);
579 rb->lcd_getstringsize(str, &w, &h);
580 rb->lcd_putsxy(LCD_WIDTH - w, y_loc + h, str);
583 /*****************************************************************************
584 * find_value() is passed a card and returns its blackjack value.
585 ******************************************************************************/
586 static unsigned int find_value(unsigned int number) {
587 unsigned int thisValue;
588 if (number == 0)
589 thisValue = 11; /* Aces get a value of 11 at first */
590 else if (number < 10)
591 thisValue = number + 1;
592 else
593 thisValue = 10; /* Anything 10 or higher gets a value of 10 */
595 return thisValue;
598 /*****************************************************************************
599 * draw_card() draws a card to the screen.
600 ******************************************************************************/
601 static void draw_card(struct card temp_card, bool shown,
602 unsigned int x, unsigned int y) {
603 if(shown)
604 rb->lcd_bitmap_part(card_deck, CARD_WIDTH*temp_card.num,
605 CARD_HEIGHT*temp_card.suit, BMPWIDTH_card_deck,
606 x+1, y+1, CARD_WIDTH, CARD_HEIGHT);
607 else
608 rb->lcd_bitmap(card_back, x+1, y+1,CARD_WIDTH, CARD_HEIGHT);
609 #if LCD_DEPTH > 1
610 rb->lcd_set_foreground(LCD_BLACK);
611 #endif
613 /* Print outlines */
614 #if CARD_WIDTH >= 26
615 rb->lcd_hline(x+2, x+CARD_WIDTH-1, y);
616 rb->lcd_hline(x+2, x+CARD_WIDTH-1, y+CARD_HEIGHT+1);
617 rb->lcd_vline(x, y+2, y+CARD_HEIGHT-3);
618 rb->lcd_vline(x+CARD_WIDTH+1, y+2, y+CARD_HEIGHT-1);
619 rb->lcd_drawpixel(x+1, y+1);
620 rb->lcd_drawpixel(x+1, y+CARD_HEIGHT);
621 rb->lcd_drawpixel(x+CARD_WIDTH, y+1);
622 rb->lcd_drawpixel(x+CARD_WIDTH, y+CARD_HEIGHT);
623 #else
624 rb->lcd_hline(x+1, x+CARD_WIDTH, y);
625 rb->lcd_hline(x+1, x+CARD_WIDTH, y+CARD_HEIGHT+1);
626 rb->lcd_vline(x, y+1, y+CARD_HEIGHT);
627 rb->lcd_vline(x+CARD_WIDTH+1, y+1, y+CARD_HEIGHT);
628 #endif
630 #if LCD_DEPTH > 1
631 rb->lcd_set_foreground(FG_COLOR);
632 #endif
635 /*****************************************************************************
636 * new_card() initializes a new card and gives it values.
637 ******************************************************************************/
638 static struct card new_card(void) {
639 struct card new_card;
640 new_card.suit = rb->rand()%4; /* Random number 0-3 */
641 new_card.num = rb->rand()%13; /* Random number 0-12 */
642 new_card.value = find_value(new_card.num);
643 new_card.is_soft_ace = new_card.num == 0 ? true : false;
644 return new_card;
647 /*****************************************************************************
648 * deal_init_card() deals and draws to the screen the player's and dealer's
649 * initial cards.
650 ******************************************************************************/
651 static void deal_init_cards(struct game_context* bj) {
652 bj->dealer_cards[0] = new_card();
653 bj->dealer_total += bj->dealer_cards[0].value;
655 draw_card(bj->dealer_cards[0], false, dealer_x, dealer_y);
657 bj->dealer_cards[1] = new_card();
658 bj->dealer_total += bj->dealer_cards[1].value;
659 draw_card(bj->dealer_cards[1], true, dealer_x + CARD_WIDTH + 4, dealer_y);
661 bj->player_cards[0][0] = new_card();
662 bj->player_total += bj->player_cards[0][0].value;
663 draw_card(bj->player_cards[0][0], true, player_x, player_y);
664 player_x += CARD_WIDTH + 4;
666 bj->player_cards[0][1] = new_card();
667 bj->player_total += bj->player_cards[0][1].value;
668 draw_card(bj->player_cards[0][1], true, player_x, player_y);
669 player_x += CARD_WIDTH + 4;
672 /*****************************************************************************
673 * redraw_board() redraws all the cards and the board
674 ******************************************************************************/
675 static void redraw_board(struct game_context* bj) {
676 unsigned int i, n, upper_bound;
677 rb->lcd_clear_display();
679 blackjack_drawtable(bj);
680 player_x = 4;
681 dealer_x = 4;
682 upper_bound = bj->split_status > 1 ? 2 : 1;
684 for (i = 0; i < bj->num_dealer_cards; i++) {
685 if (!bj->end_hand) {
686 draw_card(bj->dealer_cards[0], false, dealer_x, dealer_y);
688 /* increment i so the dealer's first card isn't displayed */
689 i++;
690 dealer_x += CARD_WIDTH + 4;
692 draw_card(bj->dealer_cards[i], true, dealer_x, dealer_y);
694 if (bj->num_dealer_cards > MAX_CARDS-1)
695 dealer_x += 10;
696 else
697 dealer_x += CARD_WIDTH + 4;
700 for (n = 0; n < upper_bound; n++) {
701 for (i = 0; i < bj->num_player_cards[n]; i++) {
702 draw_card(bj->player_cards[n][i], true, player_x, player_y);
703 if (bj->split_status>1 || bj->num_player_cards[n]>MAX_CARDS)
704 player_x += 10;
705 else
706 player_x += CARD_WIDTH + 4;
708 if (bj->split_status > 1)
709 player_x = LCD_WIDTH/2 + 4;
713 /*****************************************************************************
714 * update_total updates the player's total
715 ******************************************************************************/
716 static void update_total(struct game_context* bj) {
717 char total[3];
718 unsigned int w, h;
719 rb->snprintf(total, 3, "%d", bj->player_total);
720 rb->lcd_getstringsize(total, &w, &h);
721 #if LCD_HEIGHT > 64
722 h *= 2;
723 #endif
724 rb->lcd_putsxy(LCD_WIDTH - w, LCD_HEIGHT/2 + h, total);
725 rb->lcd_update_rect(LCD_WIDTH - w, LCD_HEIGHT/2 + h, w, h);
729 /*****************************************************************************
730 * check_for_aces() is passed an array of cards and returns where an ace is
731 * located. Otherwise, returns -1.
732 ******************************************************************************/
733 static signed int check_for_aces(struct card temp_cards[], unsigned int size) {
734 unsigned int i;
735 for(i = 0; i < size; i++) {
736 if (temp_cards[i].is_soft_ace == true)
737 return i;
739 return -1;
742 /*****************************************************************************
743 * check_totals() compares player and dealer totals.
744 * 0: bust 1: loss, 2: push, 3: win, 4: blackjack, 5: something's not right...
745 ******************************************************************************/
746 static unsigned int check_totals(struct game_context* bj) {
747 unsigned int temp;
748 if (bj->player_total > 21)
749 temp = 0;
750 else if (bj->player_total == 21 && bj->is_blackjack) {
751 if (bj->dealer_total == 21 && bj->num_dealer_cards == 2)
752 temp = 2;
753 else
754 temp = 4;
756 else if (bj->player_total == bj->dealer_total)
757 temp = 2;
758 else if (bj->dealer_total > 21 && bj->player_total < 22)
759 temp = 3;
760 else if (bj->dealer_total > bj->player_total)
761 temp = 1;
762 else if (bj->player_total > bj->dealer_total)
763 temp = 3;
764 else
765 temp = 5;
767 return temp;
770 /*****************************************************************************
771 * finish_dealer() draws cards for the dealer until he has 17 or more.
772 ******************************************************************************/
773 static void finish_dealer(struct game_context* bj) {
774 signed int temp = 0;
776 if (bj->dealer_total > 16 && bj->dealer_total < 22)
777 return;
779 while (bj->dealer_total < 17) {
780 bj->dealer_cards[bj->num_dealer_cards] = new_card();
781 bj->dealer_total += bj->dealer_cards[bj->num_dealer_cards].value;
782 bj->num_dealer_cards++;
785 while (bj->dealer_total > 21) {
786 temp = check_for_aces(bj->dealer_cards, bj->num_dealer_cards);
787 if(temp != -1) {
788 bj->dealer_cards[temp].is_soft_ace = false;
789 bj->dealer_total -= 10;
791 else
792 return;
796 /*****************************************************************************
797 * finish_game() completes the game once player's turn is over.
798 ******************************************************************************/
799 static void finish_game(struct game_context* bj) {
800 unsigned int rValue, w, h;
801 char str[19];
803 do {
804 finish_dealer(bj);
805 } while (bj->dealer_total < 17);
807 redraw_board(bj);
808 rValue = check_totals(bj);
810 if (rValue == 0) {
811 rb->snprintf(str, sizeof(str), " Bust! ");
812 bj->player_money -= bj->current_bet;
814 else if (rValue == 1) {
815 rb->snprintf(str, sizeof(str), " Sorry, you lost. ");
816 bj->player_money -= bj->current_bet;
818 else if (rValue == 2) {
819 rb->snprintf(str, sizeof(str), " Push ");
821 else if (rValue == 3) {
822 rb->snprintf(str, sizeof(str), " You won! ");
823 bj->player_money+= bj->current_bet;
825 else {
826 rb->snprintf(str, sizeof(str), " Blackjack! ");
827 bj->player_money += bj->current_bet * 3 / 2;
829 rb->lcd_getstringsize(str, &w, &h);
831 #if LCD_HEIGHT <= 64
832 rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
833 rb->lcd_fillrect(0, LCD_HEIGHT/2, LCD_WIDTH, LCD_HEIGHT/2);
834 rb->lcd_set_drawmode(DRMODE_SOLID);
835 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + h, str);
836 rb->snprintf(str, 12, "You have %d", bj->player_total);
837 rb->lcd_getstringsize(str, &w, &h);
838 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2, str);
839 #else
840 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, str);
841 #endif
842 rb->lcd_update();
845 /*****************************************************************************
846 * blackjack_loadgame() loads the saved game and returns load success.
847 ******************************************************************************/
848 static bool blackjack_loadgame(struct game_context* bj) {
849 signed int fd;
850 bool loaded = false;
852 resume = false;
853 /* open game file */
854 fd = rb->open(SAVE_FILE, O_RDONLY);
855 if(fd < 0) return loaded;
857 /* read in saved game */
858 if(rb->read(fd, bj, sizeof(struct game_context))
859 == (long)sizeof(struct game_context))
861 resume = true;
862 loaded = true;
865 rb->close(fd);
867 /* delete saved file */
868 rb->remove(SAVE_FILE);
869 return loaded;
872 /*****************************************************************************
873 * blackjack_savegame() saves the current game state.
874 ******************************************************************************/
875 static void blackjack_savegame(struct game_context* bj) {
876 int fd;
878 if(!resume)
879 return;
880 /* write out the game state to the save file */
881 fd = rb->open(SAVE_FILE, O_WRONLY|O_CREAT);
882 if(fd < 0)
883 return;
884 rb->write(fd, bj, sizeof(struct game_context));
885 rb->close(fd);
888 /*****************************************************************************
889 * blackjack_get_yes_no() gets a yes/no answer from the user
890 ******************************************************************************/
891 static unsigned int blackjack_get_yes_no(char message[20]) {
892 int button;
893 unsigned int w, h, b, choice = 0;
894 bool breakout = false;
895 char message_yes[24], message_no[24];
897 rb->strcpy(message_yes, message);
898 rb->strcpy(message_no, message);
899 rb->strcat(message_yes, " Yes");
900 rb->strcat(message_no, " No");
901 rb->lcd_getstringsize(message_yes, &w, &h);
902 const char *stg[] = {message_yes, message_no};
904 #if LCD_HEIGHT <= 64
905 b = 2*h+1;
906 #else
907 b = h-1;
908 #endif
910 #ifdef HAVE_LCD_COLOR
911 rb->lcd_fillrect(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + b, w+1, h+3);
912 rb->lcd_set_foreground(LCD_BLACK);
913 rb->lcd_set_background(LCD_WHITE);
914 #else
915 rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
916 rb->lcd_fillrect(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + b, w+1, h+3);
917 rb->lcd_set_drawmode(DRMODE_SOLID);
918 #endif
919 rb->lcd_drawrect(LCD_WIDTH/2 - w/2 - 1, LCD_HEIGHT/2 + b - 1, w+3, h+4);
921 while(!breakout) {
922 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + b +1, stg[choice]);
923 rb->lcd_update_rect(LCD_WIDTH/2 - w/2 - 1, LCD_HEIGHT/2 + b -1,
924 w+3, h+4);
925 button = rb->button_get(true);
927 switch(button) {
928 case BJACK_LEFT:
929 case (BJACK_LEFT|BUTTON_REPEAT):
930 case BJACK_RIGHT:
931 case (BJACK_RIGHT|BUTTON_REPEAT):
932 choice ^= 1;
933 break;
934 case BJACK_SELECT: breakout = true;
935 break;
936 case BJACK_QUIT: breakout = true;
937 choice = BJ_QUIT;
938 break;
942 #if LCD_DEPTH > 1
943 rb->lcd_set_foreground(FG_COLOR);
944 rb->lcd_set_background(BG_COLOR);
945 #endif
946 return choice;
949 /*****************************************************************************
950 * blackjack_get_amount() gets an amount from the player to be used
951 ******************************************************************************/
952 static signed int blackjack_get_amount(char message[20], signed int lower_limit,
953 signed int upper_limit,
954 signed int start) {
955 int button;
956 char str[9];
957 bool changed = false;
958 unsigned int w, h;
959 signed int amount;
961 rb->lcd_getstringsize("A", &w, &h); /* find the size of one character */
963 if (start > upper_limit)
964 amount = upper_limit;
965 else if (start < lower_limit)
966 amount = lower_limit;
967 else
968 amount = start;
970 #if LCD_DEPTH > 1
971 rb->lcd_set_background(LCD_WHITE);
972 rb->lcd_set_foreground(LCD_BLACK);
973 #endif
975 #if LCD_HEIGHT <= 64
976 rb->lcd_clear_display();
977 rb->lcd_puts(0, 1, message);
978 rb->snprintf(str, 9, "$%d", amount);
979 rb->lcd_puts(0, 2, str);
980 rb->lcd_puts(0, 3, "RIGHT: +1");
981 rb->lcd_puts(0, 4, "LEFT: -1");
982 rb->lcd_puts(0, 5, "UP: +10");
983 rb->lcd_puts(0, 6, "DOWN: -10");
984 rb->lcd_update();
985 #else
986 rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
987 rb->lcd_fillrect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3,
988 37*w / 2, 8*h -3);
989 rb->lcd_set_drawmode(DRMODE_SOLID);
990 rb->lcd_drawrect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3,
991 37*w / 2, 8*h -3);
992 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 4*h - 1, message);
993 rb->snprintf(str, 9, "$%d", amount);
994 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, str);
995 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
996 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
997 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
998 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - h-2, " >>|: +1");
999 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 1, " |<<: -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");
1002 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
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, "SCROLL+: +10");
1006 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + 2*h + 1, "SCROLL-: -10");
1007 #else
1008 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - h-2, "RIGHT: +1");
1009 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 1, "LEFT: -1");
1010 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + h, "UP: +10");
1011 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 + 2*h + 1, "DOWN: -10");
1012 #endif
1013 rb->lcd_update_rect(LCD_WIDTH/2 - 9*w - 1, LCD_HEIGHT/2 - 4*h - 3,
1014 37*w / 2, 8*h -3);
1015 #endif
1017 while(true) {
1018 button = rb->button_get(true);
1020 switch(button) {
1021 case BJACK_UP:
1022 case (BJACK_UP|BUTTON_REPEAT):
1023 if (amount + 10 < upper_limit + 1) {
1024 amount += 10;
1025 changed = true;
1027 break;
1028 case BJACK_DOWN:
1029 case (BJACK_DOWN|BUTTON_REPEAT):
1030 if (amount - 10 > lower_limit - 1) {
1031 amount -= 10;
1032 changed = true;
1034 break;
1035 case BJACK_RIGHT:
1036 case (BJACK_RIGHT|BUTTON_REPEAT):
1037 if (amount + 1 < upper_limit + 1) {
1038 amount++;
1039 changed = true;
1041 break;
1042 case BJACK_LEFT:
1043 case (BJACK_LEFT|BUTTON_REPEAT):
1044 if (amount - 1 > lower_limit - 1) {
1045 amount--;
1046 changed = true;
1048 break;
1049 case BJACK_MAX :
1050 amount = upper_limit;
1051 changed = true;
1052 break;
1053 case BJACK_MIN :
1054 amount = lower_limit;
1055 changed = true;
1056 break;
1057 case BJACK_QUIT:
1058 return 0;
1059 case BJACK_SELECT:
1060 #if LCD_DEPTH > 1
1061 rb->lcd_set_foreground(FG_COLOR);
1062 rb->lcd_set_background(BG_COLOR);
1063 #endif
1064 rb->lcd_clear_display();
1065 return amount;
1068 if(changed) {
1069 rb->snprintf(str, 9, "$%d", amount);
1070 #if LCD_HEIGHT <= 64
1071 rb->lcd_puts(0, 2, str);
1072 rb->lcd_update();
1073 #else
1074 rb->lcd_set_drawmode(DRMODE_BG+DRMODE_INVERSEVID);
1075 rb->lcd_fillrect(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, 5*w, h);
1076 rb->lcd_set_drawmode(DRMODE_SOLID);
1077 rb->lcd_putsxy(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, str);
1078 rb->lcd_update_rect(LCD_WIDTH/2 - 9*w, LCD_HEIGHT/2 - 3*h, 5*w, h);
1079 #endif
1080 changed = false;
1085 /*****************************************************************************
1086 * blackjack_get_bet() gets the player's bet.
1087 ******************************************************************************/
1088 static void blackjack_get_bet(struct game_context* bj) {
1089 bj->current_bet = blackjack_get_amount("Please enter a bet", 10,
1090 bj->player_money, bj->current_bet);
1093 /*****************************************************************************
1094 * double_down() returns one final card then finishes the game
1095 ******************************************************************************/
1096 static void double_down(struct game_context* bj) {
1097 bj->current_bet *= 2;
1098 bj->player_cards[0][bj->num_player_cards[0]] = new_card();
1099 bj->player_total += bj->player_cards[0][bj->num_player_cards[0]].value;
1100 bj->num_player_cards[0]++;
1103 /*****************************************************************************
1104 * split() checks if the player wants to split and acts accordingly.
1105 * When bj->split_status is 1, no split occurred. 2 means the player split and 3
1106 * means a split has already occurred and the first hand is done.
1107 ******************************************************************************/
1108 static void split(struct game_context* bj) {
1109 if (blackjack_get_yes_no("Split?") == 1)
1110 bj->split_status = 1;
1111 else {
1112 bj->split_status = 2;
1113 bj->current_bet *= 2;
1114 bj->num_player_cards[0] = 1;
1115 bj->num_player_cards[1] = 1;
1116 bj->player_cards[1][0] = bj->player_cards[0][1];
1117 bj->player_total = bj->player_cards[0][0].value;
1121 /*****************************************************************************
1122 * insurance() see if the player wants to buy insurance and how much.
1123 ******************************************************************************/
1124 static unsigned int insurance(struct game_context* bj) {
1125 unsigned int insurance, max_amount;
1127 insurance = blackjack_get_yes_no("Buy Insurance?");
1128 bj->asked_insurance = true;
1129 max_amount = bj->current_bet < (unsigned int)bj->player_money ?
1130 bj->current_bet/2 : (unsigned int)bj->player_money;
1131 if (insurance == 1) return 0;
1133 insurance = blackjack_get_amount("How much?", 0, max_amount, 0);
1134 redraw_board(bj);
1135 return insurance;
1138 /*****************************************************************************
1139 * play_again() checks to see if the player wants to keep playing.
1140 ******************************************************************************/
1141 static unsigned int play_again(void) {
1142 return blackjack_get_yes_no("Play Again?");
1145 /*****************************************************************************
1146 * blackjack_help() displays help text.
1147 ******************************************************************************/
1148 static bool blackjack_help(void) {
1149 #define WORDS (sizeof help_text / sizeof (char*))
1150 static char *help_text[] = {
1151 "Blackjack", "",
1152 "The", "goal", "is", "to", "get", "21.", "", "",
1153 "Controls", "",
1154 BJACK_SELECT_NAME, ":", "hit", "/", "select", "",
1155 BJACK_STAY_NAME, ":", "stay", "",
1156 BJACK_DOUBLE_NAME, ":", "double", "down", "",
1157 BJACK_QUIT_NAME, ":", "End", "game", "and", "go", "to", "menu", "",
1158 BJACK_RESUME_NAME, ":", "Go", "to", "menu", "without", "end", "game",
1160 static struct style_text formation[]={
1161 { 0, TEXT_CENTER|TEXT_UNDERLINE },
1162 { 12, C_RED }, /* Hit/Select */
1163 { 18, C_RED }, /* Stay */
1164 { 22, C_RED }, /* Double Down */
1165 { 27, C_RED }, /* Quit */
1166 { 36, C_RED }, /* Menu */
1167 { -1, 0 }
1169 int button;
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);
1175 #endif
1177 if (display_text(WORDS, help_text, formation, NULL))
1178 return true;
1179 do {
1180 button = rb->button_get(true);
1181 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
1182 return true;
1184 } while( ( button == BUTTON_NONE )
1185 || ( button & (BUTTON_REL|BUTTON_REPEAT) ) );
1186 rb->lcd_setfont(FONT_SYSFIXED);
1187 return false;
1190 /*****************************************************************************
1191 * blackjack_menu() is the initial menu at the start of the game.
1192 ******************************************************************************/
1193 static unsigned int blackjack_menu(struct game_context* bj) {
1194 int selection = resume?0:1;
1195 bool breakout = false;
1196 (void)bj;
1198 MENUITEM_STRINGLIST(menu, "BlackJack Menu", NULL,
1199 "Resume Game", "Start New Game",
1200 "High Scores", "Help",
1201 "Playback Control", "Quit", "Save and Quit");
1203 while(!breakout) {
1204 switch(rb->do_menu(&menu, &selection, NULL, false)) {
1205 case 0:
1206 if(!resume) {
1207 rb->splash(HZ*2, "Nothing to resume");
1208 } else {
1209 breakout = true;
1211 break;
1212 case 1:
1213 breakout = true;
1214 resume = false;
1215 break;
1216 case 2:
1217 highscore_show(NUM_SCORES, highest, NUM_SCORES, false);
1218 break;
1219 case 3:
1220 if(blackjack_help())
1221 return BJ_USB;
1222 break;
1223 case 4:
1224 if (playback_control(NULL))
1225 return BJ_USB;
1226 break;
1227 case 5:
1228 return BJ_QUIT;
1229 case 6:
1230 return BJ_SAVE_AND_QUIT;
1232 case MENU_ATTACHED_USB:
1233 return BJ_USB;
1235 default:
1236 break;
1240 return 0;
1243 /*****************************************************************************
1244 * blackjack() is the main game subroutine, it returns the final game status.
1245 ******************************************************************************/
1246 static int blackjack(struct game_context* bj) {
1247 int button;
1248 unsigned int w, h, temp_var, done = 0, todo = 1;
1249 signed int temp;
1250 bool breakout = false;
1251 bool dbl_down = false;
1253 #if LCD_DEPTH > 1
1254 rb->lcd_set_background(BG_COLOR);
1255 rb->lcd_set_foreground(FG_COLOR);
1256 #endif
1258 /********************
1259 * menu *
1260 ********************/
1261 temp_var = blackjack_menu(bj);
1262 if (temp_var != 0)
1263 return temp_var;
1266 /********************
1267 * init *
1268 ********************/
1269 blackjack_init(bj);
1271 /********************
1272 * play *
1273 ********************/
1275 /* check for resumed game */
1276 if(resume) {
1277 resume = false;
1278 redraw_board(bj);
1279 if (bj->split_status == 2) {
1280 todo=2;
1281 player_x = bj->num_player_cards[0] * 10 + 4;
1283 else if (bj->split_status == 3) {
1284 player_x = bj->num_player_cards[1] * 10 + LCD_WIDTH/2 + 4;
1285 todo=2;
1286 done=1;
1289 else {
1290 bj->player_money = 1000;
1291 bj->current_bet = 10;
1292 blackjack_get_bet(bj);
1293 if (bj->current_bet == 0)
1294 return -1;
1295 rb->lcd_clear_display();
1296 deal_init_cards(bj);
1297 blackjack_drawtable(bj);
1300 rb->lcd_update();
1302 breakout = false;
1304 while(true){
1305 if(bj->player_total == 21 && bj->num_player_cards[0] == 2) {
1306 bj->is_blackjack = true;
1307 bj->end_hand = true;
1308 finish_game(bj);
1310 else if(bj->dealer_cards[1].is_soft_ace && !breakout &&
1311 !bj->asked_insurance) {
1312 temp_var = insurance(bj);
1313 if (bj->dealer_total == 21) {
1314 rb->splash(HZ, "Dealer has blackjack");
1315 bj->player_money += temp_var;
1316 bj->end_hand = true;
1317 breakout = true;
1318 redraw_board(bj);
1319 finish_game(bj);
1321 else {
1322 rb->splash(HZ, "Dealer does not have blackjack");
1323 bj->player_money -= temp_var;
1324 breakout = true;
1325 redraw_board(bj);
1326 rb->lcd_update();
1329 if(!bj->end_hand && bj->split_status == 0 &&
1330 bj->player_cards[0][0].num == bj->player_cards[0][1].num) {
1331 split(bj);
1332 redraw_board(bj);
1333 rb->lcd_update_rect(0, LCD_HEIGHT/2, LCD_WIDTH, LCD_HEIGHT/2);
1334 if (bj->split_status == 2) {
1335 todo++;
1336 player_x = bj->num_player_cards[0] * 10 + 4;
1340 while(!bj->end_hand && done < todo) {
1341 button = rb->button_get(true);
1343 switch(button) {
1344 case BJACK_SELECT:
1345 NEXT_CARD = new_card();
1346 bj->player_total += NEXT_CARD.value;
1347 draw_card(NEXT_CARD, true, player_x, player_y);
1348 bj->num_player_cards[done]++;
1349 if (bj->num_player_cards[done] == MAX_CARDS + 1) {
1350 redraw_board(bj);
1351 rb->lcd_update_rect(0, LCD_HEIGHT/2, LCD_WIDTH,
1352 LCD_HEIGHT/2);
1354 else if (bj->num_player_cards[done]>MAX_CARDS || todo > 1) {
1355 rb->lcd_update_rect(player_x, player_y, CARD_WIDTH+2,
1356 CARD_HEIGHT+2);
1357 player_x += 10;
1359 else {
1360 rb->lcd_update_rect(player_x, player_y, CARD_WIDTH+2,
1361 CARD_HEIGHT+2);
1362 player_x += CARD_WIDTH + 4;
1364 update_total(bj);
1366 break;
1367 case BJACK_STAY:
1368 bj->end_hand = true;
1369 break;
1370 case BJACK_DOUBLEDOWN:
1371 if ((signed int)bj->current_bet * 2 <
1372 bj->player_money + 1 &&
1373 bj->num_player_cards[0]==2 && todo==1) {
1374 double_down(bj);
1375 dbl_down = true;
1376 if (bj->player_total < 22) {
1377 bj->end_hand = true;
1378 finish_game(bj);
1381 else if((signed int)bj->current_bet * 2 >
1382 bj->player_money){
1383 rb->splash(HZ, "Not enough money to double down.");
1384 redraw_board(bj);
1385 rb->lcd_update();
1387 break;
1388 case BJACK_RESUME: /* save and end game */
1389 resume = true;
1390 return BJ_END;
1392 case BJACK_QUIT:
1393 return BJ_END;
1396 while (bj->player_total > 21 && !bj->end_hand) {
1397 temp = check_for_aces(bj->player_cards[done],
1398 bj->num_player_cards[done]);
1399 if(temp != -1) {
1400 bj->player_cards[done][temp].is_soft_ace = false;
1401 bj->player_total -= 10;
1402 update_total(bj);
1403 if (dbl_down) {
1404 bj->end_hand = true;
1405 finish_game(bj);
1408 else
1409 bj->end_hand = true;
1412 if (bj->end_hand) {
1413 done++;
1414 if(todo > 1) {
1415 if (done == 2) {
1416 temp = bj->player_total;
1417 bj->player_total = temp_var;
1418 temp_var = temp;
1419 finish_game(bj);
1420 rb->lcd_getstringsize(" Split 1 ", &w, &h);
1421 rb->lcd_putsxy(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
1422 " Split 1 ");
1423 rb->lcd_update_rect(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
1424 w,h);
1425 bj->current_bet /= 2;
1426 rb->lcd_update_rect(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
1427 w,h);
1428 rb->sleep(HZ*2);
1429 bj->player_total = temp_var;
1430 finish_game(bj);
1431 rb->lcd_getstringsize(" Split 2 ", &w, &h);
1432 rb->lcd_putsxy(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
1433 " Split 2 ");
1434 rb->lcd_update_rect(LCD_WIDTH/2-w/2, LCD_HEIGHT/2-3*h/2,
1435 w,h);
1436 rb->sleep(HZ*2);
1438 else {
1439 bj->end_hand = false;
1440 bj->split_status = 3;
1441 temp_var = bj->player_total;
1442 bj->player_total = bj->player_cards[1][0].value;
1443 update_total(bj);
1444 redraw_board(bj);
1445 player_x += 10;
1446 rb->lcd_update();
1449 else
1450 finish_game(bj);
1454 if (bj->player_money < 10) {
1455 rb->sleep(HZ);
1456 return BJ_LOSE;
1459 if (bj->end_hand) { /* If hand is over */
1460 if (play_again() != 0) /* User wants to quit */
1461 return BJ_END;
1462 else { /* User keeps playing */
1463 breakout = false;
1464 temp = bj->current_bet;
1465 bj->current_bet = 0;
1466 redraw_board(bj);
1467 rb->lcd_update();
1468 bj->current_bet = temp;
1469 if(dbl_down) {
1470 bj->current_bet /= 2;
1471 dbl_down = false;
1473 done = 0;
1474 todo = 1;
1475 blackjack_init(bj);
1476 blackjack_get_bet(bj);
1477 if (bj->current_bet == 0)
1478 return BJ_END;
1479 deal_init_cards(bj);
1480 blackjack_drawtable(bj);
1481 rb->lcd_update();
1485 /* Never reached */
1486 return PLUGIN_OK;
1489 /*****************************************************************************
1490 * plugin entry point.
1491 ******************************************************************************/
1492 enum plugin_status plugin_start(const void* parameter)
1494 struct game_context bj;
1495 bool exit = false;
1497 (void)parameter;
1499 #if LCD_DEPTH > 1
1500 rb->lcd_set_backdrop(NULL);
1501 #endif
1503 /* load high scores */
1504 highscore_load(HIGH_SCORE,highest,NUM_SCORES);
1505 blackjack_loadgame(&bj);
1507 rb->lcd_setfont(FONT_SYSFIXED);
1509 while(!exit) {
1510 switch(blackjack(&bj)){
1511 case BJ_LOSE:
1512 rb->splash(HZ, "Not enough money to continue");
1513 /* fall through to BJ_END */
1515 case BJ_END:
1516 if(!resume && bj.player_money > 10) {
1517 /* There is no level, so store -1 to blank column */
1518 int position = highscore_update(bj.player_money, -1, "",
1519 highest, NUM_SCORES);
1520 if (position != -1) {
1521 if (position == 0) {
1522 rb->splash(HZ*2, "New High Score");
1524 highscore_show(position, highest, NUM_SCORES, false);
1527 break;
1529 case BJ_USB:
1530 rb->lcd_setfont(FONT_UI);
1531 highscore_save(HIGH_SCORE,highest,NUM_SCORES);
1532 return PLUGIN_USB_CONNECTED;
1534 case BJ_SAVE_AND_QUIT:
1535 if (resume) {
1536 rb->splash(HZ, "Saving game...");
1537 blackjack_savegame(&bj);
1539 /* fall through */
1541 case BJ_QUIT:
1542 highscore_save(HIGH_SCORE,highest,NUM_SCORES);
1543 exit = true;
1544 break;
1546 default:
1547 break;
1551 rb->lcd_setfont(FONT_UI);
1552 return PLUGIN_OK;