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 ...
随机推荐
- 探索哪个进程使磁盘I/O升高
如果生产环境中磁盘使用率突然升高,却不知道因为哪个应用程序导致的,这个时候我们可以使用pidstat命令来查看,比如 Linux .el7.x86_64 (ip.ec2.internal) _x86_ ...
- gitlab的使用(待书写)
1. [root@docker ~]# yum -y install git
- Parameter infoDTOs of type T from private T com.ListVO.setInfoDTOs is not resolvable to a concrete type.
WARN org.glassfish.jersey.internal.Errors - The following warnings have been detected: WARNING: Par ...
- ping 127.0.0.1和ping本地ip分别测试什么?
ping 127.0.0.1 是你本地的回环地址! 实际上只要是127.0.0.1到127.255.255.255都是回环地址!都是可以PING检查的! 它能ping通,说明你的TCP/IP协议栈没问 ...
- 网络编程_UDP协议_发送端与接收端
创建UDP传输的发送端 : 1.建立udp的socket服务 2.将要发送的数据封装到数据包中 3.通过udp的socket服务 将数据包发送出去 4.关闭socket服务(因为调用了系统 ...
- P1067 多项式输出
#include <iostream>#include<cstdio>#include<algorithm>using namespace std;int a[10 ...
- SALALchemy Session与scoped_session的源码分析
我们发现Session与scoped_session都有一些方法: 但是scoped_session的源码里面没有设置这些方法让我们从源码里去窥探下源码在哪里设置了这些方法: Session里面的方法 ...
- Django admin 的模仿流程
- python六十一课——高阶函数之reduce
2).reduce(fn,lsd): 参数一:fn --> 函数对象 参数二:lsd --> 序列对象 功能: 先将lsd中的第一和第二个元素去除传入到fn中参与运算, 运算后得到结果,再 ...
- ECS API
一.API调用方式 ➢对ECS API接口调用是通过向ECS API的服务端地址发送HTTP GET请求,并按照接口说明在请求 中加入相应请求参数来完成的;根据请求的处理情况,系统会返回处理结果. ➢ ...