minesweeper.c 16 KB

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