#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 25 //迷宫的高度,必须为奇数
#define Width 25 //迷宫的宽度,必须为奇数
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4 int map[Height+][Width+];
void gotoxy(int x,int y) //移动坐标
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}
void create(int x,int y) //随机生成迷宫
{
int c[][]={,,,,,-,-,}; //四个方向
int i,j,t;
//将方向打乱
for(i=;i<;i++)
{
j=rand()%;
t=c[i][];c[i][]=c[j][];c[j][]=t;
t=c[i][];c[i][]=c[j][];c[j][]=t;
}
map[x][y]=Road;
for(i=;i<;i++)
if(map[x+*c[i][]][y+*c[i][]]==Wall)
{
map[x+c[i][]][y+c[i][]]=Road;
create(x+*c[i][],y+*c[i][]);
}
}
int get_key() //接收按键
{
char c;
while(c=getch())
{
if(c==) return Esc; //Esc
if(c!=-)continue;
c=getch();
if(c==) return Up; //上
if(c==) return Down; //下
if(c==) return Left; //左
if(c==) return Right; //右
}
return ;
}
void paint(int x,int y) //画迷宫
{
gotoxy(*y-,x-);
switch(map[x][y])
{
case Start:
printf("入");break; //画入口
case End:
printf("出");break; //画出口
case Wall:
printf("▇");break; //画墙
case Road:
printf(" ");break; //画路
}
}
void game()
{
int x=,y=; //玩家当前位置,刚开始在入口处
int c; //用来接收按键
while()
{
gotoxy(*y-,x-);
printf("●"); //画出玩家当前位置
if(map[x][y]==End) //判断是否到达出口
{
gotoxy(,);
printf("到达终点,按任意键结束");
getch();
break;
}
c=get_key();
if(c==Esc)
{
gotoxy(,);
break;
}
switch(c)
{
case Up: //向上走
if(map[x-][y]!=Wall)
{
paint(x,y);
x--;
}
break;
case Down: //向下走
if(map[x+][y]!=Wall)
{
paint(x,y);
x++;
}
break;
case Left: //向左走
if(map[x][y-]!=Wall)
{
paint(x,y);
y--;
}
break;
case Right: //向右走
if(map[x][y+]!=Wall)
{
paint(x,y);
y++;
}
break;
}
}
}
int main()
{
system("title yourname");
int i,j;
srand((unsigned)time(NULL)); //初始化随即种子
hidden(); //隐藏光标
for(i=;i<=Height+;i++)
for(j=;j<=Width+;j++)
if(i==||i==Height+||j==||j==Width+) //初始化迷宫
map[i][j]=Road;
else map[i][j]=Wall; create(*(rand()%(Height/)+),*(rand()%(Width/)+)); //从随机一个点开始生成迷宫,该点行列都为偶数
for(i=;i<=Height+;i++) //边界处理
{
map[i][]=Wall;
map[i][Width+]=Wall;
} for(j=;j<=Width+;j++) //边界处理
{
map[][j]=Wall;
map[Height+][j]=Wall;
}
map[][]=Start; //给定入口
map[Height-][Width]=End; //给定出口
for(i=;i<=Height;i++)
for(j=;j<=Width;j++) //画出迷宫
paint(i,j);
game(); //开始游戏
getch();
return ;
}

C_数据结构_走迷宫的更多相关文章

  1. c_数据结构_图_邻接表

    课程设计------邻接表 图的遍历实现课程设计:https://files.cnblogs.com/files/Vera-y/图的遍历_课程设计.zip #include<stdio.h> ...

  2. c_ 数据结构_图_邻接矩阵

    程序主要实现了图的深度遍历和广度遍历. #include <stdio.h> #include <stdlib.h> #include <string.h> #de ...

  3. C_数据结构_链表的链式实现

    传统的链表不能实现数据和链表的分离,一旦数据改变则链表就不能用了,就要重新开发. 如上说示:外层是Teacher,里面小的是node. #ifndef _MYLINKLIST_H_ #define _ ...

  4. c_数据结构_队的实现

    # 链式存储#include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100//存储空间初始分配量 #defin ...

  5. c_数据结构_栈的实现

    #include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT ...

  6. c_数据结构_链表

    #include<stdio.h> #include<stdlib.h> #define ERROR 0 #define OK 1 #define OVERFLOW -2 ty ...

  7. c_数据结构_顺序表

    #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define LIST_INIT_SIZE 100 // 线性表存储空间的初始分配量 #define ...

  8. C_数据结构_快速排序

    # include <stdio.h> void QuickSort(int * a, int low, int high); int FindPos(int * a, int low, ...

  9. C_数据结构_链式二叉树

    # include <stdio.h> # include <malloc.h> struct BTNode { int data; struct BTNode * pLchi ...

随机推荐

  1. ALSA声卡驱动的DAPM(一)-DPAM详解

    最近使用tinymix 调试相应的音频通道,但是一直不知道音频通道的原理是什么.所以百度了一下,百度结果是与DPAM有关. 一.DAPM简介: DAPM是Dynamic Audio Power Man ...

  2. CentOS7:搭建配置SVN服务器

    1. 安装 CentOS通过yum安装subversion. $ sudo yum install subversion subversion安装在/bin目录: $ which svnserve / ...

  3. linux 下正则匹配时间命名格式的文件夹

    用正则表达式匹配时间格式命名的文件夹 ls mypath | grep -E "[0-9]{4}-[0-9]{1,2}" mypath为需要查询的目录 查询出来的文件夹格式为:例 ...

  4. C# 动态方法和静态方法的区别

    C# 动态方法和静态方法的区别 (转) 动态方法与静态方法的区别: 1,使用方法上的区别:动态方法,在使用时需要先创建实例,才能调用实例方法,而静态方法则不需要,直接使用即可. 示例代码如下:静态方法 ...

  5. UUChart的使用

    一.简介 UUChart是一个用于绘制图表的第三方,尤其适合去绘制折线图.自己再做一个医院相关的项目时,需要对一周内的血压进行监控,需要绘制折线图来表示出高压.低压的走向,因此学习了一下. 二.下载地 ...

  6. Flex布局新写法兼容写法详解

    很久之前用过flex,但是没有考虑过兼容性问题,为了兼容ios一定要加上-webkit前缀: ul{ display: flex; /* 新版本语法: Opera 12.1, Firefox 22+ ...

  7. idea报错:[2016-08-31 09:20:10,763] Artifact xxx:war exploded: Error during artifact deployment.

    [2016-08-31 09:20:10,763] Artifact newClassProject1:war exploded: Error during artifact deployment. ...

  8. Choosing number ZOJ - 3690 (矩阵快速幂)

    题意:n个人站成一排,每个人任意从1——m中任意取一个数,要求相邻两个人的如果数字相同,数字要大于k. 分划思想推导表达式: 假设  i  个人时.第i个人的选择有两种一种是选择小于等于k的数,另一种 ...

  9. YOLO2(1)配置安装win10+openvc2413+VS2013 简单测试官例

    参考官网 https://github.com/AlexeyAB/darknet#how-to-compile-on-windows https://github.com/AlexeyAB/darkn ...

  10. jenkins安装Scanner插件

    环境centos7 第一步安装scaner插件 第二步 重启之后配置sonarqube 进入Jenkins-->系统管理-->系统设置,找到sonarqube servers,填写相关信息 ...