map.c
changeset 6 bfbca2c0fc70
parent 4 d9259a605dec
child 8 b41a580b3abe
equal deleted inserted replaced
5:9d1e320acbb0 6:bfbca2c0fc70
     6 void CopyMap (struct Map *Mdest, const struct Map *Morig)
     6 void CopyMap (struct Map *Mdest, const struct Map *Morig)
     7 {
     7 {
     8 	memcpy((void *) Mdest, (void *) Morig, sizeof (struct Map));
     8 	memcpy((void *) Mdest, (void *) Morig, sizeof (struct Map));
     9 }
     9 }
    10 
    10 
    11 void ReadMap(struct Map *M, char *FileName)
    11 int are_boxes_equal(const struct Position b1[], const struct Position b2[],
       
    12 	int n)
    12 {
    13 {
    13 	FILE *Fitxer;
    14 	int i;
    14 	int i,j;
    15 	char tmp[MAX_Y][MAX_X]; /* !!!argh */
    15 
    16 
    16 	if(!(Fitxer = fopen(FileName, "r")))
    17 	memset(tmp, 0, sizeof(tmp));
       
    18 
       
    19 	for (i=0; i < n; i++)
    17 	{
    20 	{
    18 		printf("Error opening %s!", FileName);
    21 		tmp[b1[i].y][b1[i].x] = 1;
    19 		exit(1);
       
    20 	}
    22 	}
    21 
    23 	for (i=0; i < n; i++)
    22 	M->SizeX=0;
       
    23 	M->SizeY=0;
       
    24 	while (!feof(Fitxer))
       
    25 	{
    24 	{
    26 		fgets(M->Cells[M->SizeY], MAX_X, Fitxer);
    25 		if (tmp[b2[i].y][b2[i].x] != 1)
    27 		M->SizeY++;
    26 			return FALSE;
    28 	}
    27 	}
    29 	M->SizeY--;
    28 	return TRUE;
    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 }
    29 }
    82 
    30 
    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 }