game_reversi.c 10.0 KB

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