Src
This commit is contained in:
+89
-24
@@ -1,50 +1,115 @@
|
||||
#include "game.h"
|
||||
#include "utils.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
index_to_coords(index_to_move, state->cols, &r_tile, &c_tile);
|
||||
index_to_coords(state->empty_index, state->cols, &r_empty, &c_empty);
|
||||
|
||||
int game_can_move(const struct GameState* state, int tile)
|
||||
|
||||
{
|
||||
|
||||
/* TODO */
|
||||
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;
|
||||
|
||||
/* TODO */
|
||||
state->rows = rows;
|
||||
state->cols = cols;
|
||||
state->moves = 0UL;
|
||||
state->is_solved = 1; /
|
||||
state->all_tiles = tiles;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
-21
@@ -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
@@ -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 empty_index;
|
||||
|
||||
int moves;
|
||||
|
||||
int is_over;
|
||||
|
||||
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
|
||||
#endif /* GAME_H */
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user