A cleaner version, split between different files.
authorviric@llimona
Fri, 05 May 2006 23:43:20 +0200
changeset 4 d9259a605dec
parent 3 29cc57a9678e
child 5 9d1e320acbb0
A cleaner version, split between different files.
.hgignore
Makefile
general.h
map.c
sokosol.c
--- 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
--- 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
--- /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);
--- /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 <stdio.h>
+#include <stdio.h>
+#include <string.h>
+#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; j<M->SizeY; j++)
+		for (i=0; i<M->SizeX; 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<Temp.SizeY; j++)
+	{
+		for (i=0; i<Temp.SizeX; i++)
+			fprintf(stderr,"%c", Temp.Cells[j][i]);
+		fprintf(stderr,"\n");
+	}
+
+#if 0
+	// Print Where the man can move
+	for (j = 0; j<Temp.SizeY; j++)
+	{
+		for (i=0; i<Temp.SizeX; i++)
+			printf("%c", Temp.ManMoves[j][i]);
+		printf("\n");
+	}
+#endif
+
+	printf("Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y);
+	printf("Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms,
+			Temp.NumBoxesInPlatform);
+
+}
--- a/sokosol.c	Fri May 05 23:20:33 2006 +0200
+++ b/sokosol.c	Fri May 05 23:43:20 2006 +0200
@@ -4,44 +4,10 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <signal.h>
-
-#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; j<M->SizeY; j++)
-		for (i=0; i<M->SizeX; 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<Temp.SizeY; j++)
-	{
-		for (i=0; i<Temp.SizeX; i++)
-			fprintf(stderr,"%c", Temp.Cells[j][i]);
-		fprintf(stderr,"\n");
-	}
-
-#if 0
-	// Print Where the man can move
-	for (j = 0; j<Temp.SizeY; j++)
-	{
-		for (i=0; i<Temp.SizeX; i++)
-			printf("%c", Temp.ManMoves[j][i]);
-		printf("\n");
-	}
-#endif
-
-	printf("Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y);
-	printf("Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms,
-			Temp.NumBoxesInPlatform);
-
-}
-
-
-
 int InverseMove(char Dir1, char Dir2)
 {
 	if ((Dir1 + Dir2 == DIR_LEFT + DIR_RIGHT) ||