-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPushBox.cpp
147 lines (138 loc) · 5.02 KB
/
PushBox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
//交换字符数组元素
void swapPosition(char ch[][11],int oldX,int oldY,int newX,int newY)
{
char temp;
temp = *(*(ch + oldX) + oldY);
*(*(ch + oldX) + oldY) = *(*(ch + newX) + newY);
*(*(ch + newX) + newY) = temp;
}
//打印数组
void printArray(char (*ch)[11])
{
for(int i = 0;i < 10;i++)
{
for(int j = 0;j < 10;j++)
{
printf("%c\t",*(*(ch + i) + j));
}
printf("\n");
}
}
int main(int argc, char* argv[])
{
//定义数组
char ch[10][11] = {
{'#','#','#','#','#','#','#','#','#','#','\0'},
{'#','#','#',' ',' ','#','#','#','#','#','\0'},
{'#','0','X',' ',' ','#','#','#','#','#','\0'},
{'#','#','#','#',' ','#','#','#','#','#','\0'},
{'#','#','#',' ',' ',' ',' ','#','#','#','\0'},
{'#','#','#',' ',' ',' ',' ',' ','#','#','\0'},
{'#','#','#','#','#',' ',' ',' ','#','#','\0'},
{'#','#','#','#','#',' ',' ',' ',' ',' ','\0'},
{'#','#','#','#','#','#','#','#',' ',' ','\0'},
{'#','#','#','#','#','#','#','#','#','#','\0'}
};
//打印数组
printArray(ch);
//记录小人及箱子位置
//定义路的标志变量street
int personX = 2,personY = 1,boxX = 2, boxY = 2;
char street = ' ';
//提示用户输入移动方向
printf("请输入小人移动方向:(w-上,s-下,a-左,d-右)\n");
//定义记录用户输入的方向
char direction;
//根据方向定义循环语句
while(1)
{
scanf("%c",&direction);
getchar();//接收键盘回车符
switch(direction)
{
case 'w':
case 'W':
if(*(*(ch+personX-1)+personY) == street)
{
swapPosition(ch,personX,personY,personX - 1,personY );
personX--;
}
else if(*(*(ch+personX-1)+personY) == *(*(ch+boxX)+boxY))
{
if(*(*(ch+boxX-1)+boxY) == street)
{
swapPosition(ch,boxX,boxY,boxX - 1,boxY);
boxX--;
swapPosition(ch,personX,personY,personX - 1,personY);
personX--;
}
}
break;
case 's':
case 'S':
if(*(*(ch+personX + 1) + personY) == street)
{
swapPosition(ch,personX,personY,personX + 1,personY );
personX++;
}
else if(*(*(ch+personX+1)+personY) == *(*(ch+boxX)+boxY))
{
if(*(*(ch+boxX+1)+boxY) == street)
{
swapPosition(ch,boxX,boxY,boxX + 1,boxY);
boxX++;
swapPosition(ch,personX,personY,personX + 1,personY);
personX++;
}
}
break;
case 'a':
case 'A':
if(*(*(ch+personX)+personY - 1) == street)
{
swapPosition(ch,personX,personY,personX ,personY - 1 );
personY--;
}
else if(*(*(ch+personX)+personY - 1) == *(*(ch+boxX)+boxY))
{
if(*(*(ch+boxX)+boxY - 1) == street)
{
swapPosition(ch,boxX,boxY,boxX,boxY - 1);
boxY--;
swapPosition(ch,personX,personY,personX,personY - 1);
personY--;
}
}
break;
case 'd':
case 'D':
if(*(*(ch+personX)+personY + 1) == street)
{
swapPosition(ch,personX,personY,personX ,personY + 1 );
personY++;
}
else if(*(*(ch+personX)+personY + 1) == *(*(ch+boxX)+boxY))
{
if(*(*(ch+boxX)+boxY + 1) == street)
{
swapPosition(ch,boxX,boxY,boxX,boxY + 1);
boxY++;
swapPosition(ch,personX,personY,personX,personY + 1);
personY++;
}
}
break;
case 'q':
case 'Q':
return 0;
}
printArray(ch);
//定义结束标志
if (boxY == 9) {
printf("恭喜你,游戏过关!\n");
return 0;
}
}
return 0;
}