game_reversi.c 10 KB

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