Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (21)
*.o
sokoban
unittest
minunit.h
suvegardedesfonctioin.c
\ No newline at end of file
CFLAGS=-Wall -Wextra
clean_all :
rm -f sokoban unittest *.o
all: build unittest
build: sokoban
test: unittest
@./unittest
distrib:
tar -zcvf Arnaud_Albiez.tar.gz main.c grid.c grid.h Makefile
clean :
rm -f sokoban *.o
main.o : main.c grid.h
gcc -c $(CFLAGS) main.c
grid.o : grid.c grid.h
gcc -c $(CFLAGS) grid.c
sokoban : main.o grid.o
gcc -o sokoban main.o grid.o
test.o : test.c grid.h
gcc -c test.c
unittest : test.o grid.o
gcc -o unittest test.o grid.o
This diff is collapsed.
......@@ -2,6 +2,12 @@
#include <stdlib.h>
#include <stdio.h>
int coordonner_vers_indice(int x, int y, struct Grid* grid)
{
return((y * grid->column_number)+ x);
}
void init_level(const char* file_path){
// ouverture du fichier en mode lecture
FILE* file = fopen(file_path, "r");
......@@ -17,7 +23,7 @@ void init_level(const char* file_path){
fgets(line, 100, file);
sscanf(line, "%d %d %d", &number_column, &number_row, &number_goals);
int current_row = 0;
int current_goal = 0;
//int current_goal = 0;
// On lit le fichier ligne par ligne jusqu'à la fin du fichier
while(fgets(line, 100, file) != NULL){
char* buffer = line;
......@@ -31,3 +37,76 @@ void init_level(const char* file_path){
// fermeture du fichier
fclose(file);
}
enum CaseType* initialisation_grid(int x, int y)
{
size_t taille_grid = x * y;
enum CaseType* new_grid = (enum CaseType*) malloc(taille_grid * sizeof(enum CaseType*));
for(size_t i = 0; i< taille_grid; i++)
{
new_grid[i] = NONE;
}
return new_grid;
}
struct Grid* grid_new(int x, int y)
{
struct Grid * new_strut_grid = NULL;
new_strut_grid = (struct Grid *) malloc(1 * sizeof(struct Grid *));
enum CaseType* new_grid = initialisation_grid(x, y);
new_strut_grid->column_number=x;
new_strut_grid->row_number= y;
new_strut_grid->game_grid = new_grid;
return new_strut_grid;
}
enum CaseType get_grid(struct Grid* grid, int x, int y)
{
if (0 <= x && x <grid->column_number && 0 <= y && y <grid->row_number )
{
return grid->game_grid[coordonner_vers_indice(x, y, grid)];
}
else{return -1;}
}
void Affichage_grid(struct Grid* grid)
{
for(int i = 0; i<grid->column_number; i++)
{
for(int j = 0 ; j < grid->row_number; j++)
switch(get_grid(grid, i, j)){
case NONE :{
printf(" ");
break;
}
case WALL:{
printf("#");
break;
}
case PLAYER:{
printf("@");
break;
}
case GOAL:{
printf(".");
break;
}
case BOX:{
printf("$");
break;
}
}
printf("\n");
}
}
void change_cell_grid(struct Grid* grid, int x, int y, enum CaseType new_valeur)
{
grid->game_grid[coordonner_vers_indice(x, y, grid)] = new_valeur;
}
void free_grid(struct Grid* grid)
{
free( grid->game_grid);
free (grid);
}
......@@ -13,10 +13,23 @@ enum CaseType{
* concernant la grille du jeu et son contenu
*/
struct Grid{
enum CaseType** game_grid; ///< Tableau contenant les entités présents dans le jeu
enum CaseType* game_grid; ///< Tableau contenant les entités présents dans le jeu
int column_number; ///< Nombre de colonne de game_grid
int row_number; ///< Nomber de ligne de game_grid
};
void init_level(const char* file_path);
struct Grid* grid_new(int x, int y);
enum CaseType get_grid(struct Grid* grid, int x, int y);
void Affichage_grid(struct Grid* grid);
void change_cell_grid(struct Grid* grid, int x, int y, enum CaseType new_valeur);
void free_grid(struct Grid* grid)
#endif
......@@ -5,6 +5,8 @@ int main(void){
bool run = true;
while(run){
struct Grid* grid = grid_new(5, 5);
Affichage_grid(grid);
char entry = fgetc(stdin);
switch(entry){
case 'q' :{
......
#include "cester.h"
#include <stdio.h>
#include "grid.h"
CESTER_TEST(test_grid_get_cell, grid,
{
struct Grid* grid = grid_new(5, 5);
for(int x = 0; x < 5; ++x) {
for(int y = 0; y < 5; ++y) {
cester_assert_equal(NONE, get_grid(grid, x, y));
}
}
}
)
CESTER_TEST(test_grid_change_cell, grid,
{
struct Grid* grid = grid_new(5, 5);
change_cell_grid(grid, 1, 2, GOAL);
Affichage_grid(grid);
cester_assert_equal(GOAL, get_grid(grid, 1, 2));
}
)