当前位置:网站首页>C语言做一个小迷宫
C语言做一个小迷宫
2022-07-27 05:03:00 【我真不是二次元】
走迷宫:
数据分析:
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>
time_t t1=0,t2=0;
int main(int argc,const char* argv[])
{
char maze[10][10]={
{'#','#','#','#','#','#','#','#','#','#'},
{'#','#',' ','#','#','#','#','#','#','#'},
{'#','#',' ','#','#','#','#','#','#','#'},
{'#',' ',' ',' ',' ','#','#','#','#','#'},
{'#',' ','#','#',' ','#','#','#','#','#'},
{'#',' ','#','#',' ','#','#','#','#','#'},
{'#',' ','#','#',' ',' ',' ',' ',' ',' '},
{'#',' ','@','#','#','#','#','#','#','#'},
{'#','#','#','#','#','#','#','#','#','#'},
{'#','#','#','#','#','#','#','#','#','#'},
};
//记录游戏开始时间
t1=time(NULL);
//记录角色的位置坐标
char man_x=7,man_y=2;
for(;;)
{
//清理屏幕
system("clear");
//显示地图
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
printf("%c ",maze[i][j]);
}
printf("\n");
}
//获取方向键
switch(getch())
{
case 183: //向上
if(' '== maze[man_x-1][man_y])
{
maze[man_x][man_y]=' ';
maze[--man_x][man_y]='@';
}
break;
case 184: //向下
if(' '== maze[man_x+1][man_y])
{
maze[man_x][man_y]=' ';
maze[++man_x][man_y]='@';
}
break;
case 185: //右
if(' '== maze[man_x][man_y+1])
{
maze[man_x][man_y]=' ';
maze[man_x][++man_y]='@';
}
break;
case 186: //左
if(' '== maze[man_x][man_y-1])
{
maze[man_x][man_y]=' ';
}
break;
}
//判断是否到达出口
if(man_x==6 && man_y==9)
{
t2=time(NULL);
system("clear");
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
printf("%c ",maze[i][j]);
}
printf("\n");
}break;
}
}
printf("游戏胜利!总共用时%lus\n",t2-t1);
}
边栏推荐
猜你喜欢

李宏毅机器学习组队学习打卡活动day04---深度学习介绍和反向传播机制

Rolling Division

2021 Niuke multi school training camp 5 (question b)

小米商城项目_注册

Bean's life cycle & dependency injection * dependency auto assembly

接收方设置并发量和限流

消息可靠性处理

Flask框架创建项目初期总结

Li Hongyi machine learning team learning punch in activity day05 --- skills of network design

DNSmasq使用总结
随机推荐
Three waiting methods of selenium and three processing methods of alert pop-up
后台优惠券管理
Differences among bio, NiO and AIO
用户的管理-限制
用户登录-以及创建、验证短信验证码
Selenium element operation
LeetCode之6 ZigZag Conversion
GCC 编译选项
【codeforces 1695C Zero Path】DP
The concept of cloud native application and 15 characteristics of cloud native application
Carmaker quick start lesson 4 developing 48V P1 hybrid system
后台用户管理展示添加功能实现
Hi3516dv300 environment setup
B1023 group minimum
[CSAPP] Application of bit vectors | encoding and byte ordering
redis持久化
小米商城项目_注册
Li Hongyi machine learning team learning punch in activity day04 - Introduction to deep learning and back propagation mechanism
Enumeration class implements singleton mode
[codeworks round 801 div2 D tree queries] tree greedy conclusion