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 ...
随机推荐
- 字典Key值为变量
m='aaa4a' d = dict(name=m) print d['name']
- 1. svg学习笔记-在网页中使用svg
在网页中使用svg有以下三种方式 1. svg归根结底来说是一种图像格式,虽然有别于jpeg,gif,png等位图图像格式,所以在网页中能嵌入图像的地方都可以嵌入svg,例如将svg文件设置为< ...
- myeclipse编写servlet
1.File--New--Other.搜索web--Dynamic Web Project--Next,Project name--Next,Next--web应用的根目录和web资源存放的目录--- ...
- rpm安装时出现循环依赖
在安装git包时提示要安装perl-git,当安装perl-git时又提示要安装git包.报错如下: [root@racdb1 Packages]# rpm -ivh perl-Git-1.7.1-4 ...
- Appium1.9.1 部署及结果检验
1.官网下载最新的 appium 2.点击 Download Appium 3.选择适用于自己操作系统的版本,我的是 windows版本,就选择如下红圈起的 4.点击安装,一直点 下一步 直到提示安装 ...
- Sketch网页截屏插件设计开发
1.需求 在Sketch的Artboard中插入网页截图: 1.1.输入网址,自动截图到Artboard中,并居中显示: 1.2.可截取网页局部图片 2.技术选型 技术的选型主要是针对截图功能的选型, ...
- C# X509Certificate类 调用证书
一.命名空间 using System.Security.Cryptography.X509Certificates; 二.调用代码 string certPath = Server.MapPath( ...
- centos7下安装docker(13.3volume生命周期管理)
本章讨论:volume的备份,恢复,迁移和销毁 1.备份 通过前面的大量的实验,我们知道volume 是依赖host存在的,是host中的文件或目录,所以volume 的备份实际是对文件系统的备份. ...
- jvm虚拟机分享课笔记
深入理解jvm虚拟机分享 1. jvm执行流程 java-编译-.class—类加载器(随时随地加载)--[进入java虚拟机] 执行引擎—本地方法接口---本地方法库 运行时数据区 2. 运行时数据 ...
- python3打包成exe---pyinstaller方法
前言: 主要介绍python3的pyinstaller打包方法 pyinstaller安装参考地址:http://www.pyinstaller.org/ pywin32的下载地址:https://s ...