diff --git a/travail_individuel/Belkhiri/GameOfLife/run b/travail_individuel/Belkhiri/GameOfLife/run index 87257857f689c489c5941519e96c548f7a7f12f9..b4b5d1dda336d41fd1db836aed18335e57e069d6 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..a220bc3ff0c352cfc08e2d029ce437527ba8ad60 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/map.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.c @@ -2,47 +2,56 @@ int MAPSIZE = 20; -int ** map; +int **map; -int *** mapList; +int ***mapList; -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 +59,62 @@ void writeMap(char* filename){ fclose(f); } -int ** saveMapState(){ - int ** mapCopy = malloc(MAPSIZE * sizeof(int*)); - for(int i = 0; i < MAPSIZE; i++){ +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++){ + 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){ +void addMapToList(int **map) +{ int i = 0; - while(mapList[i] != NULL){ + while (mapList[i] != NULL) + { i++; } mapList[i] = map; } -void checkForCycle(){ +void checkForCycle() +{ + // count number of maps saved int i = 0; - while(mapList[i] != NULL){ - if(mapList[i] == map){ - printf("Cycle detected at iteration: %d\n", i); - return; - } + while (mapList[i] != NULL) + { i++; } + + int j = 0; + for (j = 0; j < i; j++) + { + int cycle = 1; + // compare values of maps + int k = 0; + for (k = 0; k < MAPSIZE; k++) + { + for (int l = 0; l < MAPSIZE; l++) + { + if (mapList[j][k][l] != map[k][l]) + { + cycle = 0; + } + } + } + if (cycle == 1) + { + printf("Cycle found!\n"); + break; + } + } } \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/render.c b/travail_individuel/Belkhiri/GameOfLife/src/render.c index 8ca7d3d14e9534a736200c0471ccf650b7ac312f..b1feb2ee9ca17f9504b1a4525aee293f3eef9602 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/render.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/render.c @@ -135,6 +135,19 @@ void drawGame(){ SDL_RenderPresent(renderer); } +void drawStable(){ + char str[20] = "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); +} + void MainLoop(){ CreateWindow(); 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..6a1e6ddfdfb31104c1049212196640d9c504b219 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/utility.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/utility.c @@ -39,7 +39,7 @@ void updateMap(){ } if (stable){ - printf("Stable state reached.\n"); + drawStable(); } for (int i=0; i<MAPSIZE; i++){ @@ -49,6 +49,5 @@ void updateMap(){ } addMapToList(map); - checkForCycle(); }