Guest User

Untitled

a guest
Apr 21st, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.25 KB | None | 0 0
  1. #####################################################################
  2. # Main.c                                                            #
  3. #####################################################################
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "config.h"
  7. #include "stage.h"
  8.  
  9. int main ( int argc, char** argv )
  10. {
  11.     // New stage
  12.     Sokoban_Stage Stage;
  13.     Sokoban_Config Config;
  14.  
  15.     Sokoban_loadConfiguration(&Stage, "stage1");
  16.     Sokoban_initNewStage(Stage);
  17.  
  18.  
  19.  
  20.     //Stage.build(&Stage);
  21.  
  22.     /*
  23.     Sokoban_Stage_Case* testCase;
  24.     Sokoban_Stage_Case* testCase2;
  25.     testCase = Stage.getCase(&Stage,5,5);
  26.     testCase = testCase->getTopCase(testCase);
  27.     testCase = testCase->getRightCase(testCase);
  28.     */
  29.  
  30.     return 1;
  31. }
  32.  
  33. #####################################################################
  34. # stage.h                                                           #
  35. #####################################################################
  36. #ifndef _Stage
  37. #define _Stage
  38. #define CONF_FOLDER "conf"
  39. #include "config.h"
  40. #include "case.h"
  41.  
  42. /**
  43. * libere la memoire que le (heap) du stage
  44. */
  45. void Sokoban_Stagefree();
  46.  
  47.  
  48. typedef struct Sokoban_Stage Sokoban_Stage;
  49.  
  50. struct Sokoban_Stage  {
  51.         /*variable de bases*/
  52.         int isBuilt;
  53.         int nbOfCase;
  54.         int nbOfRow;
  55.         int nbOfCol;
  56.         int ** casesIndex;
  57.  
  58.         /* pointeur dobjets*/
  59.         Sokoban_Stage_Case* cases;
  60.  
  61.         /* mes pointeurs de foncstions*/
  62.         void *(*loadConfiguration)(Sokoban_Config config);
  63.         void *(*build)(Sokoban_Stage*);
  64.         void *(*free)(Sokoban_Stage*);
  65.         Sokoban_Stage_Case* *(*getCase)(Sokoban_Stage*,int,int);
  66. };
  67. //void Sokoban_StageLoadConfiguration(Sokoban_Config config);
  68.  
  69. void Sokoban_StageBuild(Sokoban_Stage* stage);
  70.  
  71. Sokoban_Stage_Case* Sokoban_StageGetCase(Sokoban_Stage* stage, int x, int y);
  72.  
  73. Sokoban_Stage Sokoban_initNewStage();
  74.  
  75.  
  76. #endif
  77.  
  78. #####################################################################
  79. # stage.c                                                           #
  80. #####################################################################
  81. #include <stdio.h>
  82. #include "case.h"
  83. #include "stage.h"
  84. #include "config.h"
  85. #include <string.h>
  86. Sokoban_Stage Sokoban_initNewStage()
  87. {
  88.     static short isStart;
  89.     static Sokoban_Stage stage;
  90.  
  91.     /*
  92.      Si stage est deja defini on ne le redifini pas
  93.      ceci peu paraitre un peu inutile du fait quen ce
  94.      moment on ne peu que faire appel a Sokoban_initStage()
  95.      quune seule foi.
  96.     */
  97.     if(isStart == 1) {
  98.         return stage;
  99.     }
  100.  
  101.     isStart = 1;
  102.  
  103.     // Je defini cest variable manuellement, il vont eventuellement etre charger dynamique dun fichier texte
  104.     stage.nbOfRow  += 10;
  105.     stage.nbOfCol  += 10;
  106.  
  107.     // Association de pointeur vers fonctions
  108.     stage.build             = &Sokoban_StageBuild;
  109.     stage.free              = &Sokoban_Stagefree;
  110.     stage.getCase           = &Sokoban_StageGetCase;
  111.     //stage.loadConfiguration = &Sokoban_StageLoadConfiguration;
  112.  
  113.     return stage;
  114. }
  115.  
  116. void Sokoban_StageBuild(Sokoban_Stage* stage){
  117.     stage->nbOfCase   = (stage->nbOfRow * stage->nbOfCol);
  118.     stage->cases      = (Sokoban_Stage_Case*) malloc(sizeof(Sokoban_Stage_Case) * stage->nbOfCase);
  119.     stage->casesIndex = (int ** ) malloc (stage->nbOfRow * stage->nbOfCol * sizeof (int));
  120.  
  121.     // chaque case a une coordonner x et y cest ici que je loop
  122.     // dans chacune pour assigner tout ca
  123.     int i,ii,iii;
  124.     iii=0;
  125.     for (i=1;i<=stage->nbOfRow;i++) {
  126.         stage->casesIndex[(i-1)] = malloc(stage->nbOfCol*sizeof(int));
  127.         for (ii=1;ii<=stage->nbOfCol;ii++) {
  128.             stage->cases[iii].stage   = stage;
  129.  
  130.             stage->cases[iii].x       = i;
  131.             stage->cases[iii].y       = ii;
  132.  
  133.             stage->cases[iii].getTopCase    = &Sokoban_Stage_CaseGetTopCase;
  134.             stage->cases[iii].getBottomCase = &Sokoban_Stage_CaseGetBottomCase;
  135.             stage->cases[iii].getLeftCase   = &Sokoban_Stage_CaseGetLeftCase;
  136.             stage->cases[iii].getRightCase  = &Sokoban_Stage_CaseGetRightCase;
  137.  
  138.             stage->casesIndex[(i-1)][(ii-1)] = iii;
  139.             iii++;
  140.         }
  141.     }
  142.  
  143.     stage->isBuilt  = 1;
  144. }
  145. /*
  146. void Sokoban_StageloadConfiguration(Sokoban_Config config)
  147. {
  148.  
  149. }
  150. */
  151.  
  152. void Sokoban_Stagefree(Sokoban_Stage* stage){
  153.     // Petite precaution pour etre sur que le stage est deja construit
  154.     if(stage->isBuilt != 1){
  155.         return 0; // Je devrais eventuellement mettre un throw erreur ou quelque chose du genre
  156.     }
  157.  
  158.     free(stage->cases);
  159. }
  160.  
  161. Sokoban_Stage_Case* Sokoban_StageGetCase(Sokoban_Stage* stage, int x, int y) {
  162.     int index;
  163.     index = stage->casesIndex[(x-1)][(y-1)];
  164.     return &stage->cases[index];
  165. }
  166.  
  167. #####################################################################
  168. # case.h                                                            #
  169. #####################################################################
  170. #ifndef _Case
  171.     #define _Case
  172.  
  173.     // Case structure
  174.     typedef struct Sokoban_Stage_Case Sokoban_Stage_Case;
  175.     struct  Sokoban_Stage_Case{
  176.         struct Sokoban_Stage* stage;
  177.         int x;
  178.         int y;
  179.         int z;
  180.         Sokoban_Stage_Case* *(*getTopCase)(Sokoban_Stage_Case* acase);
  181.         Sokoban_Stage_Case* *(*getBottomCase)(Sokoban_Stage_Case* acase);
  182.         Sokoban_Stage_Case* *(*getLeftCase)(Sokoban_Stage_Case* acase);
  183.         Sokoban_Stage_Case* *(*getRightCase)(Sokoban_Stage_Case* acase);
  184.     };
  185.  
  186.     Sokoban_Stage_Case* Sokoban_Stage_CaseGetTopCase(Sokoban_Stage_Case* acase);
  187.     Sokoban_Stage_Case* Sokoban_Stage_CaseGetBottomCase(Sokoban_Stage_Case* acase);
  188.     Sokoban_Stage_Case* Sokoban_Stage_CaseGetLeftCase(Sokoban_Stage_Case* acase);
  189.     Sokoban_Stage_Case* Sokoban_Stage_CaseGetRightCase(Sokoban_Stage_Case* acase);
  190.  
  191. #endif
  192.  
  193. #####################################################################
  194. # case.c                                                            #
  195. #####################################################################
  196. #include "stage.h"
  197. #include "case.h"
  198.  
  199. Sokoban_Stage_Case* Sokoban_Stage_CaseGetTopCase(Sokoban_Stage_Case* acase)
  200. {
  201.     return (acase->stage->getCase(acase->stage,acase->x,(acase->y-1)));
  202. }
  203.  
  204. Sokoban_Stage_Case* Sokoban_Stage_CaseGetBottomCase(Sokoban_Stage_Case* acase)
  205. {
  206.     return acase->stage->getCase(acase->stage,acase->x,(acase->y+1));
  207. }
  208.  
  209. Sokoban_Stage_Case* Sokoban_Stage_CaseGetLeftCase(Sokoban_Stage_Case* acase)
  210. {
  211.     return acase->stage->getCase(acase->stage,(acase->x-1),acase->y);
  212. }
  213.  
  214. Sokoban_Stage_Case* Sokoban_Stage_CaseGetRightCase(Sokoban_Stage_Case* acase)
  215. {
  216.     return acase->stage->getCase(acase->stage,(acase->x+1),acase->y);
  217. }
  218.  
  219. #####################################################################
  220. # config.h                                                          #
  221. #####################################################################
  222. #ifndef _Configuration
  223. #define _Configuration
  224. typedef struct Sokoban_Config Sokoban_Config;
  225.  
  226. struct Sokoban_Config{
  227.  
  228. };
  229.  
  230. void Sokoban_loadConfiguration(Sokoban_Stage* stage, char fileName[100]);
  231.  
  232. #endif
  233.  
  234. #####################################################################
  235. # config.c                                                          #
  236. #####################################################################
  237. #include <stdio.h>
  238. #include "stage.h"
  239. #include "case.h"
  240. #include "config.h"
  241.  
  242. void Sokoban_loadConfiguration(Sokoban_Stage* stage, char fileName[100])
  243. {
  244.     FILE *configFile;
  245.     char fullPath[100], line[100],lineExtract[100], ch;
  246.  
  247.     sprintf(fullPath,"%s/%s",CONF_FOLDER,fileName);
  248.  
  249.     configFile = fopen(fullPath,"r");
  250.  
  251.     if(configFile == NULL){
  252.        perror(fullPath);
  253.     }
  254.  
  255.  
  256.  
  257.     //while(!feof(configFile)){
  258.    while( fgets(line, sizeof(line), configFile) != NULL) {
  259.  
  260.         // On regarde si on est dans une section
  261.         sscanf(line,"[%99[^]]]",lineExtract);
  262.         if(strlen(lineExtract)) {
  263.             printf("->section:%s :: %d\n",lineExtract,strlen(lineExtract));
  264.         } else{
  265.             sscanf(lineExtract,"%s\n",line);
  266.             if(strlen(line) > 1) {
  267.                 printf("->valeur:%s :: %d\n",line, strlen(line));
  268.             }
  269.         }
  270.  
  271.         sprintf(lineExtract,"","");
  272.     }
  273.  
  274.  
  275.  
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment