game_reversi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 { 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. File* file = storage_file_alloc(storage);
  149. uint16_t bytes_readed = 0;
  150. if(storage_file_open(file, SAVING_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING)) {
  151. bytes_readed = storage_file_read(file, game_state, sizeof(GameState));
  152. }
  153. storage_file_close(file);
  154. storage_file_free(file);
  155. furi_record_close(RECORD_STORAGE);
  156. return bytes_readed == sizeof(GameState);
  157. }
  158. void save_game(const GameState* game_state) {
  159. Storage* storage = furi_record_open(RECORD_STORAGE);
  160. if(storage_common_stat(storage, SAVING_DIRECTORY, NULL) == FSE_NOT_EXIST) {
  161. if(!storage_simply_mkdir(storage, SAVING_DIRECTORY)) {
  162. return;
  163. }
  164. }
  165. File* file = storage_file_alloc(storage);
  166. if(storage_file_open(file, SAVING_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  167. storage_file_write(file, game_state, sizeof(GameState));
  168. }
  169. storage_file_close(file);
  170. storage_file_free(file);
  171. furi_record_close(RECORD_STORAGE);
  172. }
  173. bool handle_key_game(GameState* game_state, InputKey key) {
  174. switch(key) {
  175. case InputKeyBack:
  176. save_game(game_state);
  177. return false;
  178. break;
  179. case InputKeyOk:
  180. if(game_state->is_game_over) {
  181. init_game(game_state);
  182. save_game(game_state);
  183. } else {
  184. human_move(game_state);
  185. }
  186. break;
  187. case InputKeyUp:
  188. if(game_state->cursor_y > 0) {
  189. game_state->cursor_y--;
  190. } else {
  191. game_state->cursor_y = BOARD_SIZE - 1;
  192. }
  193. break;
  194. case InputKeyDown:
  195. if(game_state->cursor_y < BOARD_SIZE - 1) {
  196. game_state->cursor_y++;
  197. } else {
  198. game_state->cursor_y = 0;
  199. }
  200. break;
  201. case InputKeyLeft:
  202. if(game_state->cursor_x > 0) {
  203. game_state->cursor_x--;
  204. } else {
  205. game_state->cursor_x = BOARD_SIZE - 1;
  206. }
  207. break;
  208. case InputKeyRight:
  209. if(game_state->cursor_x < BOARD_SIZE - 1) {
  210. game_state->cursor_x++;
  211. } else {
  212. game_state->cursor_x = 0;
  213. }
  214. break;
  215. default:
  216. break;
  217. }
  218. return true;
  219. }
  220. bool handle_key_menu(AppState* app_state, InputKey key) {
  221. switch(key) {
  222. case InputKeyUp:
  223. if(app_state->selected_menu_item > 0) {
  224. app_state->selected_menu_item--;
  225. }
  226. break;
  227. case InputKeyDown:
  228. if(app_state->selected_menu_item < MENU_ITEMS_COUNT - 1) {
  229. app_state->selected_menu_item++;
  230. }
  231. break;
  232. case InputKeyOk:
  233. if(app_state->selected_menu_item == 1) {
  234. // new game
  235. init_game(&app_state->game);
  236. save_game(&app_state->game);
  237. }
  238. app_state->screen = AppScreenGame;
  239. break;
  240. default:
  241. break;
  242. }
  243. return true;
  244. }
  245. // returns `true` if the event loop should keep going
  246. bool handle_key(AppState* app_state, InputKey key) {
  247. GameState* game_state = &app_state->game;
  248. switch(app_state->screen) {
  249. case AppScreenGame:
  250. return handle_key_game(game_state, key);
  251. break;
  252. case AppScreenMenu:
  253. return handle_key_menu(app_state, key);
  254. break;
  255. }
  256. return true;
  257. }
  258. int32_t game_reversi_app() {
  259. AppState app_state;
  260. app_state.screen = AppScreenGame;
  261. if(!load_game(&app_state.game)) {
  262. init_game(&app_state.game);
  263. }
  264. app_state.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  265. if(!app_state.mutex) {
  266. return 255;
  267. }
  268. InputEvent input;
  269. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  270. ViewPort* view_port = view_port_alloc();
  271. view_port_draw_callback_set(view_port, draw_callback, &app_state);
  272. view_port_input_callback_set(view_port, input_callback, event_queue);
  273. Gui* gui = furi_record_open(RECORD_GUI);
  274. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  275. bool is_finished = false;
  276. while(!is_finished) {
  277. // check if it's computer's turn
  278. if(!app_state.game.is_game_over &&
  279. (app_state.game.current_player != app_state.game.human_color)) {
  280. computer_move(&app_state.game);
  281. }
  282. FuriStatus event_status = furi_message_queue_get(event_queue, &input, 100);
  283. if(event_status == FuriStatusOk) {
  284. // handle only press event, ignore repeat/release events
  285. if(input.type == InputTypeLong && input.key == InputKeyOk &&
  286. app_state.screen == AppScreenGame) {
  287. furi_mutex_acquire(app_state.mutex, FuriWaitForever);
  288. app_state.selected_menu_item = 0;
  289. app_state.screen = AppScreenMenu;
  290. furi_mutex_release(app_state.mutex);
  291. view_port_update(view_port);
  292. continue;
  293. }
  294. if(input.type != InputTypePress) continue;
  295. furi_mutex_acquire(app_state.mutex, FuriWaitForever);
  296. is_finished = !handle_key(&app_state, input.key);
  297. furi_mutex_release(app_state.mutex);
  298. view_port_update(view_port);
  299. }
  300. view_port_update(view_port);
  301. }
  302. gui_remove_view_port(gui, view_port);
  303. furi_record_close(RECORD_GUI);
  304. view_port_free(view_port);
  305. furi_message_queue_free(event_queue);
  306. furi_mutex_free(app_state.mutex);
  307. return 0;
  308. }