Skip to content
Snippets Groups Projects
Commit 1f2323f0 authored by Taha Belkhiri's avatar Taha Belkhiri
Browse files

checking for cycles using binary values

parent 906425d4
Branches
No related tags found
No related merge requests found
No preview for this file type
...@@ -4,9 +4,6 @@ int MAPSIZE = 20; ...@@ -4,9 +4,6 @@ int MAPSIZE = 20;
int **map; int **map;
int ***mapList;
int mapListSize = 0;
void allocateMap() void allocateMap()
{ {
map = malloc(MAPSIZE * sizeof(int *)); map = malloc(MAPSIZE * sizeof(int *));
...@@ -16,15 +13,9 @@ void allocateMap() ...@@ -16,15 +13,9 @@ void allocateMap()
} }
} }
void allocateMapList(int SIZE)
{
mapList = malloc(SIZE * sizeof(int **));
}
void initMap() void initMap()
{ {
allocateMap(); allocateMap();
allocateMapList(100);
for (int i = 0; i < MAPSIZE; i++) for (int i = 0; i < MAPSIZE; i++)
{ {
for (int j = 0; j < MAPSIZE; j++) for (int j = 0; j < MAPSIZE; j++)
...@@ -60,8 +51,15 @@ void writeMap(char *filename) ...@@ -60,8 +51,15 @@ void writeMap(char *filename)
fclose(f); fclose(f);
} }
void addMapToList(int **map) int MapToBinary(int ** map)
{ {
mapList[mapListSize] = map; int binary = 0;
mapListSize++; for (int i = 0; i < MAPSIZE; i++)
{
for (int j = 0; j < MAPSIZE; j++)
{
binary += map[i][j] * pow(2, (i * MAPSIZE + j));
}
}
return binary;
} }
\ No newline at end of file
...@@ -5,13 +5,10 @@ ...@@ -5,13 +5,10 @@
extern int MAPSIZE; extern int MAPSIZE;
extern int ** map; extern int ** map;
extern int *** mapList;
extern int mapListSize;
void printMap(); void printMap();
void initMap(); void initMap();
void writeMap(char* filename); void writeMap(char* filename);
void addMapToList(int ** map); int MapToBinary(int ** map);
int ** saveMapState();
#endif #endif
\ No newline at end of file
...@@ -4,6 +4,9 @@ int surviveRule[NB_RULES] = {0, 0, 1, 1, 0, 0, 0, 0, 0}; ...@@ -4,6 +4,9 @@ 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 bornRule[NB_RULES] = {0, 0, 0, 1, 0, 0, 0, 0, 0};
int stable; int stable;
int BinaryList[LIST_SIZE];
int BinaryListSize;
int survivingNeighbors(int x, int y){ int survivingNeighbors(int x, int y){
int count = 0; int count = 0;
for(int i = -1; i <= 1; i++){ for(int i = -1; i <= 1; i++){
...@@ -15,10 +18,31 @@ int survivingNeighbors(int x, int y){ ...@@ -15,10 +18,31 @@ int survivingNeighbors(int x, int y){
return count; return count;
} }
void addBinaryToList(int binary){
if (BinaryListSize == LIST_SIZE) return;
BinaryList[BinaryListSize] = binary;
BinaryListSize++;
}
void checkForCycle(){
int i = 0;
while (i < BinaryListSize){
if (BinaryList[i] == BinaryList[BinaryListSize - 1]){
printf("Cycle detected!\n");
return;
}
i++;
}
}
void updateMap(){ void updateMap(){
int newMap[MAPSIZE][MAPSIZE]; int newMap[MAPSIZE][MAPSIZE];
stable = 1; stable = 1;
int binary = MapToBinary(map);
addBinaryToList(binary);
for (int i=0; i<MAPSIZE; i++){ for (int i=0; i<MAPSIZE; i++){
for (int j=0; j<MAPSIZE; j++){ for (int j=0; j<MAPSIZE; j++){
int count = survivingNeighbors(j, i); int count = survivingNeighbors(j, i);
...@@ -44,5 +68,7 @@ void updateMap(){ ...@@ -44,5 +68,7 @@ void updateMap(){
map[i][j] = newMap[i][j]; map[i][j] = newMap[i][j];
} }
} }
checkForCycle();
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define NB_RULES 9 #define NB_RULES 9
#define LIST_SIZE 100
extern int stable; extern int stable;
void updateMap(); void updateMap();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment