game_reversi.c 11 KB

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