old/sokosol2.c
changeset 3 29cc57a9678e
parent 0 be33ecaa3619
equal deleted inserted replaced
2:415159228618 3:29cc57a9678e
       
     1 #include <stdio.h>
       
     2 #include <string.h>
       
     3 #include <stdlib.h>
       
     4 
       
     5 #define BOX '*'
       
     6 #define WALL '#'
       
     7 #define MAN '8'
       
     8 #define PLATFORM '+'
       
     9 #define BOXINPLATFORM 'O'
       
    10 #define MANINPLATFORM 'E'
       
    11 #define BLANK ' '
       
    12 
       
    13 #define DIR_LEFT 0
       
    14 #define DIR_RIGHT 1
       
    15 #define DIR_UP 2
       
    16 #define DIR_DOWN 3
       
    17 
       
    18 #define MAX_X 50
       
    19 #define MAX_Y 50
       
    20 #define MAX_MOVES 100
       
    21 
       
    22 struct Map
       
    23 {
       
    24 	char Cells[MAX_X][MAX_Y];
       
    25 	int SizeX, SizeY;
       
    26 	int ManX, ManY;
       
    27 	int NumPlatforms;
       
    28 	int NumBoxesInPlatform;
       
    29 };
       
    30 
       
    31 
       
    32 
       
    33 void ReadMap(struct Map *M, char *FileName)
       
    34 {
       
    35 	FILE *Fitxer;
       
    36 	int i,j;
       
    37 
       
    38 	if(!(Fitxer = fopen(FileName, "r")))
       
    39 	{
       
    40 		printf("Error opening %s!", FileName);
       
    41 		exit(1);
       
    42 	}
       
    43 
       
    44 	M->SizeX=0;
       
    45 	M->SizeY=0;
       
    46 	while (!feof(Fitxer))
       
    47 	{
       
    48 		fgets(M->Cells[M->SizeY++], MAX_X, Fitxer);
       
    49 	}
       
    50 	M->SizeY--;
       
    51 	M->SizeX = strlen(M->Cells[0]) - 1;
       
    52 
       
    53 	M->NumPlatforms = 0;
       
    54 	M->NumBoxesInPlatform = 0;
       
    55 	for (j = 0; j<M->SizeY; j++)
       
    56 		for (i=0; i<M->SizeX; i++)
       
    57 		{
       
    58 			if (M->Cells[j][i] == MAN)
       
    59 			{ M->ManX = i; M->ManY = j; }
       
    60 			else if (M->Cells[j][i] == PLATFORM)
       
    61 				M->NumPlatforms++;
       
    62 			else if (M->Cells[j][i] == BOXINPLATFORM)
       
    63 			{
       
    64 				M->NumPlatforms++;
       
    65 				M->NumBoxesInPlatform++;
       
    66 			}
       
    67 		}
       
    68 }
       
    69 
       
    70 
       
    71 void ShowMap (struct Map *M)
       
    72 {
       
    73 	int i,j;
       
    74 	for (j = 0; j<M->SizeY; j++)
       
    75 	{
       
    76 		for (i=0; i<M->SizeX; i++)
       
    77 			printf("%c", M->Cells[j][i]);
       
    78 		printf("\n");
       
    79 	}
       
    80 	printf("Man is at (%i,%i)\n", M->ManX, M->ManY);
       
    81 	printf("Platforms: %i, BoxesInPlatform: %i\n", M->NumPlatforms,
       
    82 			M->NumBoxesInPlatform);
       
    83 }
       
    84 
       
    85 
       
    86 
       
    87 void CopyMap (struct Map *Mdest, struct Map *Morig)
       
    88 {
       
    89 	memcpy((void *) Mdest, (void *) Morig, sizeof (struct Map));
       
    90 }
       
    91 
       
    92 int MoveMan (struct Map *M, int Direction)
       
    93 {
       
    94 	int NewX, NewY;
       
    95 
       
    96 	// Check if man is where it should be
       
    97 	if (M->Cells[M->ManY][M->ManX] != MAN &&
       
    98 		M->Cells[M->ManY][M->ManX] != MANINPLATFORM)
       
    99 	{
       
   100 		printf("Man isn't where it should be!\n");
       
   101 		exit(2);
       
   102 	}
       
   103 
       
   104 	// Process Movement
       
   105 	if (Direction == DIR_LEFT)
       
   106 	{ NewX = M->ManX - 1; NewY = M->ManY; }
       
   107 	else if (Direction == DIR_RIGHT)
       
   108 	{ NewX = M->ManX + 1; NewY = M->ManY; }
       
   109 	else if (Direction == DIR_UP)
       
   110 	{ NewX = M->ManX; NewY = M->ManY - 1; }
       
   111 	else if (Direction == DIR_DOWN)		// if not needed
       
   112 	{ NewX = M->ManX; NewY = M->ManY + 1; }
       
   113 
       
   114 
       
   115 
       
   116 	// What's in front of the man?
       
   117 
       
   118 	if (M->Cells[NewY][NewX] == WALL)
       
   119 	{
       
   120 		return 0;	// ILLEGAL MOVE
       
   121 	}
       
   122 	else if (M->Cells[NewY][NewX] == BOX)
       
   123 	{
       
   124 		if (M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)] ==
       
   125 			BLANK)
       
   126 		{
       
   127 			M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)]
       
   128 				= BOX;
       
   129 
       
   130 			if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
       
   131 			{	// Man is over Platform
       
   132 				M->Cells[M->ManY][M->ManX] = PLATFORM;
       
   133 				M->Cells[NewY][NewX] = MAN;
       
   134 			}
       
   135 			else	// Man is over Blank
       
   136 			{
       
   137 				M->Cells[M->ManY][M->ManX] = BLANK;
       
   138 				M->Cells[NewY][NewX] = MAN;
       
   139 			}
       
   140 			M->ManX = NewX; M->ManY = NewY;
       
   141 		}
       
   142 		else
       
   143 		if (M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)] ==
       
   144 				PLATFORM)
       
   145 		{
       
   146 			M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)]
       
   147 				= BOXINPLATFORM;
       
   148 
       
   149 			if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
       
   150 			{	// Man is over Platform
       
   151 				M->Cells[M->ManY][M->ManX] = PLATFORM;
       
   152 				M->Cells[NewY][NewX] = MAN;
       
   153 			}
       
   154 			else	// Man is over Blank
       
   155 			{
       
   156 				M->Cells[M->ManY][M->ManX] = BLANK;
       
   157 				M->Cells[NewY][NewX] = MAN;
       
   158 			}
       
   159 			M->ManX = NewX; M->ManY = NewY;
       
   160 
       
   161 			M->NumBoxesInPlatform++;
       
   162 		}
       
   163 		else
       
   164 		{
       
   165 			return 0;	// ILLEGAL MOVE
       
   166 		}
       
   167 	}else
       
   168 	if (M->Cells[NewY][NewX] == BOXINPLATFORM)
       
   169 	{
       
   170 		if (M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)] ==
       
   171 			BLANK)
       
   172 		{
       
   173 			M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)]
       
   174 				= BOX;
       
   175 
       
   176 			if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
       
   177 			{	// Man is over Platform
       
   178 				M->Cells[M->ManY][M->ManX] = PLATFORM;
       
   179 				M->Cells[NewY][NewX] = MANINPLATFORM;
       
   180 			}
       
   181 			else	// Man is over Blank
       
   182 			{
       
   183 				M->Cells[M->ManY][M->ManX] = BLANK;
       
   184 				M->Cells[NewY][NewX] = MANINPLATFORM;
       
   185 			}
       
   186 			M->ManX = NewX; M->ManY = NewY;
       
   187 			M->NumBoxesInPlatform--;
       
   188 		}
       
   189 		else
       
   190 		if (M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)] ==
       
   191 				PLATFORM)
       
   192 		{
       
   193 			M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)]
       
   194 				= BOXINPLATFORM;
       
   195 
       
   196 			if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
       
   197 			{	// Man is over Platform
       
   198 				M->Cells[M->ManY][M->ManX] = PLATFORM;
       
   199 				M->Cells[NewY][NewX] = MANINPLATFORM;
       
   200 			}
       
   201 			else	// Man is over Blank
       
   202 			{
       
   203 				M->Cells[M->ManY][M->ManX] = BLANK;
       
   204 				M->Cells[NewY][NewX] = MANINPLATFORM;
       
   205 			}
       
   206 			M->ManX = NewX; M->ManY = NewY;
       
   207 		}
       
   208 		else
       
   209 		{
       
   210 			return 0;	// ILLEGAL MOVE
       
   211 		}
       
   212 	}
       
   213 	else if (M->Cells[NewY][NewX] == PLATFORM)
       
   214 	{
       
   215 		if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
       
   216 		{	// Man is over Platform
       
   217 			M->Cells[M->ManY][M->ManX] = PLATFORM;
       
   218 			M->Cells[NewY][NewX] = MANINPLATFORM;
       
   219 		}
       
   220 		else	// Man is over Blank
       
   221 		{
       
   222 			M->Cells[M->ManY][M->ManX] = BLANK;
       
   223 			M->Cells[NewY][NewX] = MANINPLATFORM;
       
   224 		}
       
   225 		M->ManX = NewX; M->ManY = NewY;
       
   226 	}
       
   227 	else if (M->Cells[NewY][NewX] == BLANK)		// IF not needed
       
   228 	{
       
   229 		if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
       
   230 		{	// Man is over Platform
       
   231 			M->Cells[M->ManY][M->ManX] = PLATFORM;
       
   232 			M->Cells[NewY][NewX] = MAN;
       
   233 		}
       
   234 		else	// Man is over Blank
       
   235 		{
       
   236 			M->Cells[M->ManY][M->ManX] = BLANK;
       
   237 			M->Cells[NewY][NewX] = MAN;
       
   238 		}
       
   239 		M->ManX = NewX; M->ManY = NewY;
       
   240 	}
       
   241 	return 1;
       
   242 }
       
   243 
       
   244 int main()
       
   245 {
       
   246 	struct Map Morigin, M;
       
   247 	char Moves[MAX_MOVES];
       
   248 	int NumMoves;
       
   249 	int IllegalMove;
       
   250 	int Carry;
       
   251 	int Arrel;
       
   252 
       
   253 	int i, j;
       
   254 
       
   255 	ReadMap(&Morigin, "model");
       
   256 
       
   257 	ShowMap(&Morigin);
       
   258 
       
   259 	CopyMap(&M, &Morigin);
       
   260 
       
   261 	printf("Numero de moviments a provar: ");
       
   262 	scanf("%i", &NumMoves);
       
   263 	printf("Arrel: ");
       
   264 	scanf("%i", &Arrel);
       
   265 	srand(Arrel);
       
   266 
       
   267 	// Process the combinations.
       
   268 	// Order: Left, Right, Up, Down
       
   269 	while (M.NumPlatforms != M.NumBoxesInPlatform)
       
   270 	{
       
   271 		// Reget the original map
       
   272 		CopyMap(&M, &Morigin);
       
   273 
       
   274 /*
       
   275 		// Print the combination
       
   276 
       
   277 		for (i=0; i < NumMoves; i++)
       
   278 		{
       
   279 			printf("%c", Moves[i]);
       
   280 		}
       
   281 		printf("\n");
       
   282 */
       
   283 
       
   284 		// Process the combination
       
   285 		for (i = 0; i < NumMoves; i++)
       
   286 		{
       
   287 			if (!MoveMan(&M, Moves[i]=rand()%4))
       
   288 				break;
       
   289 		}
       
   290 					
       
   291 	}
       
   292 
       
   293 	// Print the combination
       
   294 	for (i=0; i < NumMoves; i++)
       
   295 	{
       
   296 		printf("%c", Moves[i]+48);
       
   297 	}
       
   298 	printf("\n");
       
   299 
       
   300 /*
       
   301 	// Print The Steps
       
   302 	strcpy(Moves, "DDDRUU");
       
   303 
       
   304 	for (i=0; i < 6; i++)
       
   305 	{
       
   306 		printf("%i", MoveMan(&M, Moves[i]));
       
   307 		ShowMap(&M);
       
   308 	}
       
   309 */
       
   310 
       
   311 }