# HG changeset patch # User viric@llimona # Date 1146865400 -7200 # Node ID d9259a605dec26d29cac90a1ae41dbdb0cab9e37 # Parent 29cc57a9678e4a0e34006df1d7bdc28d8bf96ed0 A cleaner version, split between different files. diff -r 29cc57a9678e -r d9259a605dec .hgignore --- a/.hgignore Fri May 05 23:20:33 2006 +0200 +++ b/.hgignore Fri May 05 23:43:20 2006 +0200 @@ -1,9 +1,2 @@ -sokosol2 -sokosol3 -sokosol4 -sokosol5 -sokosold -sokosold2 -sokosol-profund -sokosol-branques +sokosol .*\.o diff -r 29cc57a9678e -r d9259a605dec Makefile --- a/Makefile Fri May 05 23:20:33 2006 +0200 +++ b/Makefile Fri May 05 23:43:20 2006 +0200 @@ -8,3 +8,11 @@ LDFLAGS=-pg all: sokosol + +sokosol: map.o sokosol.o + +map.c: general.h +sokosol.c: general.h + +clean: + rm -f sokosol *.o diff -r 29cc57a9678e -r d9259a605dec general.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/general.h Fri May 05 23:43:20 2006 +0200 @@ -0,0 +1,76 @@ +#define BOX '$' +#define WALL '#' +#define MAN '@' +#define PLATFORM '.' +#define BOXINPLATFORM '*' +#define MANINPLATFORM 'E' +#define BLANK ' ' +#define CORNER '-' +#define MANCANMOVE '+' + +//#define DEBUG + +enum +{ + ALARM_SECONDS=1, + MAX_X=50, + MAX_Y=50, + MAX_MOVES=50, + MAX_STEPS=50, + MAX_BOXES=15 +}; + + +enum logic +{ + TRUE=1, + FALSE=0, +}; + +struct Position +{ + int x; + int y; +}; + +struct Map +{ + char Cells[MAX_Y][MAX_X]; + char cells_boxes[MAX_Y][MAX_X]; + char man_moves[MAX_Y][MAX_X]; + int SizeX, SizeY; + struct Position Man; + int NumPlatforms; + int NumBoxesInPlatform; + struct Position Box[MAX_BOXES]; + int NumBoxes; +}; + + + +enum e_direction +{ + DIR_LEFT, + DIR_RIGHT, + DIR_DOWN, + DIR_UP +}; + +static const struct Position move_vectors[4] = { + {0, 1}, + {0, -1}, + {1, 0}, + {-1, 0}}; + +struct BoxMove +{ + int box; + struct Position dir; +}; + + +/* Prototypes for map managing */ + +void CopyMap (struct Map *Mdest, const struct Map *Morig); +void ReadMap(struct Map *M, char *FileName); +void ShowMap (const struct Map *M); diff -r 29cc57a9678e -r d9259a605dec map.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/map.c Fri May 05 23:43:20 2006 +0200 @@ -0,0 +1,122 @@ +#include +#include +#include +#include "general.h" + +void CopyMap (struct Map *Mdest, const struct Map *Morig) +{ + memcpy((void *) Mdest, (void *) Morig, sizeof (struct Map)); +} + +void ReadMap(struct Map *M, char *FileName) +{ + FILE *Fitxer; + int i,j; + + if(!(Fitxer = fopen(FileName, "r"))) + { + printf("Error opening %s!", FileName); + exit(1); + } + + M->SizeX=0; + M->SizeY=0; + while (!feof(Fitxer)) + { + fgets(M->Cells[M->SizeY], MAX_X, Fitxer); + M->SizeY++; + } + M->SizeY--; + M->SizeX = strlen(M->Cells[0]) - 1; + + M->NumPlatforms = 0; + M->NumBoxesInPlatform = 0; + M->NumBoxes = 0; + for (j = 0; jSizeY; j++) + for (i=0; iSizeX; i++) + { + if (M->Cells[j][i] == MAN) + { + M->Man.x = i; M->Man.y = j; + M->Cells[M->Man.y][M->Man.x] = BLANK; + } + + if (M->Cells[j][i] == PLATFORM) + M->NumPlatforms++; + else if (M->Cells[j][i] == BOXINPLATFORM) + { + M->Cells[j][i] = PLATFORM; + + M->NumPlatforms++; + M->NumBoxesInPlatform++; + + M->Box[M->NumBoxes].x = i; + M->Box[M->NumBoxes].y = j; + M->NumBoxes++; + } else if (M->Cells[j][i] == BOX) + { + M->Cells[j][i] = BLANK; + + M->Box[M->NumBoxes].x = i; + M->Box[M->NumBoxes].y = j; + M->NumBoxes++; + } else if (M->Cells[j][i] == CORNER) + { + M->Cells[j][i] = CORNER; + } else if (M->Cells[j][i] != WALL) + { + if ( (M->Cells[j][i-1] == WALL && + M->Cells[j-1][i] == WALL) || + (M->Cells[j][i-1] == WALL && + M->Cells[j+1][i] == WALL) || + (M->Cells[j][i+1] == WALL && + M->Cells[j-1][i] == WALL) || + (M->Cells[j][i+1] == WALL && + M->Cells[j+1][i] == WALL)) + M->Cells[j][i] = CORNER; + } + + } + +} + + +void ShowMap (const struct Map *M) +{ + struct Map Temp; + int i,j; + + CopyMap(&Temp, M); + + Temp.Cells[Temp.Man.y][Temp.Man.x] = MAN; + + for (i = 0; i < Temp.NumBoxes; i++) + { + if (Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] == PLATFORM) + Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] =BOXINPLATFORM; + else + Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] = BOX; + } + + for (j = 0; j #include #include - -#define BOX '$' -#define WALL '#' -#define MAN '@' -#define PLATFORM '.' -#define BOXINPLATFORM '*' -#define MANINPLATFORM 'E' -#define BLANK ' ' -#define CORNER '-' -#define MANCANMOVE '+' +#include "general.h" #define NBOX(n) ((n)<<2) -//#define DEBUG - -enum -{ - ALARM_SECONDS=1 -}; - - -enum logic -{ - TRUE=1, - FALSE=0, -}; - -#define MAX_X 50 -#define MAX_Y 50 -#define MAX_MOVES 50 -#define MAX_STEPS 50 -#define MAX_BOXES 15 - -#define MOVE_OK 1 -#define MOVE_BOX 2 -#define MOVE_ILLEGAL 0 - - /* SOKOBAN Solution Finder * * Input File Format: XSB @@ -53,51 +19,12 @@ * $ BOX * * BOX over PLATFORM * + PLATFORM - * - (optional) Where a BOX should not be put + * - CORNER (optional) Where a BOX should not be put * Everything that cannot be reached by the man or boxes (WALLS) must be * marked as is: WALLS. * All lines must be of the same length. */ -struct Position -{ - int x; - int y; -}; - -struct Map -{ - char Cells[MAX_Y][MAX_X]; - char cells_boxes[MAX_Y][MAX_X]; - char man_moves[MAX_Y][MAX_X]; - int SizeX, SizeY; - struct Position Man; - int NumPlatforms; - int NumBoxesInPlatform; - struct Position Box[MAX_BOXES]; - int NumBoxes; -}; - - - -enum e_direction -{ - DIR_LEFT, - DIR_RIGHT, - DIR_DOWN, - DIR_UP -}; -const struct Position move_vectors[4] = { - {0, 1}, - {0, -1}, - {1, 0}, - {-1, 0}}; - -struct BoxMove -{ - int box; - struct Position dir; -}; float percent_to_show = 0; int depth_to_show = 0; @@ -110,127 +37,6 @@ -void CopyMap (struct Map *Mdest, const struct Map *Morig) -{ - memcpy((void *) Mdest, (void *) Morig, sizeof (struct Map)); -} - - -void ReadMap(struct Map *M, char *FileName) -{ - FILE *Fitxer; - int i,j; - - if(!(Fitxer = fopen(FileName, "r"))) - { - printf("Error opening %s!", FileName); - exit(1); - } - - M->SizeX=0; - M->SizeY=0; - while (!feof(Fitxer)) - { - fgets(M->Cells[M->SizeY], MAX_X, Fitxer); - M->SizeY++; - } - M->SizeY--; - M->SizeX = strlen(M->Cells[0]) - 1; - - M->NumPlatforms = 0; - M->NumBoxesInPlatform = 0; - M->NumBoxes = 0; - for (j = 0; jSizeY; j++) - for (i=0; iSizeX; i++) - { - if (M->Cells[j][i] == MAN) - { - M->Man.x = i; M->Man.y = j; - M->Cells[M->Man.y][M->Man.x] = BLANK; - } - - if (M->Cells[j][i] == PLATFORM) - M->NumPlatforms++; - else if (M->Cells[j][i] == BOXINPLATFORM) - { - M->Cells[j][i] = PLATFORM; - - M->NumPlatforms++; - M->NumBoxesInPlatform++; - - M->Box[M->NumBoxes].x = i; - M->Box[M->NumBoxes].y = j; - M->NumBoxes++; - } else if (M->Cells[j][i] == BOX) - { - M->Cells[j][i] = BLANK; - - M->Box[M->NumBoxes].x = i; - M->Box[M->NumBoxes].y = j; - M->NumBoxes++; - } else if (M->Cells[j][i] == CORNER) - { - M->Cells[j][i] = CORNER; - } else if (M->Cells[j][i] != WALL) - { - if ( (M->Cells[j][i-1] == WALL && - M->Cells[j-1][i] == WALL) || - (M->Cells[j][i-1] == WALL && - M->Cells[j+1][i] == WALL) || - (M->Cells[j][i+1] == WALL && - M->Cells[j-1][i] == WALL) || - (M->Cells[j][i+1] == WALL && - M->Cells[j+1][i] == WALL)) - M->Cells[j][i] = CORNER; - } - - } - -} - - -void ShowMap (const struct Map *M) -{ - struct Map Temp; - int i,j; - - CopyMap(&Temp, M); - - Temp.Cells[Temp.Man.y][Temp.Man.x] = MAN; - - for (i = 0; i < Temp.NumBoxes; i++) - { - if (Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] == PLATFORM) - Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] =BOXINPLATFORM; - else - Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] = BOX; - } - - for (j = 0; j