#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. sql server 转置 和实现随机分配和一串代码的含义拼在一行

    1.sql server 转置很容易搜到方法,一般需要手动写转置的列项,如果多时会比较烦,下面试了省事的方法: --案例需求数据 ----方法一:if object_id('tempdb.dbo.#s ...

  2. Intel P4 CPU

    1. P4 CPU 结构 奔4处理器是Intel的经典之作,它是采用乱序执行内核的超标量处理器.P4采用的微架构称为 Net Burst,基本结构如下: 奔4处理器微架构被分成了4大部分: (1)存储 ...

  3. deepin安装Oracle jdk8,以及添加add-apt-repository命令支持

    @font-face{ font-family:"Times New Roman"; } @font-face{ font-family:"宋体"; } p.M ...

  4. iframe无刷新跨域上传文件并获得返回值

    原文:http://geeksun.iteye.com/blog/1070607 需求:从S平台上传文件到R平台,上传成功后R平台返回给S平台一个值,S平台是在一个页面弹出的浮窗里上传文件,所以不能用 ...

  5. fibonacci数列的性质和实现方法

    fibonacci数列的性质和实现方法 1.gcd(fib(n),fib(m))=fib(gcd(n,m)) 证明:可以通过反证法先证fibonacci数列的任意相邻两项一定互素,然后可证n>m ...

  6. 转载 [深入学习C#]C#实现多线程的方式:使用Parallel类

    简介 在C#中实现多线程的另一个方式是使用Parallel类. 在.NET4中 ,另一个新增的抽象线程是Parallel类 .这个类定义了并行的for和foreach的 静态方法.在为 for和 fo ...

  7. mysql count与sum的区别

    一.count()的结果为>=0           sum()结果可能是null 二.count()计算的行数               sum()计算的是某列的求和

  8. PAT A1016 Phone Bills (25 分)——排序,时序

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  9. C# 语法一 构造函数

    1.构造函数的执行顺序 2.静态变量和成员变量的区别 代码示例: using System; using System.Collections.Generic; using System.Linq; ...

  10. java Switch中的case后面加上大括号({})和不加大括号的区别

    java基础求真之switch 的case 后面加上大括号和不加大括号的区别. 下面给出三段代码大家看一下有什么不同以及哪段代码能够编译通过那段代码编译不能通过,为什么?(Why?) 代码片段一: i ...