diff --git a/travail_individuel/Belkhiri/GameOfLife/run b/travail_individuel/Belkhiri/GameOfLife/run index 87257857f689c489c5941519e96c548f7a7f12f9..af69540990706f8de4044047d08508fde45a9b9b 100755 Binary files a/travail_individuel/Belkhiri/GameOfLife/run and b/travail_individuel/Belkhiri/GameOfLife/run differ diff --git a/travail_individuel/Belkhiri/GameOfLife/src/map.c b/travail_individuel/Belkhiri/GameOfLife/src/map.c index 3d66af14ef65df732082f8bcd36497a49d2b5791..e49cf5f029b491fa508497c27ad5d084ca795168 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/map.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.c @@ -2,47 +2,57 @@ int MAPSIZE = 20; -int ** map; +int **map; -int *** mapList; +int ***mapList; +int mapListSize = 0; -void allocateMap(){ - map = malloc(MAPSIZE * sizeof(int*)); - for(int i = 0; i < MAPSIZE; i++){ +void allocateMap() +{ + map = malloc(MAPSIZE * sizeof(int *)); + for (int i = 0; i < MAPSIZE; i++) + { map[i] = malloc(MAPSIZE * sizeof(int)); } } -void allocateMapList(int SIZE){ - mapList = malloc(SIZE * sizeof(int**)); +void allocateMapList(int SIZE) +{ + mapList = malloc(SIZE * sizeof(int **)); } - -void initMap() { +void initMap() +{ allocateMap(); allocateMapList(100); - for(int i = 0; i < MAPSIZE; i++){ - for(int j = 0; j < MAPSIZE; j++){ + for (int i = 0; i < MAPSIZE; i++) + { + for (int j = 0; j < MAPSIZE; j++) + { map[i][j] = 0; } } } - -void printMap(){ - for(int i = 0; i < MAPSIZE; i++){ - for(int j = 0; j < MAPSIZE; j++){ +void printMap() +{ + for (int i = 0; i < MAPSIZE; i++) + { + for (int j = 0; j < MAPSIZE; j++) + { printf("%d ", map[i][j]); } printf("\n"); } } - -void writeMap(char* filename){ - FILE* f = fopen(filename, "w"); - for(int i = 0; i < MAPSIZE; i++){ - for(int j = 0; j < MAPSIZE; j++){ +void writeMap(char *filename) +{ + FILE *f = fopen(filename, "w"); + for (int i = 0; i < MAPSIZE; i++) + { + for (int j = 0; j < MAPSIZE; j++) + { fprintf(f, "%d ", map[i][j]); } fprintf(f, "\n"); @@ -50,34 +60,8 @@ void writeMap(char* filename){ fclose(f); } -int ** saveMapState(){ - int ** mapCopy = malloc(MAPSIZE * sizeof(int*)); - for(int i = 0; i < MAPSIZE; i++){ - mapCopy[i] = malloc(MAPSIZE * sizeof(int)); - } - for(int i = 0; i < MAPSIZE; i++){ - for(int j = 0; j < MAPSIZE; j++){ - mapCopy[i][j] = map[i][j]; - } - } - return mapCopy; -} - -void addMapToList(int ** map){ - int i = 0; - while(mapList[i] != NULL){ - i++; - } - mapList[i] = map; -} - -void checkForCycle(){ - int i = 0; - while(mapList[i] != NULL){ - if(mapList[i] == map){ - printf("Cycle detected at iteration: %d\n", i); - return; - } - i++; - } +void addMapToList(int **map) +{ + mapList[mapListSize] = map; + mapListSize++; } \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/map.h b/travail_individuel/Belkhiri/GameOfLife/src/map.h index ec0e592949fe5acb5bc4c6ea87ea34861e1dd430..cb95414889e3110ee70e5807314a2dcd9436e8e4 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/map.h +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.h @@ -6,11 +6,11 @@ extern int MAPSIZE; extern int ** map; extern int *** mapList; +extern int mapListSize; void printMap(); void initMap(); void writeMap(char* filename); -void checkForCycle(); void addMapToList(int ** map); int ** saveMapState(); diff --git a/travail_individuel/Belkhiri/GameOfLife/src/render.c b/travail_individuel/Belkhiri/GameOfLife/src/render.c index 8ca7d3d14e9534a736200c0471ccf650b7ac312f..5d0776c0963ac4f2d9c1790dbfbb2704288df73b 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/render.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/render.c @@ -126,12 +126,30 @@ void drawColumns(){ SDL_RenderCopy(renderer, columnTexture, NULL, &rightRect); } +void drawStable(){ + char str[21] = "Stable state reached"; + SDL_Color textColor = {0, 0, 0}; + SDL_Surface * surface = TTF_RenderText_Solid(RobotoFont, str, textColor); + SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface); + + int titleWidth, titleHeight; + SDL_QueryTexture(texture, NULL, NULL, &titleWidth, &titleHeight); + + SDL_Rect titleRect = {ScreenDimension.w/2 - titleWidth/2, ScreenDimension.h/2 - titleHeight, titleWidth, titleHeight}; + SDL_RenderCopy(renderer, texture, NULL, &titleRect); + + SDL_RenderPresent(renderer); +} + void drawGame(){ SDL_RenderClear(renderer); drawBackground(); drawBackground2(); drawMap(); drawColumns(); + if (stable){ + drawStable(); + } SDL_RenderPresent(renderer); } diff --git a/travail_individuel/Belkhiri/GameOfLife/src/render.h b/travail_individuel/Belkhiri/GameOfLife/src/render.h index 05dfe33ec768ebbfaff1137c704d71afdd9934a7..956f3ad3224d85daf002ac78a3a4149bebac3e9e 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/render.h +++ b/travail_individuel/Belkhiri/GameOfLife/src/render.h @@ -15,4 +15,6 @@ extern SDL_DisplayMode ScreenDimension; void MainLoop(); +void drawStable(); + #endif \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/utility.c b/travail_individuel/Belkhiri/GameOfLife/src/utility.c index 2eab0213c15352fa542d02bd74032f110d47131d..ebb1630d6d0bdf29cf7514338cf137d2d2e71a22 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/utility.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/utility.c @@ -2,6 +2,7 @@ int surviveRule[NB_RULES] = {0, 0, 1, 1, 0, 0, 0, 0, 0}; int bornRule[NB_RULES] = {0, 0, 0, 1, 0, 0, 0, 0, 0}; +int stable; int survivingNeighbors(int x, int y){ int count = 0; @@ -17,7 +18,7 @@ int survivingNeighbors(int x, int y){ void updateMap(){ int newMap[MAPSIZE][MAPSIZE]; - int stable = 1; + stable = 1; for (int i=0; i<MAPSIZE; i++){ for (int j=0; j<MAPSIZE; j++){ int count = survivingNeighbors(j, i); @@ -38,17 +39,10 @@ void updateMap(){ } } - if (stable){ - printf("Stable state reached.\n"); - } - for (int i=0; i<MAPSIZE; i++){ for (int j=0; j<MAPSIZE; j++){ map[i][j] = newMap[i][j]; } } - - addMapToList(map); - checkForCycle(); } diff --git a/travail_individuel/Belkhiri/GameOfLife/src/utility.h b/travail_individuel/Belkhiri/GameOfLife/src/utility.h index 5d34b4b4e0f3f0122eec2cb91099b02b0de8dab0..f781d11ca1e8175e6b4f8740c227e5b70a78fb92 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/utility.h +++ b/travail_individuel/Belkhiri/GameOfLife/src/utility.h @@ -7,6 +7,7 @@ #define NB_RULES 9 +extern int stable; void updateMap();