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

detection cycles/etat stable

parent 5cd6d071
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -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
......@@ -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();
......
......@@ -15,4 +15,6 @@ extern SDL_DisplayMode ScreenDimension;
void MainLoop();
void drawStable();
#endif
\ No newline at end of file
......@@ -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();
}
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