You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lw_kcsj/迷宫随机地图.cpp

81 lines
1.6 KiB

6 years ago
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 25 //<2F>Թ<EFBFBD><D4B9>ĸ߶ȣ<DFB6><C8A3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
#define Width 25 //<2F>Թ<EFBFBD><D4B9>Ŀ<EFBFBD><C4BF>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
#define Wall 1
#define Road 0
#define Start 2
#define End 3
int map[Height+2][Width+2];
void gotoxy(int x,int y) //<2F>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void create(int x,int y) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD>
{
int c[4][2]={0,1,1,0,0,-1,-1,0}; //<2F>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
int i,j,t;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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) //<2F><><EFBFBD>Թ<EFBFBD>
{
gotoxy(2*y-2,x-1);
switch(map[x][y])
{
case Start:
printf("<EFBFBD><EFBFBD>");break; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case End:
printf("<EFBFBD><EFBFBD>");break; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case Wall:
printf("<EFBFBD>~");break; //<2F><>ǽ
case Road:
printf(" ");break; //<2F><>·
}
}
int main()
{
system("title <20>Թ<EFBFBD><D4B9><EFBFBD>Ϸ");
int i,j;
srand((unsigned)time(NULL)); //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><E6BCB4><EFBFBD><EFBFBD>
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) //<2F><>ʼ<EFBFBD><CABC><EFBFBD>Թ<EFBFBD>
map[i][j]=Road;
else map[i][j]=Wall;
create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ʼ<E3BFAA><CABC><EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD><D4B9><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>ж<EFBFBD>Ϊż<CEAA><C5BC>
for(i=0;i<=Height+1;i++) //<2F>߽紦<DFBD><E7B4A6>
{
map[i][0]=Wall;
map[i][Width+1]=Wall;
}
for(j=0;j<=Width+1;j++) //<2F>߽紦<DFBD><E7B4A6>
{
map[0][j]=Wall;
map[Height+1][j]=Wall;
}
map[2][1]=Start; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
map[Height-1][Width]=End; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=1;i<=Height;i++)
for(j=1;j<=Width;j++) //<2F><><EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD>
paint(i,j);
return 0;
}