map.c
changeset 4 d9259a605dec
child 6 bfbca2c0fc70
equal deleted inserted replaced
3:29cc57a9678e 4:d9259a605dec
       
     1 #include <stdio.h>
       
     2 #include <stdio.h>
       
     3 #include <string.h>
       
     4 #include "general.h"
       
     5 
       
     6 void CopyMap (struct Map *Mdest, const struct Map *Morig)
       
     7 {
       
     8 	memcpy((void *) Mdest, (void *) Morig, sizeof (struct Map));
       
     9 }
       
    10 
       
    11 void ReadMap(struct Map *M, char *FileName)
       
    12 {
       
    13 	FILE *Fitxer;
       
    14 	int i,j;
       
    15 
       
    16 	if(!(Fitxer = fopen(FileName, "r")))
       
    17 	{
       
    18 		printf("Error opening %s!", FileName);
       
    19 		exit(1);
       
    20 	}
       
    21 
       
    22 	M->SizeX=0;
       
    23 	M->SizeY=0;
       
    24 	while (!feof(Fitxer))
       
    25 	{
       
    26 		fgets(M->Cells[M->SizeY], MAX_X, Fitxer);
       
    27 		M->SizeY++;
       
    28 	}
       
    29 	M->SizeY--;
       
    30 	M->SizeX = strlen(M->Cells[0]) - 1;
       
    31 
       
    32 	M->NumPlatforms = 0;
       
    33 	M->NumBoxesInPlatform = 0;
       
    34 	M->NumBoxes = 0;
       
    35 	for (j = 0; j<M->SizeY; j++)
       
    36 		for (i=0; i<M->SizeX; i++)
       
    37 		{
       
    38 			if (M->Cells[j][i] == MAN)
       
    39 			{ 
       
    40 				M->Man.x = i; M->Man.y = j; 
       
    41 				M->Cells[M->Man.y][M->Man.x] = BLANK;
       
    42 			}
       
    43 
       
    44 			if (M->Cells[j][i] == PLATFORM)
       
    45 				M->NumPlatforms++;
       
    46 			else if (M->Cells[j][i] == BOXINPLATFORM)
       
    47 			{
       
    48 				M->Cells[j][i] = PLATFORM;
       
    49 
       
    50 				M->NumPlatforms++;
       
    51 				M->NumBoxesInPlatform++;
       
    52 
       
    53 				M->Box[M->NumBoxes].x = i;
       
    54 				M->Box[M->NumBoxes].y = j;
       
    55 				M->NumBoxes++;
       
    56 			} else if (M->Cells[j][i] == BOX)
       
    57 			{
       
    58 				M->Cells[j][i] = BLANK;
       
    59 
       
    60 				M->Box[M->NumBoxes].x = i;
       
    61 				M->Box[M->NumBoxes].y = j;
       
    62 				M->NumBoxes++;
       
    63 			} else if (M->Cells[j][i] == CORNER)
       
    64 			{
       
    65 				M->Cells[j][i] = CORNER;
       
    66 			} else if (M->Cells[j][i] != WALL)
       
    67 			{
       
    68 				if (    (M->Cells[j][i-1] == WALL &&
       
    69 					 M->Cells[j-1][i] == WALL) ||
       
    70 					(M->Cells[j][i-1] == WALL &&
       
    71 					 M->Cells[j+1][i] == WALL) ||
       
    72 					(M->Cells[j][i+1] == WALL &&
       
    73 					 M->Cells[j-1][i] == WALL) ||
       
    74 					(M->Cells[j][i+1] == WALL &&
       
    75 					 M->Cells[j+1][i] == WALL))
       
    76 				M->Cells[j][i] = CORNER;
       
    77 			}
       
    78 				
       
    79 		}
       
    80 
       
    81 }
       
    82 
       
    83 
       
    84 void ShowMap (const struct Map *M)
       
    85 {
       
    86 	struct Map Temp;
       
    87 	int i,j;
       
    88 
       
    89 	CopyMap(&Temp, M);
       
    90 
       
    91 	Temp.Cells[Temp.Man.y][Temp.Man.x] = MAN;
       
    92 
       
    93 	for (i = 0; i < Temp.NumBoxes; i++)
       
    94 	{
       
    95 		if (Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] == PLATFORM)
       
    96 			Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] =BOXINPLATFORM;
       
    97 		else
       
    98 			Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] = BOX;
       
    99 	}
       
   100 
       
   101 	for (j = 0; j<Temp.SizeY; j++)
       
   102 	{
       
   103 		for (i=0; i<Temp.SizeX; i++)
       
   104 			fprintf(stderr,"%c", Temp.Cells[j][i]);
       
   105 		fprintf(stderr,"\n");
       
   106 	}
       
   107 
       
   108 #if 0
       
   109 	// Print Where the man can move
       
   110 	for (j = 0; j<Temp.SizeY; j++)
       
   111 	{
       
   112 		for (i=0; i<Temp.SizeX; i++)
       
   113 			printf("%c", Temp.ManMoves[j][i]);
       
   114 		printf("\n");
       
   115 	}
       
   116 #endif
       
   117 
       
   118 	printf("Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y);
       
   119 	printf("Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms,
       
   120 			Temp.NumBoxesInPlatform);
       
   121 
       
   122 }