This commit is contained in:
cyjullien1
2024-12-12 13:09:58 +01:00
9 changed files with 68 additions and 11 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
*.o
sokoban
*.out

View File

@@ -1,7 +1,7 @@
# Variable
CC = gcc
CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2)
OBJ = main.o function.o display.o
OBJ = main.o function.o display.o read.o
TARGET = sokoban
all: $(TARGET)
@@ -9,7 +9,7 @@ all: $(TARGET)
$(TARGET): $(OBJ)
$(CC) $(CFLAGS) $(OBJ) -o $(TARGET)
main.o : main.c function.h
main.o : main.c function.h display.h read.h
$(CC) $(CFLAGS) -c main.c
function.o : function.c function.h
@@ -18,6 +18,9 @@ function.o : function.c function.h
display.o: display.c display.h
$(CC) $(CFLAGS) -c display.c
read.o: read.c read.h function.h
$(CC) $(CFLAGS) -c read.c
clean :
rm -f $(OBJ) $(TARGET)

BIN
a.out

Binary file not shown.

View File

@@ -13,7 +13,3 @@ void screenDisplay( unsigned short int **tab,int size)
}
}

View File

@@ -15,7 +15,7 @@ unsigned short int **creatArea2D(const unsigned int N)
}
bool fail = false;
int i;
unsigned int i;
for (i = 0; i < N && !fail; ++i)
{
tab2d[i] = calloc(N, sizeof(unsigned short int));
@@ -27,7 +27,7 @@ unsigned short int **creatArea2D(const unsigned int N)
if (fail)
{
int j;
unsigned int j;
for (j = 0; j < i; ++j)
{
free(tab2d[j]);

6
main.c
View File

@@ -1,11 +1,13 @@
#include <stdio.h>
#include "function.h"
#include "display.h"
#include "read.h"
int main()
{
unsigned short int **tab2d = creatArea2D(5);
screenDisplay(tab2d, 5);
unsigned short int **tab2d = creatArea2D(32);
fileToTab2D("test.txt", tab2d, 32);
screenDisplay(tab2d, 32);
printf("main\n");
return 0;
}

40
read.c Normal file
View File

@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include "function.h"
void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned N)
{
FILE *file = fopen(name_file, "r");
unsigned int x = 0, y = 0;
while(!feof(file))
{
char current =fgetc(file);
switch (current) {
case ' ':
tab[x][y] = EMPTY;
break;
case '#':
tab[x][y] = WALL;
break;
case '$':
tab[x][y] = BOX;
break;
case '.':
tab[x][y] = TARGET;
break;
case '@':
tab[x][y] = PLAYER;
break;
case '\n':
y = 0;
++x;
break;
}
++y;
if (x >= N || y >= N)
{
perror("Level out of range !");
exit(-1);
}
}
}

5
read.h Normal file
View File

@@ -0,0 +1,5 @@
#ifndef READ_H
#define READ_H
void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned N);
#endif // !READ_H

11
test.txt Normal file
View File

@@ -0,0 +1,11 @@
#####
# #
#$ #
### $##
# $ $ #
### # ## # ######
# # ## ##### ..#
# $ $ ..#
##### ### #@## ..#
# #########
#######