Ver Fonte

Flipper Zero GUI

Dmitry Matyukhin há 2 anos atrás
pai
commit
88ecbdd7c7
10 ficheiros alterados com 578 adições e 214 exclusões
  1. 1 0
      .gitignore
  2. 17 4
      README.md
  3. 14 0
      application.fam
  4. 347 0
      game_reversi.c
  5. BIN
      game_reversi.png
  6. BIN
      images/game.png
  7. BIN
      images/menu.png
  8. 0 210
      main.c
  9. 178 0
      reversi.c
  10. 21 0
      reversi.h

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
 reversi
+compile_flags.txt

+ 17 - 4
README.md

@@ -1,12 +1,25 @@
 # Reversi game for Flipper Zero
-## Prerequisites
-TBD
+
+![Game screen](images/game.png)
+![Menu screen](images/menu.png)
 
 ## Compile
-TBD
+1. Clone [firmware](https://github.com/flipperdevices/flipperzero-firmware)
+2. Go to the `applications_user` directory
+3. Create a symlink to this repo assuming that these two repos are on the same level:
+```sh
+ln -s ../../flipperzero-reversi flipperzero-reversi
+```
+4. From the main directory of the firmware run:
+```sh
+./fbt faps
+```
 
 ## Install
-TBD
+1. Open folder `./build/f7-firmware-D/.extapps/` from the firmware directory
+2. Open qFlipper
+3. Open File Manager, SD Card/apps/games
+4. Drag `game_reversi.fapp` to qFlipper
 
 ## Thanks to:
 - [2048 game](https://github.com/eugene-kirzhanov/flipper-zero-2048-game)

+ 14 - 0
application.fam

@@ -0,0 +1,14 @@
+App(
+    appid="game_reversi",
+    name="Reversi",
+    apptype=FlipperAppType.EXTERNAL,
+    entry_point="game_reversi_app",
+    cdefines=["APP_GAME_REVERSI"],
+    requires=[
+        "gui",
+    ],
+    stack_size=1 * 1024,
+    order=90,
+    fap_icon="game_reversi.png",
+    fap_category="Games"
+)

+ 347 - 0
game_reversi.c

@@ -0,0 +1,347 @@
+// Game "Reversi" for Flipper Zero
+// Copyright 2023 Dmitry Matyukhin
+
+#include <stdio.h>
+#include <furi.h>
+#include <gui/gui.h>
+#include <input/input.h>
+#include <storage/storage.h>
+#include "reversi.h"
+
+#define FRAME_LEFT 3
+#define FRAME_TOP 3
+#define FRAME_CELL_SIZE 7
+
+#define SAVING_DIRECTORY "/ext/apps/Games"
+#define SAVING_FILENAME SAVING_DIRECTORY "/game_reversi.save"
+
+typedef enum {
+  AppScreenGame,
+  AppScreenMenu
+} AppScreen;
+
+typedef struct {
+  GameState game;
+  AppScreen screen;
+  uint8_t selected_menu_item;
+} AppState;
+
+#define MENU_ITEMS_COUNT 2
+static const char* popup_menu_strings[] = {"Resume", "New Game"};
+
+static void draw_menu(Canvas* const canvas, const AppState* app_state);
+static void gray_canvas(Canvas* const canvas);
+
+static void input_callback(InputEvent* input_event, void* ctx) {
+    furi_assert(ctx);
+    FuriMessageQueue* event_queue = ctx;
+    furi_message_queue_put(event_queue, input_event, FuriWaitForever);
+}
+
+static void draw_callback(Canvas *const canvas, void *ctx) {
+  furi_assert(ctx);
+
+  const AppState *app_state = acquire_mutex((ValueMutex *)ctx, 25);
+  if (app_state == NULL)
+    return;
+  const GameState *game_state = &app_state->game;
+
+  canvas_clear(canvas);
+  canvas_set_color(canvas, ColorBlack);
+
+  for (uint8_t i = 0; i <= BOARD_SIZE; i++) {
+    canvas_draw_line(canvas, FRAME_LEFT + FRAME_CELL_SIZE * i, FRAME_TOP,
+                     FRAME_LEFT + FRAME_CELL_SIZE * i,
+                     FRAME_TOP + FRAME_CELL_SIZE * BOARD_SIZE);
+    canvas_draw_line(canvas, FRAME_LEFT, FRAME_TOP + FRAME_CELL_SIZE * i,
+                     FRAME_LEFT + FRAME_CELL_SIZE * BOARD_SIZE,
+                     FRAME_TOP + FRAME_CELL_SIZE * i);
+  }
+  //
+  // draw cursor
+  canvas_set_color(canvas, ColorWhite);
+  canvas_draw_frame(canvas, FRAME_LEFT + FRAME_CELL_SIZE * game_state->cursor_x,
+                    FRAME_TOP + FRAME_CELL_SIZE * game_state->cursor_y,
+                    FRAME_CELL_SIZE + 1, FRAME_CELL_SIZE + 1);
+
+  canvas_set_color(canvas, ColorBlack);
+  // draw pieces
+  int blacks = 0, whites = 0;
+  const int radius = FRAME_CELL_SIZE >> 1;
+  for (uint8_t i = 0; i < BOARD_SIZE; i++) {
+    for (uint8_t j = 0; j < BOARD_SIZE; j++) {
+      if (!game_state->board[i][j]) {
+        continue;
+      }
+      if (game_state->board[i][j] == BLACK) {
+        canvas_draw_disc(
+            canvas, 
+            FRAME_LEFT + FRAME_CELL_SIZE * i + radius + 1,
+            FRAME_TOP + FRAME_CELL_SIZE * j + radius + 1, 
+            radius);
+        blacks++;
+      } else {
+        canvas_draw_circle(
+            canvas, 
+            FRAME_LEFT + FRAME_CELL_SIZE * i + radius + 1,
+            FRAME_TOP + FRAME_CELL_SIZE * j + radius + 1, 
+            radius);
+        whites++;
+      }
+    }
+  }
+
+  canvas_set_font(canvas, FontPrimary);
+  // draw score
+  char score_str[10];
+  memset(score_str, 0, sizeof(score_str));
+  snprintf(score_str, sizeof(score_str), "%d - %d", whites, blacks);
+
+  canvas_draw_str_aligned(canvas, 70, 3, AlignLeft, AlignTop, score_str);
+
+  canvas_set_font(canvas, FontSecondary);
+  if (game_state->is_game_over) {
+    canvas_draw_str_aligned(canvas, 70, 20, AlignLeft, AlignTop, "Game over");
+
+    canvas_draw_str_aligned(canvas, 
+        70, 
+        FRAME_TOP + FRAME_CELL_SIZE * BOARD_SIZE, 
+        AlignLeft, AlignBottom, 
+        "Press OK");
+
+    canvas_set_font(canvas, FontPrimary);
+
+    if (whites == blacks) {
+      canvas_draw_str_aligned(canvas, 70, 30, AlignLeft, AlignTop, "DRAW");
+    } else if (((game_state->human_color == WHITE) && whites > blacks) ||
+               ((game_state->human_color == BLACK) && blacks > whites)) {
+      canvas_draw_str_aligned(canvas, 70, 30, AlignLeft, AlignTop, "YOU WIN");
+    } else {
+      canvas_draw_str_aligned(canvas, 70, 30, AlignLeft, AlignTop, "YOU LOSE");
+    }
+  } else if (game_state->current_player == game_state->human_color) {
+    canvas_draw_str_aligned(canvas, 70, 12, AlignLeft, AlignTop, "Your turn");
+  } else {
+    canvas_draw_str_aligned(canvas, 70, 12, AlignLeft, AlignTop,
+                            "Computer turn");
+  }
+
+  if (app_state->screen == AppScreenMenu) {
+    draw_menu(canvas, app_state);
+  }
+
+  release_mutex((ValueMutex *)ctx, app_state);
+}
+
+static void draw_menu(Canvas* const canvas, const AppState* app_state) {
+  gray_canvas(canvas);
+  canvas_set_color(canvas, ColorWhite);
+  canvas_draw_rbox(canvas, 28, 16, 72, 32, 4);
+  canvas_set_color(canvas, ColorBlack);
+  canvas_draw_rframe(canvas, 28, 16, 72, 32, 4);
+
+  for (int i = 0; i < MENU_ITEMS_COUNT; i++) {
+    if (i == app_state->selected_menu_item) {
+      canvas_set_color(canvas, ColorBlack);
+      canvas_draw_box(canvas, 34, 20 + 12 * i, 60, 12);
+    }
+
+    canvas_set_color(canvas, i == app_state->selected_menu_item ? ColorWhite
+                                                                : ColorBlack);
+    canvas_draw_str_aligned(canvas, 64, 26 + 12 * i, AlignCenter, AlignCenter,
+                            popup_menu_strings[i]);
+  }
+}
+
+static void gray_canvas(Canvas* const canvas) {
+    canvas_set_color(canvas, ColorWhite);
+    for(int x = 0; x < 128; x += 2) {
+        for(int y = 0; y < 64; y++) {
+            canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y);
+        }
+    }
+}
+
+bool load_game(GameState* game_state) {
+    Storage* storage = furi_record_open(RECORD_STORAGE);
+
+    File* file = storage_file_alloc(storage);
+    uint16_t bytes_readed = 0;
+    if(storage_file_open(file, SAVING_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING)) {
+        bytes_readed = storage_file_read(file, game_state, sizeof(GameState));
+    }
+    storage_file_close(file);
+    storage_file_free(file);
+
+    furi_record_close(RECORD_STORAGE);
+
+    return bytes_readed == sizeof(GameState);
+}
+
+void save_game(const GameState* game_state) {
+    Storage* storage = furi_record_open(RECORD_STORAGE);
+
+    if(storage_common_stat(storage, SAVING_DIRECTORY, NULL) == FSE_NOT_EXIST) {
+        if(!storage_simply_mkdir(storage, SAVING_DIRECTORY)) {
+            return;
+        }
+    }
+
+    File* file = storage_file_alloc(storage);
+    if(storage_file_open(file, SAVING_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
+        storage_file_write(file, game_state, sizeof(GameState));
+    }
+    storage_file_close(file);
+    storage_file_free(file);
+
+    furi_record_close(RECORD_STORAGE);
+}
+
+bool handle_key_game(GameState *game_state, InputKey key) {
+  switch (key) {
+  case InputKeyBack:
+    save_game(game_state);
+    return false;
+    break;
+  case InputKeyOk:
+    if (game_state->is_game_over) {
+      init_game(game_state);
+      save_game(game_state);
+    } else {
+      human_move(game_state);
+    }
+    break;
+  case InputKeyUp:
+    if (game_state->cursor_y > 0) {
+      game_state->cursor_y--;
+    } else {
+      game_state->cursor_y = BOARD_SIZE - 1;
+    }
+    break;
+  case InputKeyDown:
+    if (game_state->cursor_y < BOARD_SIZE - 1) {
+      game_state->cursor_y++;
+    } else {
+      game_state->cursor_y = 0;
+    }
+    break;
+  case InputKeyLeft:
+    if (game_state->cursor_x > 0) {
+      game_state->cursor_x--;
+    } else {
+      game_state->cursor_x = BOARD_SIZE - 1;
+    }
+    break;
+  case InputKeyRight:
+    if (game_state->cursor_x < BOARD_SIZE - 1) {
+      game_state->cursor_x++;
+    } else {
+      game_state->cursor_x = 0;
+    }
+    break;
+  default:
+    break;
+  }
+  return true;
+}
+
+bool handle_key_menu(AppState *app_state, InputKey key) {
+  switch (key) {
+  case InputKeyUp:
+    if (app_state->selected_menu_item > 0) {
+      app_state->selected_menu_item--;
+    }
+    break;
+  case InputKeyDown:
+    if (app_state->selected_menu_item <= MENU_ITEMS_COUNT) {
+      app_state->selected_menu_item++;
+    }
+    break;
+  case InputKeyOk:
+    if (app_state->selected_menu_item == 1) {
+      // new game
+      init_game(&app_state->game);
+      save_game(&app_state->game);
+    }
+    app_state->screen = AppScreenGame;
+    break;
+  default:
+    break;
+  }
+  return true;
+}
+
+// returns `true` if the event loop should keep going
+bool handle_key(AppState* app_state, InputKey key) {
+  GameState* game_state = &app_state->game;
+
+  switch (app_state->screen) {
+    case AppScreenGame:
+      return handle_key_game(game_state, key);
+      break;
+    case AppScreenMenu:
+      return handle_key_menu(app_state, key);
+      break;
+  }
+  return true;
+}
+
+int32_t game_reversi_app() {
+  AppState app_state;
+  app_state.screen = AppScreenGame;
+  if (!load_game(&app_state.game)) {
+    init_game(&app_state.game);
+  }
+
+  ValueMutex state_mutex;
+  if (!init_mutex(&state_mutex, &app_state, sizeof(AppState))) {
+    return 255;
+  }
+  InputEvent input;
+  FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
+
+  ViewPort* view_port = view_port_alloc();
+  view_port_draw_callback_set(view_port, draw_callback, &state_mutex);
+  view_port_input_callback_set(view_port, input_callback, event_queue);
+
+  Gui* gui = furi_record_open(RECORD_GUI);
+  gui_add_view_port(gui, view_port, GuiLayerFullscreen);
+  bool is_finished = false;
+
+  while(!is_finished) {
+    // check if it's computer's turn
+    if (!app_state.game.is_game_over && (app_state.game.current_player != app_state.game.human_color)) {
+      computer_move(&app_state.game);
+    }
+    FuriStatus event_status = furi_message_queue_get(event_queue, &input, FuriWaitForever);
+    if(event_status == FuriStatusOk) {
+      // handle only press event, ignore repeat/release events
+
+      if (input.type == InputTypeLong && input.key == InputKeyOk && app_state.screen == AppScreenGame) {
+        AppState *app_state = (AppState *)acquire_mutex_block(&state_mutex);
+        app_state->selected_menu_item = 0;
+        app_state->screen = AppScreenMenu;
+        view_port_update(view_port);
+        release_mutex(&state_mutex, app_state);
+        continue;
+      }
+      if (input.type != InputTypePress) continue;
+
+      AppState* app_state = (AppState*)acquire_mutex_block(&state_mutex);
+      is_finished = !handle_key(app_state, input.key);
+      view_port_update(view_port);
+      release_mutex(&state_mutex, app_state);
+    }
+  }
+
+  gui_remove_view_port(gui, view_port);
+  furi_record_close(RECORD_GUI);
+
+  view_port_free(view_port);
+
+  furi_message_queue_free(event_queue);
+
+  delete_mutex(&state_mutex);
+
+  return 0;
+}

BIN
game_reversi.png


BIN
images/game.png


BIN
images/menu.png


+ 0 - 210
main.c

@@ -1,210 +0,0 @@
-#include <stdio.h>
-#include <stdbool.h>
-
-#define WHITE 1
-#define BLACK -1
-#define BOARD_SIZE 8
-
-typedef struct {
-  int board[BOARD_SIZE][BOARD_SIZE];
-  int current_player;
-  int human_color;
-} game_state;
-
-bool isLegalMove(int board[BOARD_SIZE][BOARD_SIZE], int row, int col, int player);
-
-void init_game(game_state* state) {
-  // Fill the board with 0
-    for (int i = 0; i < BOARD_SIZE; i++) {
-        for (int j = 0; j < BOARD_SIZE; j++) {
-            state->board[i][j] = 0;
-        }
-    }
-
-    // Place the initial pieces
-    int mid = BOARD_SIZE / 2;
-    state->board[mid - 1][mid - 1] = WHITE;
-    state->board[mid][mid] = WHITE;
-    state->board[mid - 1][mid] = BLACK;
-    state->board[mid][mid - 1] = BLACK;
-}
-
-bool isGameOver(int board[BOARD_SIZE][BOARD_SIZE]) {
-    /* Check if the game is over by checking if there are no more moves left for either player */
-    for (int i = 0; i < BOARD_SIZE; i++) {
-        for (int j = 0; j < BOARD_SIZE; j++) {
-            if (isLegalMove(board, i, j, BLACK) ||
-                isLegalMove(board, i, j, WHITE)) {
-                return false;
-            }
-        }
-    }
-    return true;
-}
-
-bool isLegalMove(int board[BOARD_SIZE][BOARD_SIZE], int row, int col, int player) {
-    /* Check if the move is legal by checking if it results in any opponent pieces being captured */
-    if (board[row][col] != 0) return false;
-    int opponent = -player;
-    for (int i = -1; i <= 1; i++) {
-        for (int j = -1; j <= 1; j++) {
-            if (i == 0 && j == 0) continue;
-            int r = row + i, c = col + j;
-            if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE && board[r][c] == opponent) {
-                int k = 2;
-                while (true) {
-                    r += i;
-                    c += j;
-                    if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) break;
-                    if (board[r][c] == player) return true;
-                    if (board[r][c] == 0) break;
-                    k++;
-                }
-            }
-        }
-    }
-    return false;
-}
-
-
-int heuristic(int board[BOARD_SIZE][BOARD_SIZE]) {
-    /* Calculate the heuristic value of the current board. This function can
-       be replaced with a more complex evaluation function that takes into
-       account factors such as mobility, piece square tables, etc. */
-    int white = 0, black = 0;
-    for (int i = 0; i < BOARD_SIZE; i++) {
-        for (int j = 0; j < BOARD_SIZE; j++) {
-            if (board[i][j] == 1) white++;
-            if (board[i][j] == -1) black++;
-        }
-    }
-    return white - black;
-}
-
-void makeMove(game_state* state, int x, int y, int player) {
-    /* Make a move on the board and capture any opponent pieces */
-    state->board[x][y] = player;
-    int opponent = -player;
-    for (int i = -1; i <= 1; i++) {
-        for (int j = -1; j <= 1; j++) {
-            if (i == 0 && j == 0) continue;
-            int r = x + i, c = y + j;
-            if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE && state->board[r][c] == opponent) {
-                int k = 2;
-                while (true) {
-                    r += i;
-                    c += j;
-                    if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) break;
-                    if (state->board[r][c] == player) {
-                        r -= i;
-                        c -= j;
-                        while (r != x || c != y) {
-                            state->board[r][c] = player;
-                            r -= i;
-                            c -= j;
-                        }
-                        break;
-                    }
-                    if (state->board[r][c] == 0) break;
-                    k++;
-                }
-            }
-        }
-    }
-}
-
-// function to print the board to show black and white pieces for current game state
-// and display numbers for the columns and letters for the rows
-void print_board(int board[BOARD_SIZE][BOARD_SIZE]) {
-    printf("  ");
-    for (int i = 0; i < BOARD_SIZE; i++) {
-        printf("%d ", i);
-    }
-    printf("\n");
-    for (int i = 0; i < BOARD_SIZE; i++) {
-        printf("%d ", i);
-        for (int j = 0; j < BOARD_SIZE; j++) {
-            if (board[i][j] == 0) {
-                printf(". ");
-            } else if (board[i][j] == 1) {
-                printf("W ");
-            } else {
-                printf("B ");
-            }
-        }
-        printf("\n");
-    }
-}
-
-int main() {
-  game_state state;
-
-  init_game(&state);
-  state.human_color = WHITE;
-  state.current_player = WHITE;
-
-  while (true) {
-    if (isGameOver(state.board)) {
-      // caluclate number of black and white pieces on the board
-      // and determine the winner
-      // print the winner
-      int white = 0, black = 0;
-      for (int i = 0; i < BOARD_SIZE; i++) {
-        for (int j = 0; j < BOARD_SIZE; j++) {
-          if (state.board[i][j] == 1) white++;
-          if (state.board[i][j] == -1) black++;
-        }
-      }
-      if (white > black) {
-        printf("White wins!\n");
-      } else if (black > white) {
-        printf("Black wins!\n");
-      } else {
-        printf("Tie game!\n");
-      }
-      printf("%d - %d\n", white, black);
-      break;
-    }
-    print_board(state.board);
-    if (state.current_player == state.human_color) {
-      int row, col;
-      printf("Enter row and column (or -1 -1 to skip; -2 -2 to exit): ");
-      scanf("%d %d", &row, &col);
-      if (row == -2 && col == -2) {
-        break;
-      }
-      if (row == -1 && col == -1) {
-        state.current_player = -state.current_player;
-        continue;
-      }
-      if (isLegalMove(state.board, row, col, state.current_player)) {
-        makeMove(&state, row, col, state.current_player);
-        state.current_player = -state.current_player;
-      } else {
-        printf("Illegal move!\n");
-      }
-    } else {
-      int best_row = -1, best_col = -1, best_score = -1000000;
-      for (int i = 0; i < BOARD_SIZE; i++) {
-        for (int j = 0; j < BOARD_SIZE; j++) {
-          if (isLegalMove(state.board, i, j, state.current_player)) {
-            int score = heuristic(state.board);
-            if (score > best_score) {
-              best_score = score;
-              best_row = i;
-              best_col = j;
-            }
-          }
-        }
-      }
-      if (best_row != -1) {
-        makeMove(&state, best_row, best_col, state.current_player);
-      } else {
-        printf("No legal moves for computer!\n");
-      }
-      state.current_player = -state.current_player;
-    }
-  }
-
-  return 0;
-}

+ 178 - 0
reversi.c

@@ -0,0 +1,178 @@
+// Game "Reversi" for Flipper Zero
+// Copyright 2023 Dmitry Matyukhin
+
+#include "reversi.h"
+
+// Psst! Most of this file was written with Copilot
+
+// Check if the move is legal by checking if it results in any opponent pieces being captured
+bool is_legal_move(int8_t board[BOARD_SIZE][BOARD_SIZE], int row, int col,
+                   int player) {
+  if (board[row][col] != 0)
+    return false;
+  int opponent = -player;
+  for (int i = -1; i <= 1; i++) {
+    for (int j = -1; j <= 1; j++) {
+      if (i == 0 && j == 0)
+        continue;
+      int r = row + i, c = col + j;
+      if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE &&
+          board[r][c] == opponent) {
+        int k = 2;
+        while (true) {
+          r += i;
+          c += j;
+          if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE)
+            break;
+          if (board[r][c] == player)
+            return true;
+          if (board[r][c] == 0)
+            break;
+          k++;
+        }
+      }
+    }
+  }
+  return false;
+}
+
+// Check if the game is over by checking if there are no more moves left for
+// either player
+bool is_game_over(int8_t board[BOARD_SIZE][BOARD_SIZE]) {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    for (int j = 0; j < BOARD_SIZE; j++) {
+      if (is_legal_move(board, i, j, BLACK) ||
+          is_legal_move(board, i, j, WHITE)) {
+        return false;
+      }
+    }
+  }
+  return true;
+}
+
+bool has_legal_moves(int8_t board[BOARD_SIZE][BOARD_SIZE],
+                     int8_t player_color) {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    for (int j = 0; j < BOARD_SIZE; j++) {
+      if (is_legal_move(board, i, j, player_color)) {
+        return true;
+      }
+    }
+  }
+  return false;
+}
+
+// Calculate the heuristic value of the current board. This function can
+// be replaced with a more complex evaluation function that takes into
+// account factors such as mobility, piece square tables, etc.
+int heuristic(int8_t board[BOARD_SIZE][BOARD_SIZE]) {
+  int white = 0, black = 0;
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    for (int j = 0; j < BOARD_SIZE; j++) {
+      if (board[i][j] == 1)
+        white++;
+      if (board[i][j] == -1)
+        black++;
+    }
+  }
+  return white - black;
+}
+
+// Make a move on the board and capture any opponent pieces
+void make_move(GameState *state, int x, int y, int player) {
+  state->board[x][y] = player;
+  int opponent = -player;
+  for (int i = -1; i <= 1; i++) {
+    for (int j = -1; j <= 1; j++) {
+      if (i == 0 && j == 0)
+        continue;
+      int r = x + i, c = y + j;
+      if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE &&
+          state->board[r][c] == opponent) {
+        int k = 2;
+        while (true) {
+          r += i;
+          c += j;
+          if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE)
+            break;
+          if (state->board[r][c] == player) {
+            r -= i;
+            c -= j;
+            while (r != x || c != y) {
+              state->board[r][c] = player;
+              r -= i;
+              c -= j;
+            }
+            break;
+          }
+          if (state->board[r][c] == 0)
+            break;
+          k++;
+        }
+      }
+    }
+  }
+  state->is_game_over = is_game_over(state->board);
+}
+
+void init_game(GameState *state) {
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    for (int j = 0; j < BOARD_SIZE; j++) {
+      state->board[i][j] = 0;
+    }
+  }
+
+  // Place the initial pieces
+  int mid = BOARD_SIZE / 2;
+  state->board[mid - 1][mid - 1] = WHITE;
+  state->board[mid][mid] = WHITE;
+  state->board[mid - 1][mid] = BLACK;
+  state->board[mid][mid - 1] = BLACK;
+
+  state->cursor_x = mid - 1;
+  state->cursor_y = mid + 1;
+
+  // Set up turn order
+  state->human_color = WHITE;
+  state->current_player = WHITE;
+
+  state->is_game_over = false;
+}
+
+void human_move(GameState *game_state) {
+  if (game_state->current_player != game_state->human_color) {
+    return;
+  }
+
+  if (is_legal_move(game_state->board, game_state->cursor_x,
+                    game_state->cursor_y, game_state->current_player)) {
+    make_move(game_state, game_state->cursor_x, game_state->cursor_y,
+              game_state->current_player);
+    game_state->current_player = -game_state->current_player;
+  }
+}
+
+void computer_move(GameState *game_state) {
+  if (game_state->current_player == game_state->human_color) {
+    return;
+  }
+  int best_row = -1, best_col = -1, best_score = -1000000;
+  for (int i = 0; i < BOARD_SIZE; i++) {
+    for (int j = 0; j < BOARD_SIZE; j++) {
+      if (is_legal_move(game_state->board, i, j, game_state->current_player)) {
+        int score = heuristic(game_state->board);
+        if (score > best_score) {
+          best_score = score;
+          best_row = i;
+          best_col = j;
+        }
+      }
+    }
+  }
+  if (best_row != -1) {
+    make_move(game_state, best_row, best_col, game_state->current_player);
+  }
+  if (has_legal_moves(game_state->board, game_state->human_color)) {
+    game_state->current_player = -game_state->current_player;
+  }
+}

+ 21 - 0
reversi.h

@@ -0,0 +1,21 @@
+#pragma once
+
+#include <furi.h>
+#include <stdbool.h>
+
+#define BLACK 1
+#define WHITE -1
+#define BOARD_SIZE 8
+
+typedef struct {
+  int8_t board[BOARD_SIZE][BOARD_SIZE];
+  int8_t current_player;
+  int8_t human_color;
+  uint8_t cursor_x;
+  uint8_t cursor_y;
+  uint8_t is_game_over;
+} GameState;
+
+void init_game(GameState* state);
+void computer_move(GameState *game_state);
+void human_move(GameState *game_state);