game_reversi.c 11 KB

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