C_数据结构_走迷宫
#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_数据结构_走迷宫的更多相关文章
- c_数据结构_图_邻接表
课程设计------邻接表 图的遍历实现课程设计:https://files.cnblogs.com/files/Vera-y/图的遍历_课程设计.zip #include<stdio.h> ...
- c_ 数据结构_图_邻接矩阵
程序主要实现了图的深度遍历和广度遍历. #include <stdio.h> #include <stdlib.h> #include <string.h> #de ...
- C_数据结构_链表的链式实现
传统的链表不能实现数据和链表的分离,一旦数据改变则链表就不能用了,就要重新开发. 如上说示:外层是Teacher,里面小的是node. #ifndef _MYLINKLIST_H_ #define _ ...
- c_数据结构_队的实现
# 链式存储#include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100//存储空间初始分配量 #defin ...
- c_数据结构_栈的实现
#include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT ...
- c_数据结构_链表
#include<stdio.h> #include<stdlib.h> #define ERROR 0 #define OK 1 #define OVERFLOW -2 ty ...
- c_数据结构_顺序表
#define OK 1 #define ERROR 0 #define OVERFLOW -2 #define LIST_INIT_SIZE 100 // 线性表存储空间的初始分配量 #define ...
- C_数据结构_快速排序
# include <stdio.h> void QuickSort(int * a, int low, int high); int FindPos(int * a, int low, ...
- C_数据结构_链式二叉树
# include <stdio.h> # include <malloc.h> struct BTNode { int data; struct BTNode * pLchi ...
随机推荐
- 实验吧web题(26/26)全writeup!超详细:)
#简单的SQL注入 http://www.shiyanbar.com/ctf/1875 1)试着在?id=1,没有错误 2)试着?id=1',出错了,有回显,说明有注入点: You have an e ...
- tkinter内嵌Matplotlib系列(二)之函数曲线绘制
目录 目录 前言 (一)对matplotlib画布的封装: (二)思路分析: 1.需求说明: 2.框架的设置: 3.文件说明: (三)各文件的源代码 1.main.py 2.widget.py 3.f ...
- Intent加强
Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...
- C#常见委托のdelegate定义,Func,Action,Predicate总结
委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板:(交流可以加qq群:435226676) 委托常见的方式有一般委托显示定义,Func<T,TResult> (T, ...
- C#的static
1.static意思是静态,可以修饰类.字段.属性.方法2.标记为static的就不用创建实例对象调用了,可以通过类名直接点出来3.static三种用法:4.用于变量前,表示每次重新使用该变量所在方法 ...
- 安装docker后访问外网失败
服务器网段是(172.17)与docker默认桥接的网桥号段相同 docker version 17.03.2-ce 1.centOS6 原文链接 http://www.bubuko.com/info ...
- 个人css编写规范
前言:最近在做微信小程序,因为公司小,就我一个人弄前端的东西,js和页面都是我来弄,结果那天后台的人看到我的js代码,说我的代码写得不规范,函数什么的都很乱,弄得我羞愧难当,幸亏没看我的css,其实我 ...
- 简单的CSS圆形缩放动画
简单的CSS圆形缩放动画 话不多说鼠标移动上去,看效果吧,效果预览 代码如下: <!DOCTYPE html> <html> <head> <title> ...
- 分布式计算(四)Azkaban安装
Azkaban是一个批量工作流任务调度器,使用Java语言开发.用于在一个工作流内以一个特定的顺序运行一组工作和流程.Azkaban使用job配置文件建立任务之间的依赖关系,并提供一个易于使用的web ...
- JavaScript设计模式 - 状态模式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...