game15.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <notification/notification.h>
  4. #include <notification/notification_messages.h>
  5. #include <storage/storage.h>
  6. #include "sandbox.h"
  7. #define FPS 20
  8. #define CELL_WIDTH 10
  9. #define CELL_HEIGHT 8
  10. #define MOVE_TICKS 5
  11. #define KEY_STACK_SIZE 16
  12. #define TOP_RECORD_DIRECTORY "/ext/apps/Games"
  13. #define TOP_RECORD_FILENAME TOP_RECORD_DIRECTORY "/game15.top"
  14. typedef enum {
  15. DirectionNone,
  16. DirectionUp,
  17. DirectionDown,
  18. DirectionLeft,
  19. DirectionRight
  20. } direction_e;
  21. typedef enum { ScenePlay, SceneWin } scene_e;
  22. typedef struct {
  23. uint8_t cell_index;
  24. uint8_t zero_index;
  25. uint8_t move_direction;
  26. uint8_t move_ticks;
  27. } moving_cell_t;
  28. static scene_e scene;
  29. static uint8_t board[16];
  30. static uint16_t top_record;
  31. static uint16_t move_count;
  32. static uint32_t tick_count;
  33. static NotificationApp* notification;
  34. static moving_cell_t moving_cell;
  35. static uint8_t keys[KEY_STACK_SIZE];
  36. static uint8_t key_stack_head = 0;
  37. static const uint8_t pic_cells[] = {
  38. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0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0xfd, 0x8c, 0xfd, 0x8c, 0xfd, 0x8c, 0xfd, 0x8c, 0xfd,
  50. 0xe6, 0xfd, 0x37, 0xff, 0x36, 0xff, 0x86, 0xfd, 0xc6, 0xfc, 0x66, 0xfc, 0x36, 0xfc, 0xf6, 0xff,
  51. 0xe6, 0xfd, 0x37, 0xff, 0x36, 0xff, 0x86, 0xfd, 0x06, 0xff, 0x36, 0xff, 0x36, 0xff, 0xe6, 0xfd,
  52. 0xc6, 0xfd, 0xe7, 0xfd, 0xa6, 0xfd, 0xb6, 0xfd, 0xb6, 0xfd, 0xb6, 0xff, 0xf6, 0xff, 0x86, 0xfd,
  53. 0xf6, 0xff, 0x37, 0xfc, 0x36, 0xfc, 0xf6, 0xfd, 0x06, 0xff, 0x36, 0xff, 0x36, 0xff, 0xe6, 0xfd,
  54. };
  55. static const uint8_t pic_digits[] = {
  56. 0xf0, 0xf2, 0xf2, 0xf2, 0xf2, 0xf0, 0xf9, 0xf8, 0xf9, 0xf9, 0xf9, 0xf0, 0xf0, 0xf2, 0xf3,
  57. 0xf1, 0xfc, 0xf0, 0xf0, 0xf3, 0xf1, 0xf3, 0xf2, 0xf0, 0xf3, 0xf1, 0xf2, 0xf2, 0xf0, 0xf3,
  58. 0xf0, 0xfc, 0xf0, 0xf3, 0xf2, 0xf0, 0x00, 0x0c, 0x00, 0x02, 0x02, 0x00, 0x00, 0x03, 0x03,
  59. 0x03, 0x03, 0x03, 0x00, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x02, 0x00, 0x03, 0x03, 0x00,
  60. };
  61. static const uint8_t pic_top[] = {11, 4, 0x88, 0xf8, 0xad, 0xfa, 0xad, 0xf8, 0x8d, 0xfe};
  62. static const uint8_t pic_move[] =
  63. {17, 4, 0x2e, 0x2a, 0xfe, 0xa4, 0xaa, 0xff, 0xaa, 0x2a, 0xff, 0x2e, 0x36, 0xfe};
  64. static const uint8_t pic_time[] = {15, 4, 0xa8, 0x8b, 0x2d, 0xe9, 0xad, 0xca, 0xad, 0x8b};
  65. static const uint8_t pic_puzzled[] = {
  66. 0xff, 0xcf, 0x00, 0xf3, 0xff, 0xfc, 0x3f, 0x03, 0xc0, 0xff, 0xf3, 0x0f, 0xdc, 0xff, 0xcf,
  67. 0x00, 0xf3, 0xff, 0xfc, 0x3f, 0x03, 0xc0, 0xff, 0xf3, 0x0f, 0xdc, 0x03, 0xcc, 0x00, 0x03,
  68. 0x38, 0x00, 0x0e, 0x03, 0xc0, 0x00, 0x30, 0x30, 0xdc, 0x03, 0xcc, 0x00, 0x03, 0x1c, 0x00,
  69. 0x07, 0x03, 0xc0, 0x00, 0x30, 0x30, 0xdc, 0xff, 0xcf, 0x00, 0x03, 0x0e, 0x80, 0x03, 0x03,
  70. 0xc0, 0xff, 0x33, 0xc0, 0xdc, 0xff, 0xcf, 0x00, 0x03, 0x07, 0xc0, 0x01, 0x03, 0xc0, 0xff,
  71. 0x33, 0xc0, 0xdc, 0x03, 0xc0, 0x00, 0x83, 0x03, 0xe0, 0x00, 0x03, 0xc0, 0x00, 0x30, 0xc0,
  72. 0xd0, 0x03, 0xc0, 0x00, 0xc3, 0x01, 0x70, 0x00, 0x03, 0xc0, 0x00, 0x30, 0xc0, 0xd0, 0x03,
  73. 0xc0, 0xff, 0xf3, 0xff, 0xfc, 0x3f, 0xff, 0xcf, 0xff, 0xf3, 0xff, 0xdc, 0x03, 0xc0, 0xff,
  74. 0xf3, 0xff, 0xfc, 0x3f, 0xff, 0xcf, 0xff, 0xf3, 0xff, 0xdc};
  75. static void key_stack_init() {
  76. key_stack_head = 0;
  77. }
  78. static uint8_t key_stack_pop() {
  79. return keys[--key_stack_head];
  80. }
  81. static bool key_stack_is_empty() {
  82. return key_stack_head == 0;
  83. }
  84. static int key_stack_push(uint8_t value) {
  85. if(key_stack_head != KEY_STACK_SIZE) {
  86. keys[key_stack_head] = value;
  87. key_stack_head++;
  88. return key_stack_head;
  89. } else
  90. return -1;
  91. }
  92. static void storage_top_record_load() {
  93. Storage* storage = furi_record_open(RECORD_STORAGE);
  94. File* file = storage_file_alloc(storage);
  95. if(storage_file_open(file, TOP_RECORD_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING))
  96. storage_file_read(file, &top_record, 2);
  97. storage_file_close(file);
  98. storage_file_free(file);
  99. furi_record_close(RECORD_STORAGE);
  100. }
  101. static void storage_top_record_save() {
  102. Storage* storage = furi_record_open(RECORD_STORAGE);
  103. if(storage_common_stat(storage, TOP_RECORD_DIRECTORY, NULL) == FSE_NOT_EXIST) {
  104. if(!storage_simply_mkdir(storage, TOP_RECORD_DIRECTORY)) {
  105. return;
  106. }
  107. }
  108. File* file = storage_file_alloc(storage);
  109. if(storage_file_open(file, TOP_RECORD_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  110. storage_file_write(file, &top_record, 2);
  111. storage_file_close(file);
  112. storage_file_free(file);
  113. furi_record_close(RECORD_STORAGE);
  114. }
  115. static void set_moving_cell_by_direction(direction_e direction) {
  116. moving_cell.move_direction = DirectionNone;
  117. moving_cell.zero_index = 0xff;
  118. for(int i = 0; i < 16; i++) {
  119. if(!board[i]) {
  120. moving_cell.zero_index = i;
  121. break;
  122. }
  123. }
  124. if(moving_cell.zero_index == 0xff) return;
  125. uint8_t x = moving_cell.zero_index % 4;
  126. uint8_t y = moving_cell.zero_index / 4;
  127. moving_cell.cell_index = moving_cell.zero_index;
  128. if(direction == DirectionUp && y < 3)
  129. moving_cell.cell_index += 4;
  130. else if(direction == DirectionDown && y > 0)
  131. moving_cell.cell_index -= 4;
  132. else if(direction == DirectionLeft && x < 3)
  133. moving_cell.cell_index++;
  134. else if(direction == DirectionRight && x > 0)
  135. moving_cell.cell_index--;
  136. else
  137. return;
  138. moving_cell.move_ticks = 0;
  139. moving_cell.move_direction = direction;
  140. }
  141. static bool is_board_has_solution() {
  142. uint8_t i, j, inv = 0;
  143. for(i = 0; i < 16; ++i)
  144. if(board[i])
  145. for(j = 0; j < i; ++j)
  146. if(board[j] > board[i]) ++inv;
  147. for(i = 0; i < 16; ++i)
  148. if(board[i] == 0) inv += 1 + i / 4;
  149. return inv % 2 == 0;
  150. }
  151. static void board_init() {
  152. for(int i = 0; i < 16; i++) {
  153. board[i] = (i + 1) % 16;
  154. }
  155. do {
  156. for(int i = 15; i >= 1; i--) {
  157. int j = rand() % (i + 1);
  158. uint8_t tmp = board[j];
  159. board[j] = board[i];
  160. board[i] = tmp;
  161. }
  162. } while(!is_board_has_solution());
  163. }
  164. static void game_init() {
  165. scene = ScenePlay;
  166. move_count = 0;
  167. tick_count = 0;
  168. storage_top_record_load();
  169. moving_cell.move_direction = DirectionNone;
  170. board_init();
  171. key_stack_init();
  172. }
  173. static bool is_board_solved() {
  174. for(int i = 0; i < 16; i++)
  175. if(((i + 1) % 16) != board[i]) return false;
  176. return true;
  177. }
  178. static void game_tick() {
  179. switch(scene) {
  180. case ScenePlay:
  181. tick_count++;
  182. if(moving_cell.move_direction == DirectionNone && !key_stack_is_empty()) {
  183. set_moving_cell_by_direction(key_stack_pop());
  184. if(moving_cell.move_direction == DirectionNone) {
  185. notification_message(notification, &sequence_single_vibro);
  186. key_stack_init();
  187. }
  188. }
  189. if(moving_cell.move_direction != DirectionNone) {
  190. moving_cell.move_ticks++;
  191. if(moving_cell.move_ticks == MOVE_TICKS) {
  192. board[moving_cell.zero_index] = board[moving_cell.cell_index];
  193. board[moving_cell.cell_index] = 0;
  194. moving_cell.move_direction = DirectionNone;
  195. move_count++;
  196. }
  197. if(is_board_solved()) {
  198. notification_message(notification, &sequence_double_vibro);
  199. if(move_count < top_record || top_record == 0) {
  200. top_record = move_count;
  201. storage_top_record_save();
  202. }
  203. scene = SceneWin;
  204. }
  205. }
  206. break;
  207. case SceneWin:
  208. if(!key_stack_is_empty()) game_init();
  209. break;
  210. }
  211. }
  212. static void draw_cell(Canvas* canvas, uint8_t x, uint8_t y, uint8_t cell_number) {
  213. canvas_set_color(canvas, ColorBlack);
  214. canvas_draw_rframe(canvas, x, y, 18, 14, 1);
  215. canvas_set_color(canvas, ColorBlack);
  216. canvas_draw_xbm(canvas, x + 4, y + 3, CELL_WIDTH, CELL_HEIGHT, pic_cells + cell_number * 16);
  217. }
  218. static void board_draw(Canvas* canvas) {
  219. for(int i = 0; i < 16; i++) {
  220. if(board[i]) {
  221. if(moving_cell.move_direction == DirectionNone || moving_cell.cell_index != i)
  222. draw_cell(canvas, (i % 4) * 20 + 7, (i / 4) * 16 + 1, board[i]);
  223. if(moving_cell.move_direction != DirectionNone && moving_cell.cell_index == i) {
  224. uint8_t from_x = (moving_cell.cell_index % 4) * 20 + 7;
  225. uint8_t from_y = (moving_cell.cell_index / 4) * 16 + 1;
  226. uint8_t to_x = (moving_cell.zero_index % 4) * 20 + 7;
  227. uint8_t to_y = (moving_cell.zero_index / 4) * 16 + 1;
  228. int now_x = from_x + (to_x - from_x) * moving_cell.move_ticks / MOVE_TICKS;
  229. int now_y = from_y + (to_y - from_y) * moving_cell.move_ticks / MOVE_TICKS;
  230. draw_cell(canvas, now_x, now_y, board[i]);
  231. }
  232. }
  233. }
  234. }
  235. static void number_draw(Canvas* canvas, uint8_t y, uint32_t value) {
  236. uint8_t x = 121;
  237. while(true) {
  238. uint8_t digit = value % 10;
  239. canvas_draw_xbm(canvas, x, y, 4, 6, pic_digits + digit * 6);
  240. x -= 5;
  241. value = value / 10;
  242. if(!value) break;
  243. }
  244. }
  245. static void plate_draw(
  246. Canvas* canvas,
  247. uint8_t y,
  248. const uint8_t* header,
  249. uint32_t value,
  250. bool dont_draw_zero_value) {
  251. canvas_set_color(canvas, ColorBlack);
  252. canvas_draw_rbox(canvas, 92, y, 35, 19, 2);
  253. canvas_set_color(canvas, ColorBlack);
  254. canvas_draw_xbm(canvas, 95, y + 3, header[0], header[1], &header[2]);
  255. if((!value && !dont_draw_zero_value) || value) number_draw(canvas, y + 10, value);
  256. }
  257. static void info_draw(Canvas* canvas) {
  258. plate_draw(canvas, 1, pic_top, top_record, true);
  259. plate_draw(canvas, 22, pic_move, move_count, false);
  260. plate_draw(canvas, 43, pic_time, tick_count / FPS, false);
  261. }
  262. static void render_callback(Canvas* const canvas) {
  263. canvas_set_color(canvas, ColorWhite);
  264. canvas_draw_box(canvas, 0, 0, 128, 64);
  265. if(scene == ScenePlay || scene == SceneWin) {
  266. canvas_set_color(canvas, ColorBlack);
  267. board_draw(canvas);
  268. info_draw(canvas);
  269. canvas_set_color(canvas, ColorWhite);
  270. canvas_draw_box(canvas, 0, 0, 128, 64);
  271. canvas_set_color(canvas, ColorBlack);
  272. canvas_draw_str(canvas, 10, 10, "0123456789");
  273. }
  274. if(scene == SceneWin) {
  275. canvas_set_color(canvas, ColorWhite);
  276. for(int x = 0; x < 128; x += 2) {
  277. for(int y = 0; y < 64; y++) {
  278. canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y);
  279. }
  280. }
  281. canvas_draw_box(canvas, 7, 20, 114, 24);
  282. canvas_set_color(canvas, ColorBlack);
  283. canvas_draw_box(canvas, 8, 21, 112, 22);
  284. canvas_set_color(canvas, ColorWhite);
  285. canvas_draw_box(canvas, 10, 23, 108, 18);
  286. canvas_set_color(canvas, ColorBlack);
  287. canvas_draw_xbm(canvas, 14, 27, 100, 10, pic_puzzled);
  288. }
  289. }
  290. static void game_event_handler(GameEvent const event) {
  291. if(event.type == EventTypeKey) {
  292. if(event.input.type == InputTypePress) {
  293. switch(event.input.key) {
  294. case InputKeyUp:
  295. key_stack_push(DirectionUp);
  296. break;
  297. case InputKeyDown:
  298. key_stack_push(DirectionDown);
  299. break;
  300. case InputKeyRight:
  301. key_stack_push(DirectionRight);
  302. break;
  303. case InputKeyLeft:
  304. key_stack_push(DirectionLeft);
  305. break;
  306. case InputKeyOk:
  307. key_stack_push(DirectionNone);
  308. break;
  309. case InputKeyBack:
  310. sandbox_loop_exit();
  311. break;
  312. }
  313. }
  314. } else if(event.type == EventTypeTick) {
  315. game_tick();
  316. }
  317. }
  318. static void game_alloc() {
  319. srand(DWT->CYCCNT);
  320. key_stack_init();
  321. notification = furi_record_open(RECORD_NOTIFICATION);
  322. notification_message_block(notification, &sequence_display_backlight_enforce_on);
  323. }
  324. static void game_free() {
  325. notification_message_block(notification, &sequence_display_backlight_enforce_auto);
  326. furi_record_close(RECORD_NOTIFICATION);
  327. }
  328. int32_t game15_app() {
  329. game_alloc();
  330. game_init();
  331. sandbox_init(
  332. FPS, (SandboxRenderCallback)render_callback, (SandboxEventHandler)game_event_handler);
  333. sandbox_loop();
  334. sandbox_free();
  335. game_free();
  336. return 0;
  337. }