}
}
-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) {
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'){
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;
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;
}