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