diff --git a/src/game.c b/src/game.c index b6e2790..c28b873 100644 --- a/src/game.c +++ b/src/game.c @@ -1,50 +1,115 @@ #include "game.h" +#include "utils.h" +#include +#include - -void game_init(struct GameState* state, int rows, int cols) - +static void index_to_coords(int index, int cols, int *r, int *c) { - - /* TODO */ - + *r = index / cols; + *c = index % cols; } - -void game_move(struct GameState* state, int tile) - +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; - /* TODO */ + if (index_to_move < 0 || index_to_move >= num_tiles) { + return 0; + } -} + if (state->permutation[index_to_move] == -1) { + return 0; + } - -int game_can_move(const struct GameState* state, int tile) - -{ - - /* TODO */ + 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; - } -int game_is_solved(const struct GameState* state) +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; - /* TODO */ - - return 0; - + 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_shuffle(struct GameState* state) - +void game_free(game_state_t *state) { + if (state->permutation != NULL) { + free(state->permutation); + state->permutation = NULL; + } +} - /* TODO */ +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.c~ b/src/game.c~ deleted file mode 100644 index 7f8b3b7..0000000 --- a/src/game.c~ +++ /dev/null @@ -1,21 +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/game.h b/src/game.h index 66d0d5a..79ca25b 100644 --- a/src/game.h +++ b/src/game.h @@ -1,34 +1,32 @@ #ifndef GAME_H - #define GAME_H +#include +#include -struct GameState { +#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; - int empty_index; +void game_init(game_state_t *state, int rows, int cols, tile_t **tiles); - int moves; +void game_free(game_state_t *state); - int is_over; +int game_move_tile(game_state_t *state, int index_to_move); - int *permutation; +int game_is_solved(const game_state_t *state); -}; +void game_shuffle_safe(game_state_t *state, int steps); - -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 +#endif /* GAME_H */ diff --git a/src/images.h~ b/src/images.h~ deleted file mode 100644 index f120ef9..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