当前位置:网站首页>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;
}
边栏推荐
- 排序之插入排序
- Problems encountered when the registry service Eureka switches to nocas
- [singleton mode] thread safety of lazy mode
- 链表中关于快慢指针的oj题
- Deep learning medical image model reproduction
- openjudge:统计数字字符个数
- Methods of gflops and total params of pytorch calculation model
- Merge two ordered arrays of order table OJ
- ByteBuffer. Position throws exception illegalargumentexception
- When SQL queries the list, the data is inconsistent twice, and limit is automatically added
猜你喜欢

冶金物理化学复习 --- 复杂反应的速率方程

Custom JSON return data

Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid

Framework step by step easy-to-use process

Using Navicat or PLSQL to export CSV format, more than 15 digits will become 000 (e+19) later

softmax多分类 梯度推导

Video twins: the starting point of informatization upgrading of smart Parks

Multi module packaging: package: XXX does not exist

冶金物理化学复习 --- 冶金反应动力学基础与多相反应动力学特征

Review of Metallurgical Physical Chemistry - gas liquid phase reaction kinetics
随机推荐
Mutual conversion between latex and word
softmax多分类 梯度推导
repackag failed: Unable to find main class
Openjudge: maximum span of string
函数基础知识以及特殊点
When using deep learning training image, the image is too large for segmentation training prediction
Lamda gets the current number of cycles, atomicinteger
Mabtis (I) basic use of framework
MySQL uses list as a parameter to query
图像增强——MSRCR
Feignclient calls the get method and reports an error resultvo{result= unknown exception. Exception details: request method 'post' not supported
regular expression
Openjudge: matrix multiplication
冶金物理化学复习 --- 液 - 液相反应动力学
Oracle view lock table statement and unlocking method
Openjudge: patient queuing
论文模型主图范例
Edge calculation kubeedge+edgemash
顺序表oj之合并两个有序数组
BigDecimal rounds and retains two decimal places