game_reversi.c 11 KB

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