diff --git a/travail_individuel/Belkhiri/GameOfLife/run b/travail_individuel/Belkhiri/GameOfLife/run index b4c3dc621abb0fa478779fdf9a4734e610cf775a..87257857f689c489c5941519e96c548f7a7f12f9 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 025044668b687dd7ac5589e31b6c4e8d0d862d3f..3d66af14ef65df732082f8bcd36497a49d2b5791 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/map.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.c @@ -4,6 +4,8 @@ int MAPSIZE = 20; int ** map; +int *** mapList; + void allocateMap(){ map = malloc(MAPSIZE * sizeof(int*)); for(int i = 0; i < MAPSIZE; i++){ @@ -11,9 +13,14 @@ void allocateMap(){ } } +void allocateMapList(int SIZE){ + mapList = malloc(SIZE * sizeof(int**)); +} + void initMap() { allocateMap(); + allocateMapList(100); for(int i = 0; i < MAPSIZE; i++){ for(int j = 0; j < MAPSIZE; j++){ map[i][j] = 0; @@ -41,4 +48,36 @@ void writeMap(char* filename){ fprintf(f, "\n"); } 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++; + } } \ 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 6ba8681921611b88e47f1962c8626c306636b5de..ec0e592949fe5acb5bc4c6ea87ea34861e1dd430 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/map.h +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.h @@ -5,9 +5,13 @@ extern int MAPSIZE; extern int ** map; +extern int *** mapList; void printMap(); void initMap(); void writeMap(char* filename); +void checkForCycle(); +void addMapToList(int ** map); +int ** saveMapState(); #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 0fd5618855756ec723c6f15b952c6effd01ca6d5..2eab0213c15352fa542d02bd74032f110d47131d 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/utility.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/utility.c @@ -17,6 +17,7 @@ int survivingNeighbors(int x, int y){ void updateMap(){ int newMap[MAPSIZE][MAPSIZE]; + int stable = 1; for (int i=0; i<MAPSIZE; i++){ for (int j=0; j<MAPSIZE; j++){ int count = survivingNeighbors(j, i); @@ -30,10 +31,24 @@ void updateMap(){ } } } + + for (int i=0; i<MAPSIZE; i++){ + for (int j=0; j<MAPSIZE; j++){ + if (newMap[j][i] != map[j][i]) stable = 0; + } + } + + 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(); }