This commit is contained in:
lecointre
2025-11-29 19:08:22 +01:00
parent a79e28ee9b
commit c3f8f064ec
4 changed files with 111 additions and 93 deletions
+86 -21
View File
@@ -1,50 +1,115 @@
#include "game.h" #include "game.h"
#include "utils.h"
#include <stdlib.h>
#include <stdio.h>
static void index_to_coords(int index, int cols, int *r, int *c)
void game_init(struct GameState* state, int rows, int cols)
{ {
*r = index / cols;
/* TODO */ *c = index % cols;
} }
static int game_can_move(const game_state_t *state, int index_to_move)
void game_move(struct GameState* state, int tile)
{ {
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) 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;
/* TODO */ }
return 0; 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;
/* TODO */ 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; 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;
} }
void game_shuffle(struct GameState* state) int game_is_solved(const game_state_t *state)
{ {
int num_tiles = state->rows * state->cols;
int i;
/* TODO */ 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;
} }
-21
View File
@@ -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;
}
+15 -17
View File
@@ -1,34 +1,32 @@
#ifndef GAME_H #ifndef GAME_H
#define GAME_H #define GAME_H
#include
#include
struct GameState { #define MAX_SIZE 8
typedef struct {
int rows; int rows;
int cols; int cols;
int empty_index;
int moves;
int is_over;
int *permutation; int *permutation;
}; int empty_index;
unsigned long moves;
int is_solved;
tile_t **all_tiles;
} game_state_t;
void game_init(struct GameState* state, int rows, int cols); void game_init(game_state_t *state, int rows, int cols, tile_t **tiles);
void game_move(struct GameState* state, int tile); void game_free(game_state_t *state);
int game_can_move(const struct GameState* state, int tile); int game_move_tile(game_state_t *state, int index_to_move);
int game_is_solved(const struct GameState* state); int game_is_solved(const game_state_t *state);
void game_shuffle(struct GameState* state); void game_shuffle_safe(game_state_t *state, int steps);
#endif /* GAME_H */
#endif
-24
View File
@@ -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