走迷宫函数_v0.2

// maze_v0.2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>

using namespace std;

#define WIDTH 10
#define LENGTH 20

//函数声明//
void draw(void);
void moveUp(void);
void moveDown(void);
void moveLeft(void);
void moveRight(void);
void change(void);

int map[WIDTH][LENGTH] =
{
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 2, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1},
	{1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1},
	{1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1},
	{1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1},
	{1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
	{1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
	{1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 3, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

int x = 1;//初始x坐标
int y = 1;//初始y坐标
int xx = 0;
int yy = 0;
bool victory = false;

int main()
{
	char select;
	draw();

	while (!victory)
	{
		select = _getch();

		switch(select)
		{
		case 'w':
			moveUp();
			change();
			system("cls");
			draw();
			break;
		case 's':
			moveDown();
			change();
			system("cls");
			draw();
			break;
		case 'a':
			moveLeft();
			change();
			system("cls");
			draw();
			break;
		case 'd':
			moveRight();
			change();
			system("cls");
			draw();
			break;
		}
	}

	cout << "win!\n";

	return 0;
}

void draw()//draw a map//
{
	int val_1 = 0;
	int val_2 = 0;

	for (val_1=0; val_1<WIDTH; ++val_1)//<>--------------遍历数组//
	{
		for (val_2=0; val_2<LENGTH; ++val_2)
		{
			if (1 == map[val_1][val_2])//<>-----------------画墙//
			{
				cout << "# ";
			}
			else if (0 == map[val_1][val_2])//<>-----------------画路//
			{
				cout << "  ";
			}
			else if (2 == map[val_1][val_2])//<>-----------------画人//
			{
				cout << "·";
			}
			else if (3 == map[val_1][val_2])//<>-----------------画出口//
			{
				cout << "* ";
			}
		}
		cout << endl;
	}
}

void moveUp()//上移//
{
	if (1 != map[x-1][y])
	{
		xx = x;
		yy = y;
		x = x - 1;
	}
}

void moveDown()//下移//
{
	if (1 != map[x+1][y])
	{
		xx = x;
		yy = y;
		x = x + 1;
	}
}

void moveLeft()//左移//
{
	if (1 != map[x][y-1])
	{
		xx = x;
		yy = y;
		y = y - 1;
	}
}

void moveRight()//右移//
{
	if (1 != map[x][y+1])
	{
		xx = x;
		yy = y;
		y = y + 1;
	}
}

void change()
{
	if (3 == map[x][y])
	{
		victory = true;
	}
	map[x][y] = 2;
	map[xx][yy] = 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
#include <stdio.h> #include <stdlib.h> #include <graphics.h> #include <time.h> #include <conio.h> #define MAXSIZE 20 typedef struct { int x; int y; }PosType; typedef struct { int di1; int di2; int di3; int di4; }Direct; typedef int MazeElem; typedef struct { MazeElem color[MAXSIZE+2][MAXSIZE+2]; PosType start; PosType end; }MazeType; MazeType mazes; int unit,rows,columns,ratio; PosType offset; PosType NextPos( PosType curpos,int di) { switch(di) {case 1: curpos.y++;break; /*右*/ case 2: curpos.x++;break; /*下*/ case 3: curpos.y--;break; /*左*/ case 4: curpos.x--;break;/*上*/ } return curpos; } void Near(PosType start,PosType end) { int i=0; PosType curpos; while(++i<=4) { curpos=NextPos(start,i); if(curpos.x==end.x && curpos.y==end.y) { printf(" success!\n"); getch(); closegraph(); exit(0); } } } void direction(Direct *direct,PosType start,PosType end) { if(start.y<=end.y) { if(start.x<=end.x) { if(end.y-start.y<=end.x-start.x) { direct->di1=2; /*左*/ direct->di2=3; /*下*/ direct->di3=4; /*上*/ direct->di4=1; /*右*/ } else { direct->di1=1; direct->di2=2; direct->di3=3; direct->di4=4; } } else { if(end.y-start.y<=start.x-end.x) { direct->di1=4; /*上 */ direct->di2=1; /*右*/ direct->di3=3; /*左 */ direct->di4=2; /*下*/ } else { direct->di1=1; direct->di2=4; direct->di3=3; direct->di4=2; } } } else { if(start.x<=end.x) { if(start.y-end.y<=end.x-start.x) { direct->di1=2; direct->di2=3; direct->di3=1; direct->di4=4; } else { direct->di1=3; direct->di2=2; direct->di3=4; direct->di4=1; } } else { if(start.y-end.y<=start.x-end.x) { direct->di1=4; direct->di2=3; direct->di3=1; direct->di4=2; } else { direct->di1=3; direct->di2=4; direct->di3=2; direct->di4=1; } } } } void Init(void)/*图形初始化*/ { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc"); cleardevice(); } void CreateMaze() { int i,j; char m ; int gdriver, gmode; detectgraph(&gdriver, &gmode); registerbgidriver(EGAVGA_driver); clrscr(); setbkcolor(2); settextstyle(0,0,2); outtextxy(30,60,"《**This program is designed by heqiuyun**》" ); settextstyle(0,0,1); outtextxy(60,100,"Operate handbook:"); outtextxy(70,140,"1.Black square denotations unpassable;" ); outtextxy(70,180,"2.White square denotations passable;" ); outtextxy(70,220,"3.Ratio mens white squares&&black squares" ); outtextxy(70,260,"4.Input rows,colums and ratio "); scanf("{%d,%d,%d}",&rows,&columns,&ratio); if(rows>MAXSIZE || rows<1 || columns>MAXSIZE ||columns<1) { getchar(); clrscr(); outtextxy(30,60,"attention! "); outtextxy(60,110,"1.1<=rows<=MAXSIZE"); outtextxy(60,160,"2.1<=columns<=MAXSIZE"); outtextxy(60,210,"3.MAXSIZE is defined 20"); outtextxy(60,240," Please enter the \"enter\" key" ); } getchar(); rows=20; columns=20; ratio=2; for(i=1;i<=MAXSIZE;i++) for(j=1;j<=MAXSIZE;j++) mazes.color[i][j]=WHITE; for(i=1;i<=rows;i++) for(j=1;j<=columns;j++) if(random(3)%(ratio)) mazes.color[i][j]=BLACK; } void PointColor(PosType curpos) { setcolor(GREEN); setfillstyle(SOLID_FILL,mazes.color[curpos.y][curpos.x]); bar((curpos.x+offset.x)*unit,(curpos.y+offset.y)*unit,(curpos.x+offset.x+1)*unit,(curpos.y+offset.y+1)*unit); rectangle((curpos.x+offset.x)*unit,(curpos.y+offset.y)*unit,( curpos.x+offset.x+1)*unit,(curpos.y+offset.y+1)*unit); } void DrawMaze() { int i,j,k,m,n; PosType curpos; clrscr(); m=getmaxx(); n=getmaxy(); k=(m>n?n:m); unit=k/(MAXSIZE*2); m=m/unit; n=n/unit; offset.x=m/4; offset.y=n/4; for(i=1;i<=rows;i++) for(j=1;j<=columns;j++) { curpos.x=j; curpos.y=i; PointColor(curpos); } printf("\n red square denotations maze start;yellow square denotations maze end.\n define maze start and end using (rowth,columnth).\n for example (2,3),(7,8) : "); scanf("(%d,%d),(%d,%d)",&mazes.start.y,&mazes.start.x,&mazes.end.y,&mazes.end.x); if(!mazes.start.y||!mazes.start.y||!mazes.end.y||!mazes.end.x) { mazes.start.y=1; mazes.start.x=1; mazes.end.y=20; mazes.end.x=20 ;} else if(mazes.start.x<1||mazes.start.x>columns||mazes.start.y<1||mazes.start.y>rows||mazes.end.x<1||mazes.end.x>columns||mazes.end.y<1||mazes.end.y>rows) { printf(" attention! 1<=x<=rows,1<=y<=columns, you define rows=%d,columns=%d",rows,columns); getch(); exit(0); } mazes.color[mazes.start.y][mazes.start.x]=RED; PointColor(mazes.start); sleep(1); mazes.color[mazes.end.y][mazes.end.x]=YELLOW; PointColor(mazes.end); sleep(1); i=0; while(++i<=4) { curpos=NextPos(mazes.end,i); if(mazes.color[curpos.y][curpos.x]==WHITE) break; } if(i==5) { printf(" maze end unpassable!\n"); getch(); exit(0); } } void MazePath(PosType start,PosType end) { Direct direct; if(start.x==end.x && start.y==end.y) { printf(" success!\n"); getch(); closegraph(); exit(0); } if(mazes.color[start.y][start.x]==RED ||mazes.color[start.y][start.x]==WHITE) { if(mazes.color[start.y][start.x]==WHITE) { mazes.color[start.y][start.x]=BLUE; PointColor(start); sleep(1); } Near(start,end); direction(&direct,start,end); MazePath(NextPos(start,direct.di1),end); MazePath(NextPos(start,direct.di2),end); MazePath(NextPos(start,direct.di3),end); MazePath(NextPos(start,direct.di4),end); if(mazes.color[start.y][start.x]!=RED) { mazes.color[start.y][start.x]=DARKGRAY; PointColor(start); sleep(1); } } } main() { Init(); CreateMaze(); DrawMaze(); MazePath(mazes.start,mazes.end); printf(" failed!\n"); getch(); closegraph(); }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值