minesweeper.c 17 KB

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