Home > Back-end >  The following code can be run, the problem is, many levels of processing, difficult to achieve. Boss
The following code can be run, the problem is, many levels of processing, difficult to achieve. Boss

Time:10-02

#include
#include//key processing, char ch=getch () does not display the keyboard input
# define MAPFILE "map. TXT"
#include//system
Void ReadMap (int map [] [16], int n);//read the map file
Void printMap (int map [] [16]);//print map
Void keyDown (int map [] [16]);//character mobile operating
Void no (int map [] [16]);
Int boxs;
Void initData (int map [] [16])
{
int i, j;

//load the data allows users to wait for, in general, load data faster
Printf (" the load of the game, please later... ");

//traverse map data in a
for(i=0; I & lt; 14. I++)
{
For (j=0; J & lt; 16. J++)
{

//traverse to the box, the increase in the number of boxes, boxs is the previously defined global variable
If (map [I] [j]==52 | | map [I] [j]==103)
{
Boxs++;
}
}
}
}
Int main ()
{
Int map [14] [16].
For (int n=1; N<44. N++)
{
ReadMap (map, n);
InitData (map);
While (1)
{
System (" CLS ");//clear screen
PrintMap (map);
KeyDown (map);
No (map);
If (boxs==0)
{
break;
}
}
}
System (" pause ");
}
Void no (int map [] [16])
{
For (int I=0; I & lt; 14. I++)
{
For (int j=0; J & lt; 16. J++)
{
//traverse to 2 (people), the recorder, the coordinates of x, y is the previously defined global variable
//traverse to 3, the increase in the number of boxes, boxs is the previously defined global variable
If (map [I] [j]==103)
{
Boxs,;
}
}
}

}
Void ReadMap (int map [] [16], int n)
{
FILE *fp;//file pointer
int i,j;
Int step;
Fp=fopen (MAPFILE, "r");
If (fp==NULL)
{
Printf (" map file open error! \n");
exit(1);
}
If (n<10)//1-9 off
Step=257 * (n - 1) + 5;
After the else//10 levels
Step 9 +=257 * 258 * (n - 1) + 6.
Fseek (fp, step, SEEK_SET);//location of the file pointer
For (I=0; i<14. I++)
{
For (j=0; j<16. J++)
{
The map [I] [j]=fgetc (fp);
}
The fgetc (fp);//get 10
}
fclose(fp);
}
//print map
Void printMap (int map [] [16])
{
int i,j;
For (I=0; i<14. I++)
{
For (j=0; j<16. J++)
{
The switch (map [I] [j])
{
Case '0' ://clearing
Printf (" ");
break;
Case '1' ://wall
Printf (" @ ");
break;
Case '2' ://internal clearing
Case 0:
Printf (" ");
break;
Case '3' ://destination
Printf (" * ");
break;
Case '4' ://box
Case 'f' :
Printf (" & amp;" );
break;
Case 'g' ://box on display at the destination,
Printf (" # ");
break;
Case '6' ://SIMS
Case 'h' :
In case the 'I' :
Printf (" RMB ");
break;
}
}
printf("\n");
}
}
//person move
Void keyDown (int map [] [16])
{//make sure the position of the person
int i,j;
for(i=0; i<14. I++)
{
for(j=0; j<16. J++)
{
If (map [I] [j]==54 | | map [I] [j]==104 | | map [I] [j]==105)
{
break;
}
}
If (map [I] [j]==54 | | map [I] [j]==104 | | map [I] [j]==105)
{
break;
}
}
//the direction key move characters
Char key=getch ();
The switch (key)
{
Case: 'g'//up
Case: 71
If (map [I - 1) [j]==50 | | map [j] [I - 1]==51 | | map [j] [I - 1]==0)
{
The map [j] + [I - 1]=54;
The map [I] [j] -=54;
}
Else if (map [I - 1) [j]==103 | | map [j] [I - 1]==52 | | map [I - 1) [j]==102)
{
If (map [I - 2] [j]==50 | | map [I - 2] [j]==51 | | map [I - 2] [j]==0)
{
The map [I] [j] -=54;
The map [j] + [I - 1]=2;
The map [I - 2] [j] +=52;
}
}
break;
Case 'b' ://down
Case: 66
If (map [j] [I + 1]==50 | | map [j] [I + 1]==51 | | map [j] [I + 1]==0)
{
The map [j] + [I + 1]=54;
The map [I] [j] -=54;
}
Else if (map [I + 1] [j]==103 | | map [j] [I + 1]==52 | | map [I + 1] [j]==102)
{
If (map [j] [I + 2]==50 | | map [j] [I + 2]==51 | | map [j] [I + 2]==0)
{
The map [I] [j] -=54;
The map [j] + [I + 1]=2;
The map [j] + [I + 2]=52;
}
}
break;
Case 'v' ://left
Case: 86
If (map [I] [1]==50 | | map [I] [1]==51 | | map [I] [1]==0)
{
The map [I]] [j - 1 +=54;
The map [I] [j] -=54;
}
Else if (map [I] [1]==103 | | map [I] [1]==52 | | map [I] [1]==102)
{
If (map [I] [j - 2]==50 | | map [I] [j - 2]==51 | | map [I] [j - 2]==0)
{
The map [I] [j] -=54;
Map [I]] [j - 1 +=2;
The map [I] [j] 2 +=52;
}
}
break;
Case 'n' ://
to the rightCase: 78
If (map [I] [j + 1)==50 | | map [I] [j + 1)==51 | | map [I] [j + 1)==0)
{
The map [I] [j + 1) +=54;
The map [I] [j] -=54;
}
Else if (map [I] [j + 1)==103 | | map [I] [j + 1)==52 | | map [I] [j + 1)==102)
{
If (map [I] [m + 2]==50 | | map [I] [m + 2]==51 | | map [I] [m + 2]==0)
{
The map [I] [j] -=54;
The map [I] [j + 1) +=2;
The map [I] + [m + 2]=52;
}
}
break;
}
}

Map. The first thing in the TXT file, I put up three close
[1]
0000000000000000
0000000000000000
0000000000000000
0000011100000000
0000013100000000
0000012111100000
0001114243100000
0001324611100000
0001111410000000
0000001310000000
0000001110000000
0000000000000000
0000000000000000
0000000000000000
[2]
0000000000000000
0000000000000000
0001111100000000
0001622100000000
0001244101110000
0001242101310000
0001112111310000
0000112222310000
0000122212210000
0000122211110000
0000111110000000
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related