码迷,mamicode.com
首页 > 其他好文 > 详细

推箱子

时间:2020-07-14 00:25:43      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:printf   turn   std   case   etc   clear   任务   推箱子   char*   

1、

#include<stdio.h>
#include<stdlib.h>
#include<getch.h>

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 <stdio.h>
#include <stdlib.h>
#include <getch.h>

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+ox*2][y+oy*2] || 5 == map[x+ox*2][y+oy*2]))
{
map[x+ox*2][y+oy*2] += 4;
map[x+ox][y+oy] -= 2;
map[x][y] -= 2;
x += ox;
y += oy;
step++;
}
}
}

推箱子

标签:printf   turn   std   case   etc   clear   任务   推箱子   char*   

原文地址:https://www.cnblogs.com/Nxet/p/13296686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!