minesweeper.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <stdlib.h>
  6. #include <notification/notification_messages.h>
  7. #include <dialogs/dialogs.h>
  8. #include <dolphin/dolphin.h>
  9. #include "assets.h"
  10. #define PLAYFIELD_WIDTH 16
  11. #define PLAYFIELD_HEIGHT 7
  12. #define TILE_WIDTH 8
  13. #define TILE_HEIGHT 8
  14. #define MINECOUNT 20
  15. typedef enum {
  16. EventTypeTick,
  17. EventTypeKey,
  18. } EventType;
  19. typedef struct {
  20. EventType type;
  21. InputEvent input;
  22. } PluginEvent;
  23. typedef enum {
  24. TileType0, // this HAS to be in order, for hint assignment to be ez pz
  25. TileType1,
  26. TileType2,
  27. TileType3,
  28. TileType4,
  29. TileType5,
  30. TileType6,
  31. TileType7,
  32. TileType8,
  33. TileTypeUncleared,
  34. TileTypeFlag,
  35. TileTypeMine
  36. } TileType;
  37. typedef enum { FieldEmpty, FieldMine } Field;
  38. typedef struct {
  39. FuriMutex* mutex;
  40. DialogsApp* dialogs;
  41. NotificationApp* notifications;
  42. Field minefield[PLAYFIELD_WIDTH][PLAYFIELD_HEIGHT];
  43. TileType playfield[PLAYFIELD_WIDTH][PLAYFIELD_HEIGHT];
  44. int cursor_x;
  45. int cursor_y;
  46. int mines_left;
  47. int fields_cleared;
  48. int flags_set;
  49. bool game_started;
  50. uint32_t game_started_tick;
  51. } Minesweeper;
  52. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  53. furi_assert(event_queue);
  54. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  55. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  56. }
  57. static void render_callback(Canvas* const canvas, void* ctx) {
  58. furi_assert(ctx);
  59. const Minesweeper* minesweeper_state = ctx;
  60. furi_mutex_acquire(minesweeper_state->mutex, FuriWaitForever);
  61. FuriString* mineStr;
  62. FuriString* timeStr;
  63. mineStr = furi_string_alloc();
  64. timeStr = furi_string_alloc();
  65. furi_string_printf(mineStr, "Mines: %d", MINECOUNT - minesweeper_state->flags_set);
  66. canvas_set_font(canvas, FontSecondary);
  67. canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, furi_string_get_cstr(mineStr));
  68. int seconds = 0;
  69. int minutes = 0;
  70. if(minesweeper_state->game_started) {
  71. uint32_t ticks_elapsed = furi_get_tick() - minesweeper_state->game_started_tick;
  72. seconds = (int)ticks_elapsed / furi_kernel_get_tick_frequency();
  73. minutes = (int)seconds / 60;
  74. seconds = seconds % 60;
  75. }
  76. furi_string_printf(timeStr, "%01d:%02d", minutes, seconds);
  77. canvas_draw_str_aligned(canvas, 128, 0, AlignRight, AlignTop, furi_string_get_cstr(timeStr));
  78. uint8_t* tile_to_draw;
  79. for(int y = 0; y < PLAYFIELD_HEIGHT; y++) {
  80. for(int x = 0; x < PLAYFIELD_WIDTH; x++) {
  81. if(x == minesweeper_state->cursor_x && y == minesweeper_state->cursor_y) {
  82. canvas_invert_color(canvas);
  83. }
  84. switch(minesweeper_state->playfield[x][y]) {
  85. case TileType0:
  86. tile_to_draw = tile_0_bits;
  87. break;
  88. case TileType1:
  89. tile_to_draw = tile_1_bits;
  90. break;
  91. case TileType2:
  92. tile_to_draw = tile_2_bits;
  93. break;
  94. case TileType3:
  95. tile_to_draw = tile_3_bits;
  96. break;
  97. case TileType4:
  98. tile_to_draw = tile_4_bits;
  99. break;
  100. case TileType5:
  101. tile_to_draw = tile_5_bits;
  102. break;
  103. case TileType6:
  104. tile_to_draw = tile_6_bits;
  105. break;
  106. case TileType7:
  107. tile_to_draw = tile_7_bits;
  108. break;
  109. case TileType8:
  110. tile_to_draw = tile_8_bits;
  111. break;
  112. case TileTypeFlag:
  113. tile_to_draw = tile_flag_bits;
  114. break;
  115. case TileTypeUncleared:
  116. tile_to_draw = tile_uncleared_bits;
  117. break;
  118. case TileTypeMine:
  119. tile_to_draw = tile_mine_bits;
  120. break;
  121. default:
  122. // this should never happen
  123. tile_to_draw = tile_mine_bits;
  124. break;
  125. }
  126. canvas_draw_xbm(
  127. canvas,
  128. x * TILE_HEIGHT, // x
  129. 8 + (y * TILE_WIDTH), // y
  130. TILE_WIDTH,
  131. TILE_HEIGHT,
  132. tile_to_draw);
  133. if(x == minesweeper_state->cursor_x && y == minesweeper_state->cursor_y) {
  134. canvas_invert_color(canvas);
  135. }
  136. }
  137. }
  138. furi_string_free(mineStr);
  139. furi_string_free(timeStr);
  140. furi_mutex_release(minesweeper_state->mutex);
  141. }
  142. static void setup_playfield(Minesweeper* minesweeper_state) {
  143. int mines_left = MINECOUNT;
  144. for(int y = 0; y < PLAYFIELD_HEIGHT; y++) {
  145. for(int x = 0; x < PLAYFIELD_WIDTH; x++) {
  146. minesweeper_state->minefield[x][y] = FieldEmpty;
  147. minesweeper_state->playfield[x][y] = TileTypeUncleared;
  148. }
  149. }
  150. while(mines_left > 0) {
  151. int rand_x = rand() % PLAYFIELD_WIDTH;
  152. int rand_y = rand() % PLAYFIELD_HEIGHT;
  153. // make sure first guess isn't a mine
  154. if(minesweeper_state->minefield[rand_x][rand_y] == FieldEmpty &&
  155. (minesweeper_state->cursor_x != rand_x || minesweeper_state->cursor_y != rand_y)) {
  156. minesweeper_state->minefield[rand_x][rand_y] = FieldMine;
  157. mines_left--;
  158. }
  159. }
  160. minesweeper_state->mines_left = MINECOUNT;
  161. minesweeper_state->fields_cleared = 0;
  162. minesweeper_state->flags_set = 0;
  163. minesweeper_state->game_started_tick = furi_get_tick();
  164. minesweeper_state->game_started = false;
  165. }
  166. static void place_flag(Minesweeper* minesweeper_state) {
  167. if(minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] ==
  168. TileTypeUncleared) {
  169. minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] =
  170. TileTypeFlag;
  171. minesweeper_state->flags_set++;
  172. } else if(
  173. minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] ==
  174. TileTypeFlag) {
  175. minesweeper_state->playfield[minesweeper_state->cursor_x][minesweeper_state->cursor_y] =
  176. TileTypeUncleared;
  177. minesweeper_state->flags_set--;
  178. }
  179. }
  180. static bool game_lost(Minesweeper* minesweeper_state) {
  181. // returns true if the player wants to restart, otherwise false
  182. DialogMessage* message = dialog_message_alloc();
  183. dialog_message_set_header(message, "Game Over", 64, 3, AlignCenter, AlignTop);
  184. dialog_message_set_text(message, "You hit a mine!", 64, 32, AlignCenter, AlignCenter);
  185. dialog_message_set_buttons(message, NULL, "Play again", NULL);
  186. // Set cursor to initial position
  187. minesweeper_state->cursor_x = 0;
  188. minesweeper_state->cursor_y = 0;
  189. notification_message(minesweeper_state->notifications, &sequence_single_vibro);
  190. DialogMessageButton choice = dialog_message_show(minesweeper_state->dialogs, message);
  191. dialog_message_free(message);
  192. return choice == DialogMessageButtonCenter;
  193. }
  194. static bool game_won(Minesweeper* minesweeper_state) {
  195. FuriString* tempStr;
  196. tempStr = furi_string_alloc();
  197. int seconds = 0;
  198. int minutes = 0;
  199. uint32_t ticks_elapsed = furi_get_tick() - minesweeper_state->game_started_tick;
  200. seconds = (int)ticks_elapsed / furi_kernel_get_tick_frequency();
  201. minutes = (int)seconds / 60;
  202. seconds = seconds % 60;
  203. DialogMessage* message = dialog_message_alloc();
  204. const char* header_text = "Game won!";
  205. furi_string_cat_printf(tempStr, "Minefield cleared in %01d:%02d", minutes, seconds);
  206. dialog_message_set_header(message, header_text, 64, 3, AlignCenter, AlignTop);
  207. dialog_message_set_text(
  208. message, furi_string_get_cstr(tempStr), 64, 32, AlignCenter, AlignCenter);
  209. dialog_message_set_buttons(message, NULL, "Play again", NULL);
  210. // Call dolphin deed when we win the game
  211. dolphin_deed(DolphinDeedPluginGameWin);
  212. DialogMessageButton choice = dialog_message_show(minesweeper_state->dialogs, message);
  213. dialog_message_free(message);
  214. furi_string_free(tempStr);
  215. return choice == DialogMessageButtonCenter;
  216. }
  217. // returns false if the move loses the game - otherwise true
  218. static bool play_move(Minesweeper* minesweeper_state, int cursor_x, int cursor_y) {
  219. if(minesweeper_state->playfield[cursor_x][cursor_y] == TileTypeFlag) {
  220. // we're on a flagged field, do nothing
  221. return true;
  222. }
  223. if(minesweeper_state->minefield[cursor_x][cursor_y] == FieldMine) {
  224. // player loses - draw mine
  225. minesweeper_state->playfield[cursor_x][cursor_y] = TileTypeMine;
  226. return false;
  227. }
  228. if(minesweeper_state->playfield[cursor_x][cursor_y] >= TileType1 &&
  229. minesweeper_state->playfield[cursor_x][cursor_y] <= TileType8) {
  230. // click on a cleared cell with a number
  231. // count the flags around
  232. int flags = 0;
  233. for(int y = cursor_y - 1; y <= cursor_y + 1; y++) {
  234. for(int x = cursor_x - 1; x <= cursor_x + 1; x++) {
  235. if(x == cursor_x && y == cursor_y) {
  236. // we're on the cell the user selected, so ignore.
  237. continue;
  238. }
  239. // make sure we don't go OOB
  240. if(x >= 0 && x < PLAYFIELD_WIDTH && y >= 0 && y < PLAYFIELD_HEIGHT) {
  241. if(minesweeper_state->playfield[x][y] == TileTypeFlag) {
  242. flags++;
  243. }
  244. }
  245. }
  246. }
  247. int mines = minesweeper_state->playfield[cursor_x][cursor_y]; // ¯\_(ツ)_/¯
  248. if(flags == mines) {
  249. // auto uncover all non-flags around (to win faster ;)
  250. for(int auto_y = cursor_y - 1; auto_y <= cursor_y + 1; auto_y++) {
  251. for(int auto_x = cursor_x - 1; auto_x <= cursor_x + 1; auto_x++) {
  252. if(auto_x == cursor_x && auto_y == cursor_y) {
  253. continue;
  254. }
  255. if(auto_x >= 0 && auto_x < PLAYFIELD_WIDTH && auto_y >= 0 &&
  256. auto_y < PLAYFIELD_HEIGHT) {
  257. if(minesweeper_state->playfield[auto_x][auto_y] == TileTypeUncleared) {
  258. if(!play_move(minesweeper_state, auto_x, auto_y)) {
  259. // flags were wrong, we got a mine!
  260. return false;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. // we're done without hitting a mine - so return
  267. return true;
  268. }
  269. }
  270. // calculate number of surrounding mines.
  271. int hint = 0;
  272. for(int y = cursor_y - 1; y <= cursor_y + 1; y++) {
  273. for(int x = cursor_x - 1; x <= cursor_x + 1; x++) {
  274. if(x == cursor_x && y == cursor_y) {
  275. // we're on the cell the user selected, so ignore.
  276. continue;
  277. }
  278. // make sure we don't go OOB
  279. if(x >= 0 && x < PLAYFIELD_WIDTH && y >= 0 && y < PLAYFIELD_HEIGHT) {
  280. if(minesweeper_state->minefield[x][y] == FieldMine) {
  281. hint++;
  282. }
  283. }
  284. }
  285. }
  286. // 〜( ̄▽ ̄〜) don't judge me (〜 ̄▽ ̄)〜
  287. minesweeper_state->playfield[cursor_x][cursor_y] = hint;
  288. minesweeper_state->fields_cleared++;
  289. FURI_LOG_D("Minesweeper", "Setting %d,%d to %d", cursor_x, cursor_y, hint);
  290. if(hint == 0) {
  291. // the field is "empty"
  292. // auto open surrounding fields.
  293. for(int auto_y = cursor_y - 1; auto_y <= cursor_y + 1; auto_y++) {
  294. for(int auto_x = cursor_x - 1; auto_x <= cursor_x + 1; auto_x++) {
  295. if(auto_x == cursor_x && auto_y == cursor_y) {
  296. continue;
  297. }
  298. if(auto_x >= 0 && auto_x < PLAYFIELD_WIDTH && auto_y >= 0 &&
  299. auto_y < PLAYFIELD_HEIGHT) {
  300. if(minesweeper_state->playfield[auto_x][auto_y] == TileTypeUncleared) {
  301. play_move(minesweeper_state, auto_x, auto_y);
  302. }
  303. }
  304. }
  305. }
  306. }
  307. return true;
  308. }
  309. static void minesweeper_state_init(Minesweeper* const minesweeper_state) {
  310. minesweeper_state->cursor_x = minesweeper_state->cursor_y = 0;
  311. minesweeper_state->game_started = false;
  312. for(int y = 0; y < PLAYFIELD_HEIGHT; y++) {
  313. for(int x = 0; x < PLAYFIELD_WIDTH; x++) {
  314. minesweeper_state->playfield[x][y] = TileTypeUncleared;
  315. }
  316. }
  317. }
  318. int32_t minesweeper_app(void* p) {
  319. UNUSED(p);
  320. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  321. Minesweeper* minesweeper_state = malloc(sizeof(Minesweeper));
  322. // setup
  323. minesweeper_state_init(minesweeper_state);
  324. minesweeper_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  325. if(!minesweeper_state->mutex) {
  326. FURI_LOG_E("Minesweeper", "cannot create mutex\r\n");
  327. free(minesweeper_state);
  328. return 255;
  329. }
  330. // BEGIN IMPLEMENTATION
  331. minesweeper_state->dialogs = furi_record_open(RECORD_DIALOGS);
  332. minesweeper_state->notifications = furi_record_open(RECORD_NOTIFICATION);
  333. DialogMessage* message = dialog_message_alloc();
  334. dialog_message_set_header(message, "Minesweeper", 64, 3, AlignCenter, AlignTop);
  335. dialog_message_set_text(
  336. message,
  337. "Hold OK pressed to toggle flags.\ngithub.com/panki27",
  338. 64,
  339. 32,
  340. AlignCenter,
  341. AlignCenter);
  342. dialog_message_set_buttons(message, NULL, "Play", NULL);
  343. dialog_message_show(minesweeper_state->dialogs, message);
  344. dialog_message_free(message);
  345. // Set system callbacks
  346. ViewPort* view_port = view_port_alloc();
  347. view_port_draw_callback_set(view_port, render_callback, minesweeper_state);
  348. view_port_input_callback_set(view_port, input_callback, event_queue);
  349. // Open GUI and register view_port
  350. Gui* gui = furi_record_open(RECORD_GUI);
  351. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  352. // Call dolphin deed on game start
  353. dolphin_deed(DolphinDeedPluginGameStart);
  354. PluginEvent event;
  355. for(bool processing = true; processing;) {
  356. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  357. if(event_status == FuriStatusOk) {
  358. // press events
  359. if(event.type == EventTypeKey) {
  360. if(event.input.type == InputTypeShort) {
  361. switch(event.input.key) {
  362. case InputKeyUp:
  363. furi_mutex_acquire(minesweeper_state->mutex, FuriWaitForever);
  364. minesweeper_state->cursor_y--;
  365. if(minesweeper_state->cursor_y < 0) {
  366. minesweeper_state->cursor_y = PLAYFIELD_HEIGHT - 1;
  367. }
  368. furi_mutex_release(minesweeper_state->mutex);
  369. break;
  370. case InputKeyDown:
  371. furi_mutex_acquire(minesweeper_state->mutex, FuriWaitForever);
  372. minesweeper_state->cursor_y++;
  373. if(minesweeper_state->cursor_y >= PLAYFIELD_HEIGHT) {
  374. minesweeper_state->cursor_y = 0;
  375. }
  376. furi_mutex_release(minesweeper_state->mutex);
  377. break;
  378. case InputKeyRight:
  379. furi_mutex_acquire(minesweeper_state->mutex, FuriWaitForever);
  380. minesweeper_state->cursor_x++;
  381. if(minesweeper_state->cursor_x >= PLAYFIELD_WIDTH) {
  382. minesweeper_state->cursor_x = 0;
  383. }
  384. furi_mutex_release(minesweeper_state->mutex);
  385. break;
  386. case InputKeyLeft:
  387. furi_mutex_acquire(minesweeper_state->mutex, FuriWaitForever);
  388. minesweeper_state->cursor_x--;
  389. if(minesweeper_state->cursor_x < 0) {
  390. minesweeper_state->cursor_x = PLAYFIELD_WIDTH - 1;
  391. }
  392. furi_mutex_release(minesweeper_state->mutex);
  393. break;
  394. case InputKeyOk:
  395. if(!minesweeper_state->game_started) {
  396. setup_playfield(minesweeper_state);
  397. minesweeper_state->game_started = true;
  398. }
  399. if(!play_move(
  400. minesweeper_state,
  401. minesweeper_state->cursor_x,
  402. minesweeper_state->cursor_y)) {
  403. // ooops. looks like we hit a mine!
  404. if(game_lost(minesweeper_state)) {
  405. // player wants to restart.
  406. setup_playfield(minesweeper_state);
  407. } else {
  408. // player wants to exit :(
  409. processing = false;
  410. }
  411. } else {
  412. // check win condition.
  413. if(minesweeper_state->fields_cleared ==
  414. (PLAYFIELD_HEIGHT * PLAYFIELD_WIDTH) - MINECOUNT) {
  415. if(game_won(minesweeper_state)) {
  416. //player wants to restart
  417. setup_playfield(minesweeper_state);
  418. } else {
  419. processing = false;
  420. }
  421. }
  422. }
  423. break;
  424. case InputKeyBack:
  425. // Exit the plugin
  426. processing = false;
  427. break;
  428. default:
  429. break;
  430. }
  431. } else if(event.input.type == InputTypeLong) {
  432. // hold events
  433. FURI_LOG_D("Minesweeper", "Got a long press!");
  434. switch(event.input.key) {
  435. case InputKeyUp:
  436. case InputKeyDown:
  437. case InputKeyRight:
  438. case InputKeyLeft:
  439. break;
  440. case InputKeyOk:
  441. FURI_LOG_D("Minesweeper", "Toggling flag");
  442. furi_mutex_acquire(minesweeper_state->mutex, FuriWaitForever);
  443. place_flag(minesweeper_state);
  444. furi_mutex_release(minesweeper_state->mutex);
  445. break;
  446. case InputKeyBack:
  447. processing = false;
  448. break;
  449. default:
  450. break;
  451. }
  452. }
  453. }
  454. }
  455. view_port_update(view_port);
  456. }
  457. view_port_enabled_set(view_port, false);
  458. gui_remove_view_port(gui, view_port);
  459. furi_record_close(RECORD_GUI);
  460. furi_record_close(RECORD_DIALOGS);
  461. furi_record_close(RECORD_NOTIFICATION);
  462. view_port_free(view_port);
  463. furi_message_queue_free(event_queue);
  464. furi_mutex_free(minesweeper_state->mutex);
  465. free(minesweeper_state);
  466. return 0;
  467. }