blackjack.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. #include <gui/gui.h>
  2. #include <stdlib.h>
  3. #include <dolphin/dolphin.h>
  4. #include <dialogs/dialogs.h>
  5. #include <gui/canvas_i.h>
  6. #include <math.h>
  7. #include "util.h"
  8. #include "defines.h"
  9. #include "common/card.h"
  10. #include "common/dml.h"
  11. #include "common/queue.h"
  12. #include "util.h"
  13. #include "ui.h"
  14. #include "blackjack_icons.h"
  15. #define DEALER_MAX 17
  16. void start_round(GameState *game_state);
  17. void init(GameState *game_state);
  18. static void draw_ui(Canvas *const canvas, const GameState *game_state) {
  19. draw_money(canvas, game_state->player_score);
  20. draw_score(canvas, true, hand_count(game_state->player_cards, game_state->player_card_count));
  21. if (!game_state->queue_state.running && game_state->state == GameStatePlay) {
  22. render_menu(game_state->menu,canvas, 2, 47);
  23. }
  24. }
  25. static void render_callback(Canvas *const canvas, void *ctx) {
  26. const GameState *game_state = acquire_mutex((ValueMutex *) ctx, 25);
  27. if (game_state == NULL) {
  28. return;
  29. }
  30. canvas_set_color(canvas, ColorBlack);
  31. canvas_draw_frame(canvas, 0, 0, 128, 64);
  32. if (game_state->state == GameStateStart) {
  33. canvas_draw_icon(canvas, 0, 0, &I_blackjack);
  34. }
  35. if (game_state->state == GameStateGameOver) {
  36. canvas_draw_icon(canvas, 0, 0, &I_endscreen);
  37. }
  38. if (game_state->state == GameStatePlay || game_state->state == GameStateDealer) {
  39. if (game_state->state == GameStatePlay)
  40. draw_player_scene(canvas, game_state);
  41. else
  42. draw_dealer_scene(canvas, game_state);
  43. render_queue(&(game_state->queue_state), game_state, canvas);
  44. draw_ui(canvas, game_state);
  45. } else if (game_state->state == GameStateSettings) {
  46. settings_page(canvas, game_state);
  47. }
  48. release_mutex((ValueMutex *) ctx, game_state);
  49. }
  50. //region card draw
  51. Card draw_card(GameState *game_state) {
  52. Card c = game_state->deck.cards[game_state->deck.index];
  53. game_state->deck.index++;
  54. return c;
  55. }
  56. void drawPlayerCard(void *ctx) {
  57. GameState *game_state = ctx;
  58. Card c = draw_card(game_state);
  59. game_state->player_cards[game_state->player_card_count] = c;
  60. game_state->player_card_count++;
  61. if(game_state->player_score < game_state->settings.round_price || game_state->doubled){
  62. set_menu_state(game_state->menu, 0, false);
  63. }
  64. }
  65. void drawDealerCard(void *ctx) {
  66. GameState *game_state = ctx;
  67. Card c = draw_card(game_state);
  68. game_state->dealer_cards[game_state->dealer_card_count] = c;
  69. game_state->dealer_card_count++;
  70. }
  71. //endregion
  72. //region queue callbacks
  73. void to_lose_state(const void *ctx, Canvas *const canvas) {
  74. const GameState *game_state = ctx;
  75. if (game_state->settings.message_duration == 0)
  76. return;
  77. popup_frame(canvas);
  78. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "You lost");
  79. }
  80. void to_bust_state(const void *ctx, Canvas *const canvas) {
  81. const GameState *game_state = ctx;
  82. if (game_state->settings.message_duration == 0)
  83. return;
  84. popup_frame(canvas);
  85. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Busted!");
  86. }
  87. void to_draw_state(const void *ctx, Canvas *const canvas) {
  88. const GameState *game_state = ctx;
  89. if (game_state->settings.message_duration == 0)
  90. return;
  91. popup_frame(canvas);
  92. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Draw");
  93. }
  94. void to_dealer_turn(const void *ctx, Canvas *const canvas) {
  95. const GameState *game_state = ctx;
  96. if (game_state->settings.message_duration == 0)
  97. return;
  98. popup_frame(canvas);
  99. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Dealers turn");
  100. }
  101. void to_win_state(const void *ctx, Canvas *const canvas) {
  102. const GameState *game_state = ctx;
  103. if (game_state->settings.message_duration == 0)
  104. return;
  105. popup_frame(canvas);
  106. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "You win");
  107. }
  108. void to_start(const void *ctx, Canvas *const canvas) {
  109. const GameState *game_state = ctx;
  110. if (game_state->settings.message_duration == 0)
  111. return;
  112. popup_frame(canvas);
  113. elements_multiline_text_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "Round started");
  114. }
  115. void before_start(void *ctx) {
  116. GameState *game_state = ctx;
  117. game_state->dealer_card_count = 0;
  118. game_state->player_card_count = 0;
  119. }
  120. void start(void *ctx) {
  121. GameState *game_state = ctx;
  122. start_round(game_state);
  123. }
  124. void draw(void *ctx) {
  125. GameState *game_state = ctx;
  126. game_state->player_score += game_state->bet;
  127. game_state->bet = 0;
  128. enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
  129. game_state->settings.message_duration);
  130. }
  131. void game_over(void *ctx) {
  132. GameState *game_state = ctx;
  133. game_state->state = GameStateGameOver;
  134. }
  135. void lose(void *ctx) {
  136. GameState *game_state = ctx;
  137. game_state->state = GameStatePlay;
  138. game_state->bet = 0;
  139. if (game_state->player_score >= game_state->settings.round_price) {
  140. enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
  141. game_state->settings.message_duration);
  142. } else {
  143. enqueue(&(game_state->queue_state), game_state, game_over, NULL, NULL,
  144. 0);
  145. }
  146. }
  147. void win(void *ctx) {
  148. GameState *game_state = ctx;
  149. game_state->state = GameStatePlay;
  150. game_state->player_score += game_state->bet * 2;
  151. game_state->bet = 0;
  152. enqueue(&(game_state->queue_state), game_state, start, before_start, to_start,
  153. game_state->settings.message_duration);
  154. }
  155. void dealerTurn(void *ctx) {
  156. GameState *game_state = ctx;
  157. game_state->state = GameStateDealer;
  158. }
  159. float animationTime(const GameState *game_state){
  160. return (float) (furi_get_tick() - game_state->queue_state.start) /
  161. (float) (game_state->settings.animation_duration);
  162. }
  163. void dealer_card_animation(const void *ctx, Canvas *const canvas) {
  164. const GameState *game_state = ctx;
  165. float t = animationTime(game_state);
  166. Card animatingCard = game_state->deck.cards[game_state->deck.index];
  167. if (game_state->dealer_card_count > 1) {
  168. Vector end = card_pos_at_index(game_state->dealer_card_count);
  169. draw_card_animation(animatingCard,
  170. (Vector) {0, 64},
  171. (Vector) {0, 32},
  172. end,
  173. t,
  174. true,
  175. canvas);
  176. } else {
  177. draw_card_animation(animatingCard,
  178. (Vector) {32, -CARD_HEIGHT},
  179. (Vector) {64, 32},
  180. (Vector) {2, 2},
  181. t,
  182. false,
  183. canvas);
  184. }
  185. }
  186. void dealer_back_card_animation(const void *ctx, Canvas *const canvas) {
  187. const GameState *game_state = ctx;
  188. float t = animationTime(game_state);
  189. Vector currentPos = quadratic_2d((Vector) {32, -CARD_HEIGHT}, (Vector) {64, 32}, (Vector) {13, 5}, t);
  190. draw_card_back_at(currentPos.x, currentPos.y, canvas);
  191. }
  192. void player_card_animation(const void *ctx, Canvas *const canvas) {
  193. const GameState *game_state = ctx;
  194. float t = animationTime(game_state);
  195. Card animatingCard = game_state->deck.cards[game_state->deck.index];
  196. Vector end = card_pos_at_index(game_state->player_card_count);
  197. draw_card_animation(animatingCard,
  198. (Vector) {32, -CARD_HEIGHT},
  199. (Vector) {0, 32},
  200. end,
  201. t,
  202. true,
  203. canvas);
  204. }
  205. //endregion
  206. void player_tick(GameState *game_state) {
  207. uint8_t score = hand_count(game_state->player_cards, game_state->player_card_count);
  208. if ((game_state->doubled && score <= 21) || score == 21) {
  209. enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
  210. game_state->settings.message_duration);
  211. } else if (score > 21) {
  212. enqueue(&(game_state->queue_state), game_state, lose, NULL, to_bust_state,
  213. game_state->settings.message_duration);
  214. } else {
  215. if(game_state->selectDirection == DirectionUp || game_state->selectDirection == DirectionDown){
  216. move_menu(game_state->menu, game_state->selectDirection == DirectionUp ? -1 : 1);
  217. }
  218. if (game_state->selectDirection == Select){
  219. activate_menu(game_state->menu, game_state);
  220. }
  221. }
  222. }
  223. void dealer_tick(GameState *game_state) {
  224. uint8_t dealer_score = hand_count(game_state->dealer_cards, game_state->dealer_card_count);
  225. uint8_t player_score = hand_count(game_state->player_cards, game_state->player_card_count);
  226. if (dealer_score >= DEALER_MAX) {
  227. if (dealer_score > 21 || dealer_score < player_score) {
  228. enqueue(&(game_state->queue_state), game_state, win, NULL, to_win_state,
  229. game_state->settings.message_duration);
  230. } else if (dealer_score > player_score) {
  231. enqueue(&(game_state->queue_state), game_state, lose, NULL, to_lose_state,
  232. game_state->settings.message_duration);
  233. } else if (dealer_score == player_score) {
  234. enqueue(&(game_state->queue_state), game_state, draw, NULL, to_draw_state,
  235. game_state->settings.message_duration);
  236. }
  237. } else {
  238. enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_card_animation,
  239. game_state->settings.animation_duration);
  240. }
  241. }
  242. void settings_tick(GameState *game_state) {
  243. if (game_state->selectDirection == DirectionDown && game_state->selectedMenu < 4) {
  244. game_state->selectedMenu++;
  245. }
  246. if (game_state->selectDirection == DirectionUp && game_state->selectedMenu > 0) {
  247. game_state->selectedMenu--;
  248. }
  249. if (game_state->selectDirection == DirectionLeft || game_state->selectDirection == DirectionRight) {
  250. int nextScore = 0;
  251. switch (game_state->selectedMenu) {
  252. case 0:
  253. nextScore = game_state->settings.starting_money;
  254. if (game_state->selectDirection == DirectionLeft)
  255. nextScore -= 10;
  256. else
  257. nextScore += 10;
  258. if (nextScore >= (int) game_state->settings.round_price && nextScore < 400)
  259. game_state->settings.starting_money = nextScore;
  260. break;
  261. case 1:
  262. nextScore = game_state->settings.round_price;
  263. if (game_state->selectDirection == DirectionLeft)
  264. nextScore -= 10;
  265. else
  266. nextScore += 10;
  267. if (nextScore >= 5 && nextScore <= (int) game_state->settings.starting_money)
  268. game_state->settings.round_price = nextScore;
  269. break;
  270. case 2:
  271. nextScore = game_state->settings.animation_duration;
  272. if (game_state->selectDirection == DirectionLeft)
  273. nextScore -= 100;
  274. else
  275. nextScore += 100;
  276. if (nextScore >= 0 && nextScore < 2000)
  277. game_state->settings.animation_duration = nextScore;
  278. break;
  279. case 3:
  280. nextScore = game_state->settings.message_duration;
  281. if (game_state->selectDirection == DirectionLeft)
  282. nextScore -= 100;
  283. else
  284. nextScore += 100;
  285. if (nextScore >= 0 && nextScore < 2000)
  286. game_state->settings.message_duration = nextScore;
  287. break;
  288. case 4:
  289. game_state->settings.sound_effects = !game_state->settings.sound_effects;
  290. default:
  291. break;
  292. }
  293. }
  294. }
  295. void tick(GameState *game_state) {
  296. game_state->last_tick = furi_get_tick();
  297. bool queue_ran = run_queue(&(game_state->queue_state), game_state);
  298. switch (game_state->state) {
  299. case GameStateGameOver:
  300. case GameStateStart:
  301. if (game_state->selectDirection == Select)
  302. init(game_state);
  303. else if (game_state->selectDirection == DirectionRight) {
  304. game_state->selectedMenu = 0;
  305. game_state->state = GameStateSettings;
  306. }
  307. break;
  308. case GameStatePlay:
  309. if (!game_state->started) {
  310. game_state->selectedMenu = 0;
  311. game_state->started = true;
  312. enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_back_card_animation,
  313. game_state->settings.animation_duration);
  314. enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
  315. game_state->settings.animation_duration);
  316. enqueue(&(game_state->queue_state), game_state, drawDealerCard, NULL, dealer_card_animation,
  317. game_state->settings.animation_duration);
  318. enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
  319. game_state->settings.animation_duration);
  320. }
  321. if (!queue_ran)
  322. player_tick(game_state);
  323. break;
  324. case GameStateDealer:
  325. if (!queue_ran)
  326. dealer_tick(game_state);
  327. break;
  328. case GameStateSettings:
  329. settings_tick(game_state);
  330. break;
  331. default:
  332. break;
  333. }
  334. game_state->selectDirection = None;
  335. }
  336. void start_round(GameState *game_state) {
  337. game_state->menu->current_menu=1;
  338. game_state->player_card_count = 0;
  339. game_state->dealer_card_count = 0;
  340. set_menu_state(game_state->menu, 0, true);
  341. game_state->menu->enabled=true;
  342. game_state->started = false;
  343. game_state->doubled = false;
  344. game_state->queue_state.running = true;
  345. shuffle_deck(&(game_state->deck));
  346. game_state->doubled = false;
  347. game_state->bet = game_state->settings.round_price;
  348. if (game_state->player_score < game_state->settings.round_price) {
  349. game_state->state = GameStateGameOver;
  350. } else {
  351. game_state->player_score -= game_state->settings.round_price;
  352. }
  353. game_state->state = GameStatePlay;
  354. }
  355. void init(GameState *game_state) {
  356. set_menu_state(game_state->menu, 0, true);
  357. game_state->menu->enabled=true;
  358. game_state->menu->current_menu=1;
  359. game_state->settings = load_settings();
  360. game_state->last_tick = 0;
  361. game_state->processing = true;
  362. game_state->selectedMenu = 0;
  363. game_state->player_score = game_state->settings.starting_money;
  364. generate_deck(&(game_state->deck), 6);
  365. start_round(game_state);
  366. }
  367. static void input_callback(InputEvent *input_event, FuriMessageQueue *event_queue) {
  368. furi_assert(event_queue);
  369. AppEvent event = {.type = EventTypeKey, .input = *input_event};
  370. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  371. }
  372. static void update_timer_callback(FuriMessageQueue *event_queue) {
  373. furi_assert(event_queue);
  374. AppEvent event = {.type = EventTypeTick};
  375. furi_message_queue_put(event_queue, &event, 0);
  376. }
  377. void doubleAction(void *state){
  378. GameState *game_state = state;
  379. if (!game_state->doubled && game_state->player_score >= game_state->settings.round_price) {
  380. game_state->player_score -= game_state->settings.round_price;
  381. game_state->bet += game_state->settings.round_price;
  382. game_state->doubled = true;
  383. enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
  384. game_state->settings.animation_duration);
  385. game_state->player_cards[game_state->player_card_count] = game_state->deck.cards[game_state->deck.index];
  386. uint8_t score = hand_count(game_state->player_cards, game_state->player_card_count + 1);
  387. if (score > 21) {
  388. enqueue(&(game_state->queue_state), game_state, lose, NULL, to_bust_state,
  389. game_state->settings.message_duration);
  390. } else {
  391. enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
  392. game_state->settings.message_duration);
  393. }
  394. set_menu_state(game_state->menu, 0, false);
  395. }
  396. }
  397. void hitAction(void *state){
  398. GameState *game_state = state;
  399. enqueue(&(game_state->queue_state), game_state, drawPlayerCard, NULL, player_card_animation,
  400. game_state->settings.animation_duration);
  401. }
  402. void stayAction(void *state){
  403. GameState *game_state = state;
  404. enqueue(&(game_state->queue_state), game_state, dealerTurn, NULL, to_dealer_turn,
  405. game_state->settings.message_duration);
  406. }
  407. int32_t blackjack_app(void *p) {
  408. UNUSED(p);
  409. int32_t return_code = 0;
  410. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(AppEvent));
  411. GameState *game_state = malloc(sizeof(GameState));
  412. game_state->menu= malloc(sizeof(Menu));
  413. game_state->menu->menu_width=40;
  414. init(game_state);
  415. add_menu(game_state->menu, "Double", doubleAction);
  416. add_menu(game_state->menu, "Hit", hitAction);
  417. add_menu(game_state->menu, "Stay", stayAction);
  418. set_card_graphics(&I_card_graphics);
  419. game_state->state = GameStateStart;
  420. ValueMutex state_mutex;
  421. if (!init_mutex(&state_mutex, game_state, sizeof(GameState))) {
  422. FURI_LOG_E(APP_NAME, "cannot create mutex\r\n");
  423. return_code = 255;
  424. goto free_and_exit;
  425. }
  426. ViewPort *view_port = view_port_alloc();
  427. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  428. view_port_input_callback_set(view_port, input_callback, event_queue);
  429. FuriTimer *timer =
  430. furi_timer_alloc(update_timer_callback, FuriTimerTypePeriodic, event_queue);
  431. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
  432. Gui *gui = furi_record_open("gui");
  433. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  434. AppEvent event;
  435. for (bool processing = true; processing;) {
  436. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  437. GameState *localstate = (GameState *) acquire_mutex_block(&state_mutex);
  438. if (event_status == FuriStatusOk) {
  439. if (event.type == EventTypeKey) {
  440. if (event.input.type == InputTypePress) {
  441. switch (event.input.key) {
  442. case InputKeyUp:
  443. localstate->selectDirection = DirectionUp;
  444. break;
  445. case InputKeyDown:
  446. localstate->selectDirection = DirectionDown;
  447. break;
  448. case InputKeyRight:
  449. localstate->selectDirection = DirectionRight;
  450. break;
  451. case InputKeyLeft:
  452. localstate->selectDirection = DirectionLeft;
  453. break;
  454. case InputKeyBack:
  455. if (localstate->state == GameStateSettings) {
  456. localstate->state = GameStateStart;
  457. save_settings(localstate->settings);
  458. } else
  459. processing = false;
  460. break;
  461. case InputKeyOk:
  462. localstate->selectDirection = Select;
  463. break;
  464. default:
  465. break;
  466. }
  467. }
  468. } else if (event.type == EventTypeTick) {
  469. tick(localstate);
  470. processing = localstate->processing;
  471. }
  472. } else {
  473. FURI_LOG_D(APP_NAME, "osMessageQueue: event timeout");
  474. // event timeout
  475. }
  476. view_port_update(view_port);
  477. release_mutex(&state_mutex, localstate);
  478. }
  479. furi_timer_free(timer);
  480. view_port_enabled_set(view_port, false);
  481. gui_remove_view_port(gui, view_port);
  482. furi_record_close(RECORD_GUI);
  483. view_port_free(view_port);
  484. delete_mutex(&state_mutex);
  485. free_and_exit:
  486. free(game_state->deck.cards);
  487. free_menu(game_state->menu);
  488. queue_clear(&(game_state->queue_state));
  489. free(game_state);
  490. furi_message_queue_free(event_queue);
  491. return return_code;
  492. }