推箱子

1、

#include
#include
#include

int main(int argc,const char* argv[])
{
char map[8][8] = { {0,0,3,3,3,3,0,0},
{0,0,3,5,5,3,0,0},
{0,3,3,0,5,3,3,0},
{0,3,0,0,4,5,3,0},
{3,3,0,4,0,0,3,3},
{3,0,0,3,4,4,0,3},
{3,0,0,2,0,0,0,3},
{3,3,3,3,3,3,3,3}};
int cnt=0,step=0;
int human_x=6,human_y=3;
for( ; ; )
{
system(“clear”);
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(0 == map[i][j])
{
printf("%c “,’ ');
}
else if(2 == map[i][j] || 7 == map[i][j])
{
printf(”%c “,’@’);
}
else if(3 == map[i][j])
{
printf(”%c “,’#’);
}
else if(4 == map[i][j])
{
printf(”%c “,’$’);
}
else if(5 == map[i][j])
{
printf(”%c “,‘o’);
}
else if(9 == map[i][j])
{
printf(”%c ",’&’);
}

}
printf("\n");
if(9 == map[1][3] && 9 == map[1][4] && 9 == map[2][4] && 9 == map[3][5])
{
printf(“任务完成”);
printf("%d",step);
return 0;
}
}
switch(getch())
{
case 183:
if( 3 != map[human_x-1][human_y])
{
if(0 == map[human_x-1][human_y])
{
map[human_x][human_y] -= 2;
map[–human_x][human_y] += 2;
}
else if(9 == map[human_x-1][human_y] && 9!= map[human_x-2][human_y] && 4!= map[human_x-2][human_y] && 3!= map[human_x-2][human_y])
{
map[human_x][human_y] -= 2;
map[–human_x][human_y] += 2;
map[human_x][human_y] -= 4;
map[–human_x][human_y] += 4;
human_x++;
}
else if(5 == map[human_x-1][human_y])
{
map[human_x][human_y] -= 2;
map[–human_x][human_y] += 2;
}
else if(4 == map[human_x-1][human_y] && 4!=map[human_x-2][human_y] && 3!=map[human_x-2][human_y] )
{
map[human_x][human_y] -= 2;
map[–human_x][human_y] += 2;
map[human_x][human_y] -= 4;
map[–human_x][human_y] += 4;
human_x++;
}
}
step++;break;
case 184:
if(3 != map[human_x+1][human_y])
{
if(0 == map[human_x+1][human_y])
{
map[human_x][human_y] -= 2;
map[++human_x][human_y] += 2;
}
else if(9 == map[human_x+1][human_y] && 9!= map[human_x+2][human_y] && 4!= map[human_x+2][human_y] && 3!= map[human_x+2][human_y])
{
map[human_x][human_y] -= 2;
map[++human_x][human_y] += 2;
map[human_x][human_y] -= 4;
map[++human_x][human_y] += 4;
human_x–;
}
else if(5 == map[human_x+1][human_y])
{
map[human_x][human_y] -= 2;
map[++human_x][human_y] += 2;
}
else if(4 == map[human_x+1][human_y] &&4!=map[human_x+2][human_y] && 3!=map[human_x+2][human_y] )
{
map[human_x][human_y] -= 2;
map[++human_x][human_y] += 2;
map[human_x][human_y] -= 4;
map[++human_x][human_y] += 4;
human_x–;
}
}
step++;break;
case 185:
if( 3 != map[human_x][human_y+1])
{
if(0 == map[human_x][human_y+1])
{
map[human_x][human_y] -= 2;
map[human_x][++human_y] += 2;
}
else if(9 == map[human_x][human_y+1] && 9 != map[human_x][human_y+2] && 4!= map[human_x][human_y+2] && 3!= map[human_x][human_y+2])
{
map[human_x][human_y] -= 2;
map[human_x][++human_y] += 2;
map[human_x][human_y] -= 4;
map[human_x][++human_y] += 4;
human_y–;
}
else if(5 == map[human_x][human_y+1])
{
map[human_x][human_y] -= 2;
map[human_x][++human_y] += 2;
}
else if(4 == map[human_x][human_y+1] &&4!=map[human_x][human_y+2] && 3!=map[human_x][human_y+2] ) {
map[human_x][human_y] -= 2;
map[human_x][++human_y] += 2;
map[human_x][human_y] -= 4;
map[human_x][++human_y] += 4;
human_y–;
}
}
step++;break;
case 186:
if( 3 != map[human_x][human_y-1])
{
if(0 == map[human_x][human_y-1])
{
map[human_x][human_y] -= 2;
map[human_x][–human_y] += 2;
}
else if(9 == map[human_x][human_y-1] && 9 != map[human_x][human_y-2] && 4!= map[human_x][human_y-2] && 3!= map[human_x][human_y-2])
{
map[human_x][human_y] -= 2;
map[human_x][–human_y] += 2;
map[human_x][human_y] -= 4;
map[human_x][–human_y] += 4;
human_y++;
}
else if(5 == map[human_x][human_y-1])
{
map[human_x][human_y] -= 2;
map[human_x][–human_y] += 2;
}
else if(4==map[human_x][human_y-1] &&4!=map[human_x][human_y-2] && 3!=map[human_x][human_y-2] )
{
map[human_x][human_y] -= 2;
map[human_x][–human_y] += 2;
map[human_x][human_y] -= 4;
map[human_x][–human_y] += 4;
human_y++;
}
}
step++;break;
}
}
}

2、

#include
#include
#include

int main()
{
char map[8][8] = {
{0,0,3,3,3,3,0,0},
{0,0,3,5,5,3,0,0},
{0,3,3,0,5,3,3,0},
{0,3,0,0,4,5,3,0},
{3,3,0,4,0,0,3,3},
{3,0,0,3,4,4,0,3},
{3,0,0,2,0,0,0,3},
{3,3,3,3,3,3,3,3},
};//数组

int x = 6 , y = 3 , step = 0;

for(; ; )
{
system(“clear”);

int cnt = 0;
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
switch(map[i][j])
{
case 0: printf(" “); break;
case 2: printf(”@ “); break;
case 3: printf(”# “); break;
case 4: printf(”$ "); break;
case 5: printf(“O “); break;
case 7: printf(”@ “); break;
case 9: printf(”$ “); cnt++;
}//输出地图
}
printf(”\n”);
}

if(4 == cnt)
{
printf(“恭喜完成任务,一共走%d步!\n”, step);
return 0;
}

int ox = 0 , oy = 0;
switch(getch())
{
case 183: ox–; break;//上
case 184: ox++; break;//下
case 185: oy++; break;//右
case 186: oy–; break;//左

}

if(0 == map[x+ox][y+oy] || 5 == map[x+ox][y+oy])
{
map[x+ox][y+oy] += 2;
map[x][y] -= 2;
x += ox;
y += oy;
step++;//记录步数
continue;
}

if((4 == map[x+ox][y+oy] || 9==map[x+ox][y+oy]) &&
(0 == map[x+ox2][y+oy2] || 5 == map[x+ox2][y+oy2]))
{
map[x+ox2][y+oy2] += 4;
map[x+ox][y+oy] -= 2;
map[x][y] -= 2;
x += ox;
y += oy;
step++;
}
}
}

你可能感兴趣的:(推箱子)