From d9dc92b65a2789f1e55b8deb06b42755aeb457d0 Mon Sep 17 00:00:00 2001 From: Mykola Syniavskyi Date: Sat, 14 Jun 2025 03:00:26 +0000 Subject: [PATCH] =?UTF-8?q?P=C5=99idat=20du8/main.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du8/main.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 du8/main.c diff --git a/du8/main.c b/du8/main.c new file mode 100644 index 0000000..ed3c394 --- /dev/null +++ b/du8/main.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include "game.h" +#include "world.h" + +typedef struct { + GameState game; +} AppState; + +void* init_game_wrapper() { + AppState* state = malloc(sizeof(AppState)); + init_game(&state->game); + return state; +} + +int world_event(struct event* ev, void* data) { + AppState* app = (AppState*)data; + + if (ev->type == EVENT_KEY) { + int key = ev->key; + + if (app->game.game_over) { + if (key == '\n') { + init_game(&app->game); + } else if (key == 'q' || key == 'Q') { + return 1; + } + } else { + if (key == 'q' || key == 'Q') { + return 1; + } + + handle_input(&app->game, key); + + int winner = check_winner(&app->game); + if (winner != 0) { + app->game.winner = winner; + app->game.game_over = 1; + } else { + bool draw = true; + for (int y = 0; y < 3; y++) + for (int x = 0; x < 3; x++) + if (app->game.board[y][x] == EMPTY) + draw = false; + if (draw) { + app->game.winner = 0; + app->game.game_over = 1; + } + } + } + } + + clear_screen(); + draw_game(&app->game); + return 0; +} + +void destroy_game_wrapper(void* data) { + free(data); +} + +int main() { + return start_world(init_game_wrapper, world_event, destroy_game_wrapper); +}