Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #####################################################################
- # Main.c #
- #####################################################################
- #include <stdlib.h>
- #include <stdio.h>
- #include "config.h"
- #include "stage.h"
- int main ( int argc, char** argv )
- {
- // New stage
- Sokoban_Stage Stage;
- Sokoban_Config Config;
- Sokoban_loadConfiguration(&Stage, "stage1");
- Sokoban_initNewStage(Stage);
- //Stage.build(&Stage);
- /*
- Sokoban_Stage_Case* testCase;
- Sokoban_Stage_Case* testCase2;
- testCase = Stage.getCase(&Stage,5,5);
- testCase = testCase->getTopCase(testCase);
- testCase = testCase->getRightCase(testCase);
- */
- return 1;
- }
- #####################################################################
- # stage.h #
- #####################################################################
- #ifndef _Stage
- #define _Stage
- #define CONF_FOLDER "conf"
- #include "config.h"
- #include "case.h"
- /**
- * libere la memoire que le (heap) du stage
- */
- void Sokoban_Stagefree();
- typedef struct Sokoban_Stage Sokoban_Stage;
- struct Sokoban_Stage {
- /*variable de bases*/
- int isBuilt;
- int nbOfCase;
- int nbOfRow;
- int nbOfCol;
- int ** casesIndex;
- /* pointeur dobjets*/
- Sokoban_Stage_Case* cases;
- /* mes pointeurs de foncstions*/
- void *(*loadConfiguration)(Sokoban_Config config);
- void *(*build)(Sokoban_Stage*);
- void *(*free)(Sokoban_Stage*);
- Sokoban_Stage_Case* *(*getCase)(Sokoban_Stage*,int,int);
- };
- //void Sokoban_StageLoadConfiguration(Sokoban_Config config);
- void Sokoban_StageBuild(Sokoban_Stage* stage);
- Sokoban_Stage_Case* Sokoban_StageGetCase(Sokoban_Stage* stage, int x, int y);
- Sokoban_Stage Sokoban_initNewStage();
- #endif
- #####################################################################
- # stage.c #
- #####################################################################
- #include <stdio.h>
- #include "case.h"
- #include "stage.h"
- #include "config.h"
- #include <string.h>
- Sokoban_Stage Sokoban_initNewStage()
- {
- static short isStart;
- static Sokoban_Stage stage;
- /*
- Si stage est deja defini on ne le redifini pas
- ceci peu paraitre un peu inutile du fait quen ce
- moment on ne peu que faire appel a Sokoban_initStage()
- quune seule foi.
- */
- if(isStart == 1) {
- return stage;
- }
- isStart = 1;
- // Je defini cest variable manuellement, il vont eventuellement etre charger dynamique dun fichier texte
- stage.nbOfRow += 10;
- stage.nbOfCol += 10;
- // Association de pointeur vers fonctions
- stage.build = &Sokoban_StageBuild;
- stage.free = &Sokoban_Stagefree;
- stage.getCase = &Sokoban_StageGetCase;
- //stage.loadConfiguration = &Sokoban_StageLoadConfiguration;
- return stage;
- }
- void Sokoban_StageBuild(Sokoban_Stage* stage){
- stage->nbOfCase = (stage->nbOfRow * stage->nbOfCol);
- stage->cases = (Sokoban_Stage_Case*) malloc(sizeof(Sokoban_Stage_Case) * stage->nbOfCase);
- stage->casesIndex = (int ** ) malloc (stage->nbOfRow * stage->nbOfCol * sizeof (int));
- // chaque case a une coordonner x et y cest ici que je loop
- // dans chacune pour assigner tout ca
- int i,ii,iii;
- iii=0;
- for (i=1;i<=stage->nbOfRow;i++) {
- stage->casesIndex[(i-1)] = malloc(stage->nbOfCol*sizeof(int));
- for (ii=1;ii<=stage->nbOfCol;ii++) {
- stage->cases[iii].stage = stage;
- stage->cases[iii].x = i;
- stage->cases[iii].y = ii;
- stage->cases[iii].getTopCase = &Sokoban_Stage_CaseGetTopCase;
- stage->cases[iii].getBottomCase = &Sokoban_Stage_CaseGetBottomCase;
- stage->cases[iii].getLeftCase = &Sokoban_Stage_CaseGetLeftCase;
- stage->cases[iii].getRightCase = &Sokoban_Stage_CaseGetRightCase;
- stage->casesIndex[(i-1)][(ii-1)] = iii;
- iii++;
- }
- }
- stage->isBuilt = 1;
- }
- /*
- void Sokoban_StageloadConfiguration(Sokoban_Config config)
- {
- }
- */
- void Sokoban_Stagefree(Sokoban_Stage* stage){
- // Petite precaution pour etre sur que le stage est deja construit
- if(stage->isBuilt != 1){
- return 0; // Je devrais eventuellement mettre un throw erreur ou quelque chose du genre
- }
- free(stage->cases);
- }
- Sokoban_Stage_Case* Sokoban_StageGetCase(Sokoban_Stage* stage, int x, int y) {
- int index;
- index = stage->casesIndex[(x-1)][(y-1)];
- return &stage->cases[index];
- }
- #####################################################################
- # case.h #
- #####################################################################
- #ifndef _Case
- #define _Case
- // Case structure
- typedef struct Sokoban_Stage_Case Sokoban_Stage_Case;
- struct Sokoban_Stage_Case{
- struct Sokoban_Stage* stage;
- int x;
- int y;
- int z;
- Sokoban_Stage_Case* *(*getTopCase)(Sokoban_Stage_Case* acase);
- Sokoban_Stage_Case* *(*getBottomCase)(Sokoban_Stage_Case* acase);
- Sokoban_Stage_Case* *(*getLeftCase)(Sokoban_Stage_Case* acase);
- Sokoban_Stage_Case* *(*getRightCase)(Sokoban_Stage_Case* acase);
- };
- Sokoban_Stage_Case* Sokoban_Stage_CaseGetTopCase(Sokoban_Stage_Case* acase);
- Sokoban_Stage_Case* Sokoban_Stage_CaseGetBottomCase(Sokoban_Stage_Case* acase);
- Sokoban_Stage_Case* Sokoban_Stage_CaseGetLeftCase(Sokoban_Stage_Case* acase);
- Sokoban_Stage_Case* Sokoban_Stage_CaseGetRightCase(Sokoban_Stage_Case* acase);
- #endif
- #####################################################################
- # case.c #
- #####################################################################
- #include "stage.h"
- #include "case.h"
- Sokoban_Stage_Case* Sokoban_Stage_CaseGetTopCase(Sokoban_Stage_Case* acase)
- {
- return (acase->stage->getCase(acase->stage,acase->x,(acase->y-1)));
- }
- Sokoban_Stage_Case* Sokoban_Stage_CaseGetBottomCase(Sokoban_Stage_Case* acase)
- {
- return acase->stage->getCase(acase->stage,acase->x,(acase->y+1));
- }
- Sokoban_Stage_Case* Sokoban_Stage_CaseGetLeftCase(Sokoban_Stage_Case* acase)
- {
- return acase->stage->getCase(acase->stage,(acase->x-1),acase->y);
- }
- Sokoban_Stage_Case* Sokoban_Stage_CaseGetRightCase(Sokoban_Stage_Case* acase)
- {
- return acase->stage->getCase(acase->stage,(acase->x+1),acase->y);
- }
- #####################################################################
- # config.h #
- #####################################################################
- #ifndef _Configuration
- #define _Configuration
- typedef struct Sokoban_Config Sokoban_Config;
- struct Sokoban_Config{
- };
- void Sokoban_loadConfiguration(Sokoban_Stage* stage, char fileName[100]);
- #endif
- #####################################################################
- # config.c #
- #####################################################################
- #include <stdio.h>
- #include "stage.h"
- #include "case.h"
- #include "config.h"
- void Sokoban_loadConfiguration(Sokoban_Stage* stage, char fileName[100])
- {
- FILE *configFile;
- char fullPath[100], line[100],lineExtract[100], ch;
- sprintf(fullPath,"%s/%s",CONF_FOLDER,fileName);
- configFile = fopen(fullPath,"r");
- if(configFile == NULL){
- perror(fullPath);
- }
- //while(!feof(configFile)){
- while( fgets(line, sizeof(line), configFile) != NULL) {
- // On regarde si on est dans une section
- sscanf(line,"[%99[^]]]",lineExtract);
- if(strlen(lineExtract)) {
- printf("->section:%s :: %d\n",lineExtract,strlen(lineExtract));
- } else{
- sscanf(lineExtract,"%s\n",line);
- if(strlen(line) > 1) {
- printf("->valeur:%s :: %d\n",line, strlen(line));
- }
- }
- sprintf(lineExtract,"","");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment