当前位置:网站首页>C语言推箱子
C语言推箱子
2022-07-28 05:18:00 【c7473168】
推箱子:数据分析:
1.确定数值与字符的对应关系
0 ' '
1 '@'
2 '$'
3 '#'
4 'o'
5 '@'
7 '$'
2.定义8*8整数地图并初始化
3.定义记录角色位置的变量x y
4.定义记录步数的变量
逻辑分析:进入死循环
1.清理屏幕,显示地图
if(0 == map[i][j]) printf(" ");
2.获取方向键并处理
3.判断是否游戏胜利
前方是路,参考走迷宫
前方+1
原位置-1
更新坐标
2.前方是箱子
箱子的前方是路\目标点
人前方的前方 +3
人前方 -3+1
人原位置 -1
'更新左边
#include <stdio.h>
#include <stdlib.h>
#include <getch.h>
int main(int argc,const char* argv[])
{
char map[8][8] = {
{' ',' ','*','*','*','*',' ',' '},
{' ',' ','*','x','x','*',' ',' '},
{' ','*','*',' ','x','*','*',' '},
{' ','*',' ',' ','o','x','*',' '},
{'*','*',' ','o',' ',' ','*','*'},
{'*',' ',' ','*','o','o',' ','*'},
{'*',' ',' ','w',' ',' ',' ','*'},
{'*','*','*','*','*','*','*','*'},
};
int i,j,cnt=0;
char x=6,y=3;
for(;;)
{
if(' '==map[1][3])
map[1][3]='x';
if(' '==map[1][4])
map[1][4]='x';
if(' '==map[2][4])
map[2][4]='x';
if(' '==map[3][5])
map[3][5]='x';
system("clear");
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%c ",map[i][j]);
}
printf("\n");
}
if('o'==map[1][3]&&'o'==map[1][4]&&'o'==map[2][4]&&'o'==map[3][5])
{
printf("你赢啦!共用%d步\n",cnt);
break;
}
int key = getch();
if(183 == key && '*'!=map[x-1][y] && 'o'!=map[x-1][y])
{
map[x][y]=' ';
map[x-1][y]='w';
x-=1;cnt++;
}
else if(183==key&&'*'!=map[x-2][y]&&'o'==map[x-1][y]&&'o'!=map[x-2][y])
{
map[x][y]=' ';
map[x-2][y]='o';
map[x-1][y]='w';
x-=1;cnt++;
}
if(184 == key && '*'!=map[x+1][y] && 'o'!=map[x+1][y])
{
map[x][y]=' ';
map[x+1][y]='w';
x+=1;cnt++;
}
else if(184==key&&'*'!=map[x+2][y]&&'o'==map[x+1][y]&&'o'!=map[x+2][y])
{
map[x][y]=' ';
map[x+2][y]='o';
map[x+1][y]='w';
x+=1;cnt++;
}
if(185 == key && '*'!=map[x][y+1] && 'o'!=map[x][y+1])
{
map[x][y]=' ';
map[x][y+1]='w';
y+=1;cnt++;
}
else if(185==key&&'*'!=map[x][y+2]&&'o'==map[x][y+1]&&'o'!=map[x][y+2])
{
map[x][y]=' ';
map[x][y+2]='o';
map[x][y+1]='w';
y+=1;cnt++;
}
if(186 == key && '*'!=map[x][y-1] && 'o'!=map[x][y-1])
{
map[x][y]=' ';
map[x][y-1]='w';
y-=1;cnt++;
}
else if(186==key&&'*'!=map[x][y-2]&&'o'==map[x][y-1]&&'o'!=map[x][y-2])
{
map[x][y]=' ';
map[x][y-2]='o';
map[x][y-1]='w';
y-=1;cnt++;
}
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
Openjudge: upper and lower case letters are interchanged
Lamda gets the current number of cycles, atomicinteger
Simpledateformat thread unsafe and datetimeformatter thread safe
Merge two ordered arrays of order table OJ
The difference between get and post
RESNET structure comparison
Openjudge: maximum span of string
ECCV22 最新54篇论文主图整理
深度学习热力图可视化的方式
Advanced multithreading: Lock strategy
restFul接口使用个人总结
Pytorch uses hook to get feature map
How to compare long and integer and why to report errors
You must configure either the server or JDBC driver (via the ‘serverTimezone)
Video twins: the starting point of informatization upgrading of smart Parks
图像增强——MSRCR
ByteBuffer.position 抛出异常 IllegalArgumentException
IO流的使用
论文写作用词
openjudge:过滤多余的空格








