parent
ce88d0c387
commit
bceeed60e2
@ -1,80 +0,0 @@
|
||||
|
||||
#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
|
||||
int map[Height+2][Width+2];
|
||||
void gotoxy(int x,int y) //移动坐标
|
||||
{
|
||||
COORD coord;
|
||||
coord.X=x;
|
||||
coord.Y=y;
|
||||
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
|
||||
}
|
||||
void create(int x,int y) //随机生成迷宫
|
||||
{
|
||||
int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向
|
||||
int i,j,t;//将方向打乱
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
j=rand()%4;
|
||||
t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;
|
||||
t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;
|
||||
}
|
||||
map[x][y]=Road;
|
||||
for(i=0;i<4;i++)
|
||||
if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall)
|
||||
{
|
||||
map[x+c[i][0]][y+c[i][1]]=Road;
|
||||
create(x+2*c[i][0],y+2*c[i][1]);
|
||||
}
|
||||
}
|
||||
void paint(int x,int y) //画迷宫
|
||||
{
|
||||
gotoxy(2*y-2,x-1);
|
||||
switch(map[x][y])
|
||||
{
|
||||
case Start:
|
||||
printf("入");break; //画入口
|
||||
case End:
|
||||
printf("出");break; //画出口
|
||||
case Wall:
|
||||
printf("▇");break; //画墙
|
||||
case Road:
|
||||
printf(" ");break; //画路
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
system("title 迷宫游戏");
|
||||
int i,j;
|
||||
srand((unsigned)time(NULL)); //初始化随即种子
|
||||
for(i=0;i<=Height+1;i++)
|
||||
for(j=0;j<=Width+1;j++)
|
||||
if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫
|
||||
map[i][j]=Road;
|
||||
else map[i][j]=Wall;
|
||||
create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数
|
||||
for(i=0;i<=Height+1;i++) //边界处理
|
||||
{
|
||||
map[i][0]=Wall;
|
||||
map[i][Width+1]=Wall;
|
||||
}
|
||||
for(j=0;j<=Width+1;j++) //边界处理
|
||||
{
|
||||
map[0][j]=Wall;
|
||||
map[Height+1][j]=Wall;
|
||||
}
|
||||
map[2][1]=Start; //给定入口
|
||||
map[Height-1][Width]=End; //给定出口
|
||||
for(i=1;i<=Height;i++)
|
||||
for(j=1;j<=Width;j++) //画出迷宫
|
||||
paint(i,j);
|
||||
return 0;
|
||||
}
|
Binary file not shown.
Loading…
Reference in new issue