走迷宫小游戏

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 45
#define Width 45
#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;
	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;
		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("A");
		if(map[x][y]==End)
		{
			gotoxy(30,24);
			printf("\n");
			printf("到达终点,按任意键结束");
			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() {
	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));
	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;
}

#include<iostream> #include<windows.h> #include "conio.h" using namespace std; const int m=10 ; //迷宫行数 const int n=15 ; //迷宫列数 struct migong //结构体迷宫,,,pre为前驱,,,xy 为当前目标 { int x,y; int pre; }sq[500]; //最大成员500个,,迷宫大小,,,,, int mg[m+2][n+2]; //迷宫数组 外带边2个 ,, int zx[8+1],zy[8+1]; //迷宫方向数组 九宫格,,,他在中间,,,所以8个方向 void printlj(int rear) //打印迷宫路径 { int i; i=rear; //保存当前节点前驱,,为了在,,向前试探失败时,返回,,换一个方向继续 do { cout<<sq[i].x<<sq[i].y; //输出当前,,X,y值,,其实就是地址。。。。 i=sq[i].pre; // 保存这个节点的前驱 }while(i!=0); //i!=0表示通路 } void mglj() //球迷宫最短路径 { int i,j,x,y,v,front,rear,found; sq[1].x=sq[1].y=1;sq[1].pre=0; //从(1,1)开始搜索 。。。。左上角开始 found=0; //初始化。 front=rear=1; //初始化 mg[1][1]=-1; //开始节点就不需要来回遍历了,,,不设为-1,,一样可以算,,对于CPU来说无所谓 while((front<=rear)&&(!found)) //found初始化,,等于0 ,,,,这里是一个BUG,,,因为找到了,,他就退出了,,,很多时候不止一条路径 { x=sq[front].x; // y=sq[front].y; //引入临时变量x,y,,,保存当前,,,因为下面要进行,,,查找遍历,,,为了能够回到当前节点,,在无法试探的情况下 for(v=1;v<=8;v++) //循环扫描8个方向 {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值