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