screen shrinking done
authorAlexander Fedotov <a.fedotov@student.ru.nl>
Sat, 26 Mar 2016 12:08:25 +0000 (13:08 +0100)
committerAlexander Fedotov <a.fedotov@student.ru.nl>
Sat, 26 Mar 2016 12:08:25 +0000 (13:08 +0100)
modelchecker/main.c
modelchecker/sokoban.c
modelchecker/sokoban.h

index 240e3c7..d8d7e23 100644 (file)
@@ -44,6 +44,7 @@ void solve(FILE *inputstream)
        sokoban_screen *screen = parse_screen(inputstream);
        if (screen == NULL) printf("Something went wrong...\n");
        sokoban_print(screen);
+       sokoban_free(screen);
        //parse_screen(inputstream);
        time_end_read = clock();
 
index d5e967f..465c628 100644 (file)
@@ -50,7 +50,24 @@ void sokoban_print(sokoban_screen *screen)
        }
 }
 
-void sokoban_clear(sokoban_screen *screen)
+sokoban_screen *sokoban_shrink(int x, int y, sokoban_screen *screen, sokoban_screen *newscreen)
+{
+       sokoban_screen *c, *nc = NULL;
+       c = get_coord(x, y, screen);
+       if (c) {
+               nc = get_coord(c->coord.x, c->coord.y, newscreen);
+               if (c->tile != WALL && nc == NULL) {
+                       newscreen = add_coord(x, y, c->tile, newscreen);
+                       newscreen = sokoban_shrink(x-1, y, screen, newscreen);
+                       newscreen = sokoban_shrink(x+1, y, screen, newscreen);
+                       newscreen = sokoban_shrink(x, y-1, screen, newscreen);
+                       newscreen = sokoban_shrink(x, y+1, screen, newscreen);
+               }
+       }
+       return newscreen;
+}
+
+void sokoban_free(sokoban_screen *screen)
 {
        sokoban_screen *r, *tmp = NULL;
        HASH_ITER(hh, screen, r, tmp) {
@@ -61,9 +78,11 @@ void sokoban_clear(sokoban_screen *screen)
 
 sokoban_screen *parse_screen(FILE *stream)
 {
-       int buffer, x, y;
+       int buffer, x, y, agent_x, agent_y;
        x = 0;
        y = 0;
+       agent_x = 0;
+       agent_y = 0;
        sokoban_screen *screen = NULL;
        while((buffer = fgetc(stream)) != EOF){
                if (buffer == '\n'){
@@ -73,7 +92,11 @@ sokoban_screen *parse_screen(FILE *stream)
                else {
                        switch(buffer) {
                        case ' ': screen = add_coord(x, y, FREE, screen); break;
-                       case '@': screen = add_coord(x, y, AGENT, screen); break;
+                       case '@':
+                               screen = add_coord(x, y, AGENT, screen);
+                               agent_x = x;
+                               agent_y = y;
+                               break;
                        case '.': screen = add_coord(x, y, TARGET, screen); break;
                        case '#': screen = add_coord(x, y, WALL, screen); break;
                        case '$': screen = add_coord(x, y, BOX, screen); break;
@@ -84,8 +107,9 @@ sokoban_screen *parse_screen(FILE *stream)
                        x++;
                }
        }
-       sokoban_screen *test = NULL;
-       test = get_coord(0,0,screen);
-       if(test->tile == TARGAGENT) printf("Yuppi\n");
-       return screen;
+       sokoban_screen *newscreen = NULL;
+       newscreen = sokoban_shrink(agent_x, agent_y, screen, newscreen);
+       //sokoban_print(screen); //unshrinked screen
+       sokoban_free(screen);
+       return newscreen;
 }
index 414daa0..30466e3 100644 (file)
@@ -21,6 +21,6 @@ sokoban_screen *get_coord(int x, int y, sokoban_screen *screen);
 
 void sokoban_print(sokoban_screen *screen);
 
-void sokoban_clear(sokoban_screen *screen);
+void sokoban_free(sokoban_screen *screen);
 
 #endif