当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Advanced multi threading: the underlying principle of synchronized, the process of lock optimization and lock upgrade

Custom JSON return data

冶金物理化学复习 --- 金属的电沉积,还原过程

Operation and use of collection framework

Idea uses dev tool to realize hot deployment

Sequence table OJ topic

链表中关于快慢指针的oj题

JUC notes

You must configure either the server or JDBC driver (via the ‘serverTimezone)

Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid
随机推荐
(黑马)MYSQL初级-高级笔记(博主懒狗)
Distillation model diagram
JUC notes
Openjudge: judge whether the string is palindrome
openjudge:找第一个只出现一次的字符
Video twins: the starting point of informatization upgrading of smart Parks
Lamda gets the current number of cycles, atomicinteger
蒸馏模型图
FusionGAN代码学习(一)
Pytorch uses hook to get feature map
Mysql database index (InnoDB engine)
冶金物理化学复习 -- 金属电沉积过程中的阴极极化,超电势以及阳极和阳极过程
Delete specific elements in order table OJ
Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid
对极大似然估计、梯度下降、线性回归、逻辑回归的理解
冶金物理化学复习 --- 化学反应动力学基础
Example of main diagram of paper model
A file upload tool website written by individuals
How to compare long and integer and why to report errors
ByteBuffer. Position throws exception illegalargumentexception