blackjack.c 20 KB

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