??在第一版的代码中,将人在目标点上以及箱子在目标点上另外输出,在后续的获取方向键并处理时,只需减去人和箱子的数字就可以刷新目标点的显示,不会出现“吃”目标点的情况。 ??而在第二版的代码中,使用偏移值的改变来简化代码,只需通过方向键的按键活动来改变偏移值,就可实现通过两个嵌套的if语句来进行人、箱子、目标点的改变与刷新。 以下是第一版的推箱子代码:
#include <stdio.h>
#include <getch.h>
#include <stdlib.h>
int main(int argc,const char* argv[])
{
char map[8][8] = {
{0,0,1,1,1,1,0,0},
{0,0,1,4,4,1,0,0},
{0,1,1,0,4,1,1,0},
{1,1,1,0,3,4,1,0},
{1,1,0,3,0,0,1,1},
{1,0,0,1,3,3,0,1},
{1,0,0,2,0,0,0,1},
{1,1,1,1,1,1,1,1},
};
char m_x = 6,m_y = 3;
int step = 0;
for(;;)
{
int cnt = 0;
system("clear");
for(int i =0;i<8;i++)
{
for(int j =0;j<8;j++)
{
switch(map[i][j])
{
case 0:printf(" ");break;
case 1:printf("# ");break;
case 2:printf("@ ");break;
case 3:printf("$ ");break;
case 4:printf("0 ");break;
case 6:printf("@ ");break;
case 7:printf("$ ");
cnt++;
break;
}
}
printf("\n");
}
if(cnt == 4)
{
printf("游戏结束,共走了%d步\n",step);
return 0 ;
}
switch(getch())
{
case 183:
if(0 == map[m_x-1][m_y] || 4 == map[m_x-1][m_y])
{
map[m_x-1][m_y] += 2;
map[m_x][m_y] -= 2;
m_x--;
step++;
}
else if(3 == map[m_x-1][m_y] || 7 == map[m_x-1][m_y])
{
if(0 == map[m_x-2][m_y] || 4 == map[m_x-2][m_y])
{
map[m_x-2][m_y] += 3;
map[m_x-1][m_y] -= 1;
map[m_x][m_y] -= 2;
m_x--;
step++;
}
}
break;
case 184:
if(0 == map[m_x+1][m_y] || 4 == map[m_x+1][m_y])
{
map[m_x+1][m_y] += 2;
map[m_x][m_y] -= 2;
m_x++;
step++;
}
else if(3 == map[m_x+1][m_y] || 7 == map[m_x+1][m_y])
{
if(0 == map[m_x+2][m_y] || 4 == map[m_x+2][m_y])
{
map[m_x+2][m_y] += 3;
map[m_x+1][m_y] -= 1;
map[m_x][m_y] -= 2;
m_x++;
step++;
}
}
break;
case 185:
if(0 == map[m_x][m_y+1] || 4 == map[m_x][m_y+1])
{
map[m_x][m_y+1] += 2;
map[m_x][m_y] -= 2;
m_y++;
step++;
}
else if(3 == map[m_x][m_y+1] || 7 == map[m_x][m_y+1])
{
if(0 == map[m_x][m_y+2] || 4 == map[m_x][m_y+2])
{
map[m_x][m_y+2] += 3;
map[m_x][m_y+1] -= 1;
map[m_x][m_y] -= 2;
m_y++;
step++;
}
}
break;
case 186:
if(0 == map[m_x][m_y-1] || 4 == map[m_x][m_y-1])
{
map[m_x][m_y-1] += 2;
map[m_x][m_y] -= 2;
m_y--;
step++;
}
else if(3 == map[m_x][m_y-1] || 7 == map[m_x][m_y-1])
{
if(0 == map[m_x][m_y-2] || 4 == map[m_x][m_y-2])
{
map[m_x][m_y-2] += 3;
map[m_x][m_y-1] -= 1;
map[m_x][m_y] -= 2;
m_y--;
step++;
}
}
break;
}
}
}
以下是第二版代码:
#include <stdio.h>
#include <getch.h>
#include <stdlib.h>
int main(int argc,const char* argv[])
{
char map[8][8] = {
{0,0,1,1,1,1,0,0},
{0,0,1,4,4,1,0,0},
{0,1,1,0,4,1,1,0},
{1,1,1,0,3,4,1,0},
{1,1,0,3,0,0,1,1},
{1,0,0,1,3,3,0,1},
{1,0,0,2,0,0,0,1},
{1,1,1,1,1,1,1,1},
};
char m_x = 6,m_y = 3;
int step = 0;
for(;;)
{
int cnt = 0;
system("clear");
for(int i =0;i<8;i++)
{
for(int j =0;j<8;j++)
{
switch(map[i][j])
{
case 0:printf(" ");break;
case 1:printf("# ");break;
case 2:printf("@ ");break;
case 3:printf("$ ");break;
case 4:printf("0 ");break;
case 6:printf("@ ");break;
case 7:printf("$ ");
cnt++;
break;
}
}
printf("\n");
}
if(cnt == 4)
{
printf("游戏结束,共走了%d步\n",step);
return 0 ;
}
int x = 0,y = 0;
switch(getch())
{
case 183:
x = -1;break;
case 184:
x = 1;break;
case 185:
y = 1;break;
case 186:
y = -1;break;
}
if(0 == map[m_x+x][m_y+y] || 4 == map[m_x+x][m_y+y])
{
map[m_x+x][m_y+y] += 2;
map[m_x][m_y] -= 2;
m_x +=x;m_y +=y;
step++;
}
else if(3 == map[m_x+x][m_y+y] || 7 == map[m_x+x][m_y+y])
{
if(0 == map[m_x+2*x][m_y+2*y] || 4 == map[m_x+2*x][m_y+2*y])
{
map[m_x+2*x][m_y+2*y] += 3;
map[m_x+x][m_y+y] -= 1;
map[m_x][m_y] -= 2;
m_x += x; m_y +=y;
step++;
}
}
}
}
可以看到,代码量减少了将近一半。
|