当前位置:网站首页>C语言走迷宫
C语言走迷宫
2022-07-28 05:18:00 【c7473168】
走迷宫:
数据分析:
1.定义二维字符数组作为迷宫地图
2.定义变量记录角色的位置 x y
3.时间:time(NULL) 获取1970-1-1到运行时过了总秒数
逻辑分析:
进入死循环:
1.显示地图
2.获取方向键并处理
判断前方是不是路' '
如果是:
1.把旧位置变成' '
2.把新位置变成'@'
3.更新角色位置坐标x y
3.判断是否到达出口
如果是:程序结束
#include <stdio.h>
#include <getch.h>
#include <stdlib.h>
#include <time.h>
int main(int argc,const char* argv[])
{
char maze[10][10] = {
{'*','*','*','*','*','*','*','*','*','*'},
{'*','@','*',' ',' ',' ','*','*','*','*'},
{'*',' ','*',' ','*',' ','*',' ',' ','*'},
{'*',' ','*',' ','*',' ','*',' ','*','*'},
{'*',' ','*',' ','*',' ',' ',' ','*','*'},
{'*',' ','*',' ','*','*','*','*','*','*'},
{'*',' ',' ',' ','*',' ',' ',' ',' ','*'},
{'*','*',' ','*','*',' ','*','*',' ','*'},
{'*','*',' ',' ',' ',' ','*','*',' ','*'},
{'*','*','*','*','*','*','*','*',' ','*'},
};
char row = 1,col = 1; //记录角色位置
time_t start_time=time(NULL);
int i,j;
for(;;)
{
system("clear"); //清理屏幕
for(i=0;i<10;i++) //显示地图
{
for(j=0;j<10;j++)
{
printf("%c ",maze[i][j]);
}
printf("\n");
}
// 判断是否到达出口
if(9 == row && 8 == col)
{
printf("游戏胜利!过了%lu秒\n",time(NULL)-start_time);
break;
}
switch(getch()) // 获取方向键
{
case 183: //上
if(' ' == maze[row-1][col])
{
maze[row][col]=' ';
maze[--row][col]='@';
}
break;
case 184: //下
if(' ' == maze[row+1][col])
{
maze[row][col]=' ';
maze[++row][col]='@';
}
break;
case 185: //右
if(' ' == maze[row][col+1])
{
maze[row][col]=' ';
maze[row][++col]='@';
}
break;
case 186: //左
if(' ' == maze[row][col-1])
{
maze[row][col]=' ';
maze[row][--col]='@';
}
break;
}
}
return 0;
}
边栏推荐
猜你喜欢

softmax多分类 梯度推导

环形链表问题

The Monte Carlo method solves the PI and draws points with turtle, and completes the progress bar problem

冶金物理化学复习 ---- 气固反应动力学

正则表达式

Review of metallurgical physical chemistry ---- gas solid reaction kinetics

Personal summary of restful interface use

repackag failed: Unable to find main class

蒸馏模型图

框架一步一步方便使用的流程
随机推荐
openjudge:找第一个只出现一次的字符
Multi module packaging: package: XXX does not exist
Advanced multi threading: the underlying principle of synchronized, the process of lock optimization and lock upgrade
Advanced multithreading: Lock strategy
JUC notes
排序之插入排序
Openjudge: stone scissors cloth
How Visio accurately controls the size, position and angle of graphics
About localdatetime in swagger
函数基础知识以及特殊点
Methods of gflops and total params of pytorch calculation model
Learning of image enhancement evaluation index -- structural similarity SSIM
蒙特卡罗方法求解圆周率π并用turtle画点,以及完成进度条问题
关于swagger中的localDateTime
Lamda gets the current number of cycles, atomicinteger
(dark horse) MySQL beginner advanced notes (blogger lazy dog)
正则表达式
Image enhancement - msrcr
Fusiongan code learning (I)
Mutual conversion between latex and word