game_reversi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Game "Reversi" for Flipper Zero
  2. // Copyright 2023 Dmitry Matyukhin
  3. #include <stdio.h>
  4. #include <furi.h>
  5. #include <gui/gui.h>
  6. #include <input/input.h>
  7. #include <storage/storage.h>
  8. #include "reversi.h"
  9. #define FRAME_LEFT 3
  10. #define FRAME_TOP 3
  11. #define FRAME_CELL_SIZE 7
  12. #define SAVING_DIRECTORY STORAGE_APP_DATA_PATH_PREFIX
  13. #define SAVING_FILENAME SAVING_DIRECTORY "/game_reversi.save"
  14. typedef enum {
  15. AppScreenGame,
  16. AppScreenMenu
  17. } AppScreen;
  18. typedef struct {
  19. GameState game;
  20. AppScreen screen;
  21. uint8_t selected_menu_item;
  22. FuriMutex* mutex;
  23. } AppState;
  24. #define MENU_ITEMS_COUNT 2
  25. static const char* popup_menu_strings[] = {"Resume", "New Game"};
  26. static void draw_menu(Canvas* const canvas, const AppState* app_state);
  27. static void gray_canvas(Canvas* const canvas);
  28. static void input_callback(InputEvent* input_event, void* ctx) {
  29. furi_assert(ctx);
  30. FuriMessageQueue* event_queue = ctx;
  31. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  32. }
  33. static void draw_callback(Canvas *const canvas, void *ctx) {
  34. furi_assert(ctx);
  35. const AppState *app_state = ctx;
  36. furi_mutex_acquire(app_state->mutex, FuriWaitForever);
  37. if (app_state == NULL)
  38. return;
  39. const GameState *game_state = &app_state->game;
  40. canvas_clear(canvas);
  41. canvas_set_color(canvas, ColorBlack);
  42. for (uint8_t i = 0; i <= BOARD_SIZE; i++) {
  43. canvas_draw_line(canvas, FRAME_LEFT + FRAME_CELL_SIZE * i, FRAME_TOP,
  44. FRAME_LEFT + FRAME_CELL_SIZE * i,
  45. FRAME_TOP + FRAME_CELL_SIZE * BOARD_SIZE);
  46. canvas_draw_line(canvas, FRAME_LEFT, FRAME_TOP + FRAME_CELL_SIZE * i,
  47. FRAME_LEFT + FRAME_CELL_SIZE * BOARD_SIZE,
  48. FRAME_TOP + FRAME_CELL_SIZE * i);
  49. }
  50. //
  51. // draw cursor
  52. canvas_set_color(canvas, ColorWhite);
  53. canvas_draw_frame(canvas, FRAME_LEFT + FRAME_CELL_SIZE * game_state->cursor_x,
  54. FRAME_TOP + FRAME_CELL_SIZE * game_state->cursor_y,
  55. FRAME_CELL_SIZE + 1, FRAME_CELL_SIZE + 1);
  56. canvas_set_color(canvas, ColorBlack);
  57. // draw pieces
  58. int blacks = 0, whites = 0;
  59. const int radius = FRAME_CELL_SIZE >> 1;
  60. for (uint8_t i = 0; i < BOARD_SIZE; i++) {
  61. for (uint8_t j = 0; j < BOARD_SIZE; j++) {
  62. if (!game_state->board[i][j]) {
  63. continue;
  64. }
  65. if (game_state->board[i][j] == BLACK) {
  66. canvas_draw_disc(
  67. canvas,
  68. FRAME_LEFT + FRAME_CELL_SIZE * i + radius + 1,
  69. FRAME_TOP + FRAME_CELL_SIZE * j + radius + 1,
  70. radius);
  71. blacks++;
  72. } else {
  73. canvas_draw_circle(
  74. canvas,
  75. FRAME_LEFT + FRAME_CELL_SIZE * i + radius + 1,
  76. FRAME_TOP + FRAME_CELL_SIZE * j + radius + 1,
  77. radius);
  78. whites++;
  79. }
  80. }
  81. }
  82. canvas_set_font(canvas, FontPrimary);
  83. // draw score
  84. char score_str[25];
  85. memset(score_str, 0, sizeof(score_str));
  86. snprintf(score_str, sizeof(score_str), "%d - %d", whites, blacks);
  87. canvas_draw_str_aligned(canvas, 70, 3, AlignLeft, AlignTop, score_str);
  88. canvas_set_font(canvas, FontSecondary);
  89. if (game_state->is_game_over) {
  90. canvas_draw_str_aligned(canvas, 70, 20, AlignLeft, AlignTop, "Game over");
  91. canvas_draw_str_aligned(canvas,
  92. 70,
  93. FRAME_TOP + FRAME_CELL_SIZE * BOARD_SIZE,
  94. AlignLeft, AlignBottom,
  95. "Press OK");
  96. canvas_set_font(canvas, FontPrimary);
  97. if (whites == blacks) {
  98. canvas_draw_str_aligned(canvas, 70, 30, AlignLeft, AlignTop, "DRAW");
  99. } else if (((game_state->human_color == WHITE) && whites > blacks) ||
  100. ((game_state->human_color == BLACK) && blacks > whites)) {
  101. canvas_draw_str_aligned(canvas, 70, 30, AlignLeft, AlignTop, "YOU WIN");
  102. } else {
  103. canvas_draw_str_aligned(canvas, 70, 30, AlignLeft, AlignTop, "YOU LOSE");
  104. }
  105. } else if (game_state->current_player == game_state->human_color) {
  106. canvas_draw_str_aligned(canvas, 70, 12, AlignLeft, AlignTop, "Your turn");
  107. } else {
  108. canvas_draw_str_aligned(canvas, 70, 12, AlignLeft, AlignTop,
  109. "Computer turn");
  110. }
  111. if (app_state->screen == AppScreenMenu) {
  112. draw_menu(canvas, app_state);
  113. }
  114. furi_mutex_release(app_state->mutex);
  115. }
  116. static void draw_menu(Canvas* const canvas, const AppState* app_state) {
  117. gray_canvas(canvas);
  118. canvas_set_color(canvas, ColorWhite);
  119. canvas_draw_rbox(canvas, 28, 16, 72, 32, 4);
  120. canvas_set_color(canvas, ColorBlack);
  121. canvas_draw_rframe(canvas, 28, 16, 72, 32, 4);
  122. for (int i = 0; i < MENU_ITEMS_COUNT; i++) {
  123. if (i == app_state->selected_menu_item) {
  124. canvas_set_color(canvas, ColorBlack);
  125. canvas_draw_box(canvas, 34, 20 + 12 * i, 60, 12);
  126. }
  127. canvas_set_color(canvas, i == app_state->selected_menu_item ? ColorWhite
  128. : ColorBlack);
  129. canvas_draw_str_aligned(canvas, 64, 26 + 12 * i, AlignCenter, AlignCenter,
  130. popup_menu_strings[i]);
  131. }
  132. }
  133. static void gray_canvas(Canvas* const canvas) {
  134. canvas_set_color(canvas, ColorWhite);
  135. for(int x = 0; x < 128; x += 2) {
  136. for(int y = 0; y < 64; y++) {
  137. canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y);
  138. }
  139. }
  140. }
  141. bool load_game(GameState* game_state) {
  142. Storage* storage = furi_record_open(RECORD_STORAGE);
  143. storage_common_migrate(storage, EXT_PATH("apps/Games/game_reversi.save"), SAVING_FILENAME);
  144. File* file = storage_file_alloc(storage);
  145. uint16_t bytes_readed = 0;
  146. if(storage_file_open(file, SAVING_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING)) {
  147. bytes_readed = storage_file_read(file, game_state, sizeof(GameState));
  148. }
  149. storage_file_close(file);
  150. storage_file_free(file);
  151. furi_record_close(RECORD_STORAGE);
  152. return bytes_readed == sizeof(GameState);
  153. }
  154. void save_game(const GameState* game_state) {
  155. Storage* storage = furi_record_open(RECORD_STORAGE);
  156. File* file = storage_file_alloc(storage);
  157. if(storage_file_open(file, SAVING_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  158. storage_file_write(file, game_state, sizeof(GameState));
  159. }
  160. storage_file_close(file);
  161. storage_file_free(file);
  162. furi_record_close(RECORD_STORAGE);
  163. }
  164. bool handle_key_game(GameState *game_state, InputKey key) {
  165. switch (key) {
  166. case InputKeyBack:
  167. save_game(game_state);
  168. return false;
  169. break;
  170. case InputKeyOk:
  171. if (game_state->is_game_over) {
  172. init_game(game_state);
  173. save_game(game_state);
  174. } else {
  175. human_move(game_state);
  176. }
  177. break;
  178. case InputKeyUp:
  179. if (game_state->cursor_y > 0) {
  180. game_state->cursor_y--;
  181. } else {
  182. game_state->cursor_y = BOARD_SIZE - 1;
  183. }
  184. break;
  185. case InputKeyDown:
  186. if (game_state->cursor_y < BOARD_SIZE - 1) {
  187. game_state->cursor_y++;
  188. } else {
  189. game_state->cursor_y = 0;
  190. }
  191. break;
  192. case InputKeyLeft:
  193. if (game_state->cursor_x > 0) {
  194. game_state->cursor_x--;
  195. } else {
  196. game_state->cursor_x = BOARD_SIZE - 1;
  197. }
  198. break;
  199. case InputKeyRight:
  200. if (game_state->cursor_x < BOARD_SIZE - 1) {
  201. game_state->cursor_x++;
  202. } else {
  203. game_state->cursor_x = 0;
  204. }
  205. break;
  206. default:
  207. break;
  208. }
  209. return true;
  210. }
  211. bool handle_key_menu(AppState *app_state, InputKey key) {
  212. switch (key) {
  213. case InputKeyUp:
  214. if (app_state->selected_menu_item > 0) {
  215. app_state->selected_menu_item--;
  216. }
  217. break;
  218. case InputKeyDown:
  219. if (app_state->selected_menu_item < MENU_ITEMS_COUNT - 1) {
  220. app_state->selected_menu_item++;
  221. }
  222. break;
  223. case InputKeyOk:
  224. if (app_state->selected_menu_item == 1) {
  225. // new game
  226. init_game(&app_state->game);
  227. save_game(&app_state->game);
  228. }
  229. app_state->screen = AppScreenGame;
  230. break;
  231. default:
  232. break;
  233. }
  234. return true;
  235. }
  236. // returns `true` if the event loop should keep going
  237. bool handle_key(AppState* app_state, InputKey key) {
  238. GameState* game_state = &app_state->game;
  239. switch (app_state->screen) {
  240. case AppScreenGame:
  241. return handle_key_game(game_state, key);
  242. break;
  243. case AppScreenMenu:
  244. return handle_key_menu(app_state, key);
  245. break;
  246. }
  247. return true;
  248. }
  249. int32_t game_reversi_app() {
  250. AppState app_state;
  251. app_state.screen = AppScreenGame;
  252. if (!load_game(&app_state.game)) {
  253. init_game(&app_state.game);
  254. }
  255. app_state.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  256. if (!app_state.mutex) {
  257. return 255;
  258. }
  259. InputEvent input;
  260. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  261. ViewPort* view_port = view_port_alloc();
  262. view_port_draw_callback_set(view_port, draw_callback, &app_state);
  263. view_port_input_callback_set(view_port, input_callback, event_queue);
  264. Gui* gui = furi_record_open(RECORD_GUI);
  265. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  266. bool is_finished = false;
  267. while(!is_finished) {
  268. // check if it's computer's turn
  269. if (!app_state.game.is_game_over && (app_state.game.current_player != app_state.game.human_color)) {
  270. computer_move(&app_state.game);
  271. }
  272. FuriStatus event_status = furi_message_queue_get(event_queue, &input, 100);
  273. if(event_status == FuriStatusOk) {
  274. // handle only press event, ignore repeat/release events
  275. if (input.type == InputTypeLong && input.key == InputKeyOk && app_state.screen == AppScreenGame) {
  276. furi_mutex_acquire(app_state.mutex, FuriWaitForever);
  277. app_state.selected_menu_item = 0;
  278. app_state.screen = AppScreenMenu;
  279. view_port_update(view_port);
  280. furi_mutex_release(app_state.mutex);
  281. continue;
  282. }
  283. if (input.type != InputTypePress) continue;
  284. furi_mutex_acquire(app_state.mutex, FuriWaitForever);
  285. is_finished = !handle_key(&app_state, input.key);
  286. view_port_update(view_port);
  287. furi_mutex_release(app_state.mutex);
  288. }
  289. view_port_update(view_port);
  290. }
  291. gui_remove_view_port(gui, view_port);
  292. furi_record_close(RECORD_GUI);
  293. view_port_free(view_port);
  294. furi_message_queue_free(event_queue);
  295. furi_mutex_free(app_state.mutex);
  296. return 0;
  297. }