From 7da24e557ee9928cacd5cb10c19805e13015b8b0 Mon Sep 17 00:00:00 2001 From: lecointre Date: Sun, 30 Nov 2025 15:13:09 +0100 Subject: [PATCH] Menu --- Makefile | 15 ++-- src/game.c | 115 --------------------------- src/game.h | 32 -------- src/game_view.c | 14 ---- src/game_view.h | 54 ------------- src/images.c | 180 ----------------------------------------- src/images.h | 24 ------ src/input.c | 153 ----------------------------------- src/input.h | 18 ----- src/main.c | 23 ------ src/menu_view.c | 30 ------- src/menu_view.h | 28 ------- src/utils.c | 207 ------------------------------------------------ src/utils.h | 13 --- 14 files changed, 9 insertions(+), 897 deletions(-) delete mode 100644 src/game.c delete mode 100644 src/game.h delete mode 100644 src/game_view.c delete mode 100644 src/game_view.h delete mode 100644 src/images.c delete mode 100644 src/images.h delete mode 100644 src/input.c delete mode 100644 src/input.h delete mode 100644 src/utils.c diff --git a/Makefile b/Makefile index c2c256a..4136f75 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ -CC = gcc -CFLAGS = -Wall -Wextra -std=c89 -SRC_DIR = src +CC = gcc +CFLAGS = -Wall -Wextra -std=c89 +LIBS = -lgraph OBJ_DIR = obj +SRC_DIR = src SRC = $(wildcard $(SRC_DIR)/*.c) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) @@ -12,14 +13,16 @@ EXEC = taquin all: $(EXEC) $(EXEC): $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c - mkdir -p $(OBJ_DIR) + @mkdir -p $(OBJ_DIR) $(CC) $(CFLAGS) -c $< -o $@ run: $(EXEC) ./$(EXEC) clean: - rm -rf $(OBJ_DIR) *.o $(EXEC) + rm -rf $(OBJ_DIR) $(EXEC) + +.PHONY : all clean run diff --git a/src/game.c b/src/game.c deleted file mode 100644 index c28b873..0000000 --- a/src/game.c +++ /dev/null @@ -1,115 +0,0 @@ -#include "game.h" -#include "utils.h" -#include -#include - -static void index_to_coords(int index, int cols, int *r, int *c) -{ - *r = index / cols; - *c = index % cols; -} - -static int game_can_move(const game_state_t *state, int index_to_move) -{ - int r_tile, c_tile; - int r_empty, c_empty; - int num_tiles = state->rows * state->cols; - - if (index_to_move < 0 || index_to_move >= num_tiles) { - return 0; - } - - if (state->permutation[index_to_move] == -1) { - return 0; - } - - index_to_coords(index_to_move, state->cols, &r_tile, &c_tile); - index_to_coords(state->empty_index, state->cols, &r_empty, &c_empty); - - if (abs_val(r_tile - r_empty) + abs_val(c_tile - c_empty) == 1) { - return 1; - } - - return 0; -} - - - -void game_init(game_state_t *state, int rows, int cols, tile_t **tiles) -{ - int num_tiles = rows * cols; - int i; - - state->rows = rows; - state->cols = cols; - state->moves = 0UL; - state->is_solved = 1; / - state->all_tiles = tiles; - - state->permutation = (int *)malloc(num_tiles * sizeof(int)); - if (state->permutation == NULL) { - return; - } - - for (i = 0; i < num_tiles - 1; i++) { - state->permutation[i] = i; - } - - state->permutation[num_tiles - 1] = -1; - state->empty_index = num_tiles - 1; -} - -void game_free(game_state_t *state) -{ - if (state->permutation != NULL) { - free(state->permutation); - state->permutation = NULL; - } -} - - -int game_move_tile(game_state_t *state, int index_to_move) -{ - int temp_tile_id; - - if (!game_can_move(state, index_to_move)) { - return 0; - } - - temp_tile_id = state->permutation[index_to_move]; - state->permutation[index_to_move] = state->permutation[state->empty_index]; - state->permutation[state->empty_index] = temp_tile_id; - - state->empty_index = index_to_move; - - state->moves++; - state->is_solved = game_is_solved(state); - - return 1; -} - - -int game_is_solved(const game_state_t *state) -{ - int num_tiles = state->rows * state->cols; - int i; - - for (i = 0; i < num_tiles - 1; i++) { - if (state->permutation[i] != i) { - return 0; - } - } - - if (state->permutation[num_tiles - 1] != -1) { - return 0; - } - - return 1; / -} - - -void game_shuffle_safe(game_state_t *state, int steps) -{ - (void)state; - (void)steps; -} diff --git a/src/game.h b/src/game.h deleted file mode 100644 index 79ca25b..0000000 --- a/src/game.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef GAME_H -#define GAME_H - -#include -#include - -#define MAX_SIZE 8 - -typedef struct { - int rows; - int cols; - - int *permutation; - - int empty_index; - unsigned long moves; - int is_solved; - - tile_t **all_tiles; -} game_state_t; - -void game_init(game_state_t *state, int rows, int cols, tile_t **tiles); - -void game_free(game_state_t *state); - -int game_move_tile(game_state_t *state, int index_to_move); - -int game_is_solved(const game_state_t *state); - -void game_shuffle_safe(game_state_t *state, int steps); - -#endif /* GAME_H */ diff --git a/src/game_view.c b/src/game_view.c deleted file mode 100644 index dee2638..0000000 --- a/src/game_view.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "game_view.h" - - -void render_game(const struct GameState* state) - -{ - - /* TODO : dessiner les tuiles */ - -} - - - - diff --git a/src/game_view.h b/src/game_view.h deleted file mode 100644 index 2c52bfa..0000000 --- a/src/game_view.h +++ /dev/null @@ -1,54 +0,0 @@ -Baptiste LECOINTRE - -12:59 (il y a 0 minute) - - -À moi - -#ifndef GAME_H - -#define GAME_H - - -struct GameState { - - int rows; - - int cols; - - int empty_index; - - int moves; - - int is_over; - - int *permutation; - -}; - - -void game_init(struct GameState* state, int rows, int cols); - -void game_move(struct GameState* state, int tile); - -int game_can_move(const struct GameState* state, int tile); - -int game_is_solved(const struct GameState* state); - -void game_shuffle(struct GameState* state); - - -#endif - -#ifndef GAME_VIEW_H - -#define GAME_VIEW_H - - -#include "game.h" - - -void render_game(const struct GameState* state); - - -#endif diff --git a/src/images.c b/src/images.c deleted file mode 100644 index 56a5b70..0000000 --- a/src/images.c +++ /dev/null @@ -1,180 +0,0 @@ -Baptiste LECOINTRE - -13:01 (il y a 3 minutes) - - -À moi -Ce message semble être en anglais - -#include "game.h" - -#include "game_view.h" - -#include "menu_view.h" - -#include "input.h" - - -int main(void) - -{ - - /* TODO : initialiser la bibliothèque graphique */ - - /* TODO : boucle principale */ - - - return 0; - -} - - - - -#include "game.h" - - -void game_init(struct GameState* state, int rows, int cols) - -{ - - /* TODO */ - -} - - -void game_move(struct GameState* state, int tile) - -{ - - /* TODO */ - -} - - -int game_can_move(const struct GameState* state, int tile) - -{ - - /* TODO */ - - return 0; - -} - - -int game_is_solved(const struct GameState* state) - -{ - - /* TODO */ - - return 0; - -} - - -void game_shuffle(struct GameState* state) - -{ - - /* TODO */ - -} - - - - - -#include "game_view.h" - - -void render_game(const struct GameState* state) - -{ - - /* TODO : dessiner les tuiles */ - -} - - - - -#include "menu_view.h" - - -void render_menu(const struct MenuState* menu) - -{ - - /* TODO */ - -} - - -void menu_handle_mouse(struct MenuState* menu, int x, int y) - -{ - - /* TODO */ - -} - - -void menu_handle_key(struct MenuState* menu, int key) - -{ - - /* TODO */ - -} - - - - -#include "input.h" - - -void input_handle_mouse_game(struct GameState* state, int x, int y) - -{ - - /* TODO */ - -} - - -void input_handle_key_game(struct GameState* state, int key) - -{ - - /* TODO */ - -} - - - - -#include "images.h" - - -Image load_image(const char* path) - -{ - - Image img; - - /* TODO */ - - return img; - -} - - -void slice_image_into_tiles(const Image img, int rows, int cols) - -{ - - /* TODO */ - -} diff --git a/src/images.h b/src/images.h deleted file mode 100644 index 22a3e05..0000000 --- a/src/images.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef IMAGES_H - -#define IMAGES_H - - -typedef struct { - - /* dépend de la bibliothèque de l’IUT */ - - int width; - - int height; - - void* data; - -} Image; - - -Image load_image(const char* path); - -void slice_image_into_tiles(const Image img, int rows, int cols); - - -#endif diff --git a/src/input.c b/src/input.c deleted file mode 100644 index 565476a..0000000 --- a/src/input.c +++ /dev/null @@ -1,153 +0,0 @@ -Baptiste LECOINTRE - -13:01 (il y a 3 minutes) - - -À moi -Ce message semble être en anglais - -#include "game.h" - -#include "game_view.h" - -#include "menu_view.h" - -#include "input.h" - - -int main(void) - -{ - - /* TODO : initialiser la bibliothèque graphique */ - - /* TODO : boucle principale */ - - - return 0; - -} - - - - -#include "game.h" - - -void game_init(struct GameState* state, int rows, int cols) - -{ - - /* TODO */ - -} - - -void game_move(struct GameState* state, int tile) - -{ - - /* TODO */ - -} - - -int game_can_move(const struct GameState* state, int tile) - -{ - - /* TODO */ - - return 0; - -} - - -int game_is_solved(const struct GameState* state) - -{ - - /* TODO */ - - return 0; - -} - - -void game_shuffle(struct GameState* state) - -{ - - /* TODO */ - -} - - - - - -#include "game_view.h" - - -void render_game(const struct GameState* state) - -{ - - /* TODO : dessiner les tuiles */ - -} - - - - -#include "menu_view.h" - - -void render_menu(const struct MenuState* menu) - -{ - - /* TODO */ - -} - - -void menu_handle_mouse(struct MenuState* menu, int x, int y) - -{ - - /* TODO */ - -} - - -void menu_handle_key(struct MenuState* menu, int key) - -{ - - /* TODO */ - -} - - - - -#include "input.h" - - -void input_handle_mouse_game(struct GameState* state, int x, int y) - -{ - - /* TODO */ - -} - - -void input_handle_key_game(struct GameState* state, int key) - -{ - - /* TODO */ - -} diff --git a/src/input.h b/src/input.h deleted file mode 100644 index cafd159..0000000 --- a/src/input.h +++ /dev/null @@ -1,18 +0,0 @@ - - -#ifndef INPUT_H - -#define INPUT_H - - -#include "game.h" - -#include "menu_view.h" - - -void input_handle_mouse_game(struct GameState* state, int x, int y); - -void input_handle_key_game(struct GameState* state, int key); - - -#endif diff --git a/src/main.c b/src/main.c index 8f59677..e69de29 100644 --- a/src/main.c +++ b/src/main.c @@ -1,23 +0,0 @@ -#include "game.h" - -#include "game_view.h" - -#include "menu_view.h" - -#include "input.h" - - -int main(void) - -{ - - /* TODO : initialiser la bibliothèque graphique */ - - /* TODO : boucle principale */ - - - return 0; - -} - - diff --git a/src/menu_view.c b/src/menu_view.c index 8add763..e69de29 100644 --- a/src/menu_view.c +++ b/src/menu_view.c @@ -1,30 +0,0 @@ - - -#include "menu_view.h" - - -void render_menu(const struct MenuState* menu) - -{ - - /* TODO */ - -} - - -void menu_handle_mouse(struct MenuState* menu, int x, int y) - -{ - - /* TODO */ - -} - - -void menu_handle_key(struct MenuState* menu, int key) - -{ - - /* TODO */ - -} diff --git a/src/menu_view.h b/src/menu_view.h index 52a0bbe..e69de29 100644 --- a/src/menu_view.h +++ b/src/menu_view.h @@ -1,28 +0,0 @@ - - -#ifndef MENU_VIEW_H - -#define MENU_VIEW_H - - -struct MenuState { - - int selected_image; - - int rows; - - int cols; - - int ready; - -}; - - -void render_menu(const struct MenuState* menu); - -void menu_handle_mouse(struct MenuState* menu, int x, int y); - -void menu_handle_key(struct MenuState* menu, int key); - - -#endif diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index ec22ad0..0000000 --- a/src/utils.c +++ /dev/null @@ -1,207 +0,0 @@ -Baptiste LECOINTRE - -13:01 (il y a 4 minutes) - - -À moi -Ce message semble être en anglais - -#include "game.h" - -#include "game_view.h" - -#include "menu_view.h" - -#include "input.h" - - -int main(void) - -{ - - /* TODO : initialiser la bibliothèque graphique */ - - /* TODO : boucle principale */ - - - return 0; - -} - - - - -#include "game.h" - - -void game_init(struct GameState* state, int rows, int cols) - -{ - - /* TODO */ - -} - - -void game_move(struct GameState* state, int tile) - -{ - - /* TODO */ - -} - - -int game_can_move(const struct GameState* state, int tile) - -{ - - /* TODO */ - - return 0; - -} - - -int game_is_solved(const struct GameState* state) - -{ - - /* TODO */ - - return 0; - -} - - -void game_shuffle(struct GameState* state) - -{ - - /* TODO */ - -} - - - - - -#include "game_view.h" - - -void render_game(const struct GameState* state) - -{ - - /* TODO : dessiner les tuiles */ - -} - - - - -#include "menu_view.h" - - -void render_menu(const struct MenuState* menu) - -{ - - /* TODO */ - -} - - -void menu_handle_mouse(struct MenuState* menu, int x, int y) - -{ - - /* TODO */ - -} - - -void menu_handle_key(struct MenuState* menu, int key) - -{ - - /* TODO */ - -} - - - - -#include "input.h" - - -void input_handle_mouse_game(struct GameState* state, int x, int y) - -{ - - /* TODO */ - -} - - -void input_handle_key_game(struct GameState* state, int key) - -{ - - /* TODO */ - -} - - - - -#include "images.h" - - -Image load_image(const char* path) - -{ - - Image img; - - /* TODO */ - - return img; - -} - - -void slice_image_into_tiles(const Image img, int rows, int cols) - -{ - - /* TODO */ - -} - - - - -#include "utils.h" - - -int random_int(int max) - -{ - - return rand() % max; - -} - - -void swap(int* a, int* b) - -{ - - int tmp = *a; - - *a = *b; - - *b = tmp; - -} diff --git a/src/utils.h b/src/utils.h index 72058a0..e69de29 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,13 +0,0 @@ - - -#ifndef UTILS_H - -#define UTILS_H - - -int random_int(int max); - -void swap(int* a, int* b); - - -#endif