久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
用C語言寫的走迷宮的代碼
[打印本頁]
作者:
gzkgzk
時間:
2019-8-10 11:15
標題:
用C語言寫的走迷宮的代碼
//走迷宮 普通走法
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 25 //迷宮的高度,必須為奇數(shù)
#define Width 25 //迷宮的寬度,必須為奇數(shù)
#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+2][Width+2];
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=0;//賦1為顯示,賦0為隱藏
SetConsoleCursorInfo(hOut,&cci);
}
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]);
}
}
int get_key() //接收按鍵
{
char c;
while(c=getch())
{
if(c==27) return Esc; //Esc
if(c!=-32)continue;
c=getch();
if(c==72) return Up; //上
if(c==80) return Down; //下
if(c==75) return Left; //左
if(c==77) return Right; //右
}
return 0;
}
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; //畫路
}
}
void game()
{
int x=2,y=1; //玩家當前位置,剛開始在入口處
int c; //用來接收按鍵
while(1)
{
gotoxy(2*y-2,x-1);
printf("●"); //畫出玩家當前位置 這里畫出光標的位置也就是個圈圈
if(map[x][y]==End) //判斷是否到達出口
{
gotoxy(30,24);
printf("到達終點,按任意鍵結(jié)束");
getch();
break;
}
c=get_key();
if(c==Esc)
{
gotoxy(0,24);
break;
}
switch(c)
{
case Up: //向上走
if(map[x-1][y]!=Wall)
{
paint(x,y);
x--;
}
break;
case Down: //向下走
if(map[x+1][y]!=Wall)
{
paint(x,y);
x++;
}
break;
case Left: //向左走
if(map[x][y-1]!=Wall)
{
paint(x,y);
y--;
}
break;
case Right: //向右走
if(map[x][y+1]!=Wall)
{
paint(x,y);
y++;
}
break;
}
}
}
int main()
{
system("title yourname");
int i,j;
srand((unsigned)time(NULL)); //初始化隨即種子
hidden(); //隱藏光標
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)); //從隨機一個點開始生成迷宮,該點行列都為偶數(shù)
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);
game(); //開始游戲
getch();
return 0;
}
復(fù)制代碼
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
看片91
|
午夜欧美
|
亚洲乱码一区二区三区在线观看
|
午夜综合
|
国产一区二区三区免费
|
国产一区二区三区四区hd
|
亚洲成人免费网址
|
天天综合成人网
|
国产久
|
羞羞色影院
|
久久综合狠狠综合久久综合88
|
亚洲另类春色偷拍在线观看
|
97精品超碰一区二区三区
|
在线中文字幕视频
|
欧美久久电影
|
日日操网站
|
精产国产伦理一二三区
|
国产特黄一级
|
日本大香伊一区二区三区
|
午夜视频网
|
亚洲视频在线看
|
午夜免费在线电影
|
免费观看黄a一级视频
|
性色av香蕉一区二区
|
精品欧美一区二区三区久久久
|
久久精品免费一区二区
|
亚洲国产精品视频
|
成人在线精品视频
|
免费看91
|
999国产精品视频免费
|
国产乱码精品一区二区三区忘忧草
|
欧美日韩不卡
|
99久久精品一区二区毛片吞精
|
91精品国产91久久综合桃花
|
亚欧午夜
|
亚洲一区二区精品视频
|
日本涩涩网
|
日韩网站免费观看
|
久久婷婷av
|
免费a v网站
|
久久国产麻豆
|