推箱子(c代码)

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<stdio.h>
#include <stdlib.h>
int x,y;
int boxs;
#define LONG 8 //图案大小
#define WIDTH 8
int map[WIDTH][LONG] = { //打印图形结构<!-- more -->
{0,0,1,1,1,0,0,0},
{0,0,1,4,1,0,0,0},
{0,0,1,0,1,1,1,1},
{1,1,1,3,0,3,4,1},
{1,4,0,3,2,1,1,1},
{1,1,1,1,3,1,0,0},
{0,0,0,1,4,1,0,0},
{0,0,0,1,1,1,0,0}};
int boxNum(){ //盒子数量
int i,j;
boxs = 0;
for(i = 0; i < LONG; i++){
for(j = 0; j < WIDTH; j++){
if(map[i][j] == 3){
boxs++;
}
}
}
}
void initData(){ //确定人的坐标
int i,j;
for(i = 0; i < LONG; i++){
for(j = 0; j < WIDTH; j++){
if(map[i][j] == 2){
x = i;
y = j;
}
}
}
}
void drawMap(){ //打印图形
int i,j;
for(i = 0; i < LONG; i++){
for(j = 0; j < WIDTH; j++){
switch(map[i][j]){
case 0:
printf(" "); //空
break;
case 1:
printf("■ "); //墙
break;
case 2:
printf("♀ "); //人
break;
case 3:
printf("◆ "); //箱子
break;
case 4:
printf("● "); //目标位置
break;
case 5:
printf("★ "); //完成目标
break;
}
}
printf("\n");
}
}
void moveUp(){ //上移
printf("up\n");
if(map[x - 1][y] == 0){ //前方为空
map[x - 1][y] = 2;
map[x][y] = 0;
}
if(map[x - 1][y] == 3){ //前方为箱子
if(map[x - 2][y] == 0){ //箱子前方为空
map[x - 2][y] = 3;
map[x - 1][y] = 2;
map[x][y] = 0;
}
if(map[x - 2][y] == 4){ //箱子前方为目标位置
map[x - 2][y] = 5;
map[x - 1][y] = 2;
map[x][y] = 0;
}
}
}
void moveLeft(){ //左移
printf("left\n");
if(map[x][y - 1] == 0){
map[x][y - 1] = 2;
map[x][y] = 0;
}
if(map[x][y - 1] == 3){
if(map[x][y - 2] == 0){
map[x][y - 2] = 3;
map[x][y - 1] = 2;
map[x][y] = 0;
}
if(map[x][y - 2] == 4){
map[x][y - 2] = 5;
map[x][y - 1] = 2;
map[x][y] = 0;
}
}
}
void moveDown(){ //下移
printf("down\n");
if(map[x + 1][y] == 0){
map[x + 1][y] = 2;
map[x][y] = 0;
}
if(map[x + 1][y] == 3){
if(map[x + 2][y] == 0){
map[x + 2][y] = 3;
map[x + 1][y] = 2;
map[x][y] = 0;
}
if(map[x + 2][y] == 4){
map[x + 2][y] = 5;
map[x + 1][y] = 2;
map[x][y] = 0;
}
}
}
void moveRight(){ //右移
printf("ringht\n");
if(map[x][y + 1] == 0){
map[x][y + 1] = 2;
map[x][y] = 0;
}
if(map[x][y + 1] == 3){
if(map[x][y + 2] == 0){
map[x][y + 2] = 3;
map[x][y + 1] = 2;
map[x][y] = 0;
}
if(map[x][y + 2] == 4){
map[x][y + 2] = 5;
map[x][y + 1] = 2;
map[x][y] = 0;
}
}
}
int main(int argc,char*argv[]){
char direction;
while(1){
system("clear"); //清屏
initData(); //人的位置
boxNum(); //盒子的数量
drawMap(); //打印图
direction = getchar(); //输入方向w,a,s,d
switch(direction){
case 'w':
moveUp();
break;
case 'a':
moveLeft();
break;
case 's':
moveDown();
break;
case 'd':
moveRight();
break;
}
if(boxs == 0){ //没有盒子完成
printf("game over\n");
break;
}
}
return 0;
}