栈实现的一个小迷宫游戏

昨天学习了栈结构以后通宵写了个利用栈实现深度遍历的迷宫小程序。

简单解释下:

在main函数中定义了一个N*N的二维char数组,全部用#填充,调用build生成地图,调用output输出地图。

重点在build,在build中定义了一个起始点(0, 1),定义了一个栈结构,循环调用valiable来生成可用的方向和步数,调用schess把生成的方向和步数画到地图上。

如果已无法生成点则退出循环,把结束点也打通即可。这里的重点在valiable函数,不细说了,看代码吧。

     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
     >>      # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
     # #   # #   #   #   # # # # # # #         #         #         # 
     # #     #       #                   # #   # #   # # #   # #   # 
     # # #       #   # #   #   # # # #           #   # #     #     # 
     # # # # # # #     #   #   #         #   #   #         # #   # # 
     # #           #       #   #   #   #     #   # #   #   # #     # 
     # #   #   #       #       #   #   # # # #     # # #     # #   # 
     # #   #   # # #   # # #   #   #           #           # #     # 
     # #   #               #   #     # # # #   #   # # #   #     # # 
     # #   #   # # # #   #       #   # #   #   #             #   # # 
     # #   # # #         #   # # #         #   #   # # # # # #   # # 
     # #         # # #             # # #   #                     # # 
     # #   # #           # #   #       #   #   # #   #   # # #   # # 
     # #     #   # # #     #     # #       # #   #   #   #         # 
     # # #   #   #   # #   # #     #   # #       #         # # # # # 
     # #               #   # # #   #     #   #   # # # #           # 
     # #   # # #   #   #           # #       #           # # # #   # 
     # # # #   #   #   # # # # #       # #       # # #             # 
     # # #     #   #         #     #   # # # #   #   # # # # #   # # 
     # # #   # #   # # # #       # #         #   #           #     # 
     # #     # #       # #   #   #   # # #   #   #   # # #   # #   # 
     # #   #       #     #   #   #   #   #   #   #                 # 
     # #       #   # #       #   #   #       #   #   # # # # # #   # 
     # #   #         #   #   #   #       # # #   # # #             # 
     # #   #   # # # #             # #                   #   #   # # 
     # #   #             # #   #     # #   #   # # # #       #     # 
     # # # #   # # # # #   #   # #   # #   # #           #   # #   # 
     # # #     #           #   #                 # # # # #   # #   # 
     # # #   #     # # #   #       # # # # # # #                   # 
     # # #       # #           #   #               # # # # # # #     >>>
     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define N 0x20
#define X '#'

typedef struct {
	int x, y;
} point;

enum direction {
	UP,
	RIGHT,
	DOWN,
	LEFT,
	UNVALID
};

typedef struct {
	point *bottom;
	point *top;
	unsigned int length;
} stack;

int initstack(stack *p, unsigned int n)
{
	p->bottom = p->top = (point*)malloc(sizeof(point) * n);
	p->length = n;
	return 0;
}

int cleanstack(stack *p)
{
	free(p->bottom);
	p->bottom = p->top = NULL;
	p->length = 0;
	return 0;
}

int push(stack *p, point n)
{
	*++p->top = n;
	return 0;
}

point pop(stack *p)
{
	return *p->top--;
}

int output(char p[N][N])
{
	int i, j;
	printf("\n");
	for (i = 0; i < N; i++)
	{
		if (i == 1)
		{
			printf(">>>  ");
		}
		else
		{
			printf("     ");
		}
		for (j = 0; j < N; j++)
		{
			printf("%c ", p[j][i]);
		}
		if (i == N - 2)
		{
			printf("  >>>\n");
		}
		else
		{
			printf("\n");
		}
	}
	printf("\n");
	return 0;
}

enum direction direct(point p)
{
	return UP + rand() % 4;
}

int steplength()
{
	return rand() % 2 + 1;
}

enum direction valiable(point *p, int *length, char chess[N][N], stack *x)
{
	enum direction d;
	int l;
	int k = 0;
	int fs = 0;
	while (1)
	{
		int f = 0;
		int i, r;
		k += 1;
		d = direct(*p);
		l = steplength();
		if (fs)
		{
			l = 1;
		}
		switch (d)
		{
		case UP:
			if (p->y - l > 1 && p->x != 0)
			{
				for (i = 1, r = 0; i <= l; i++)
				{
					r = r + chess[p->x][p->y - i] + chess[p->x - 1][p->y - i] + chess[p->x + 1][p->y - i];
				}
				if (r / (l * 3) == X)
				{
					f = 1;
				}
			}
			break;
		case RIGHT:
			if (p->x + l < N - 1)
			{
				for (i = 1, r = 0; i <= l; i++)
				{
					r = r + chess[p->x + i][p->y] + chess[p->x + i][p->y - 1] + chess[p->x + i][p->y + 1];
				}
				if (r / (l * 3) == X)
				{
					f = 1;
				}
			}
			break;
		case DOWN:
			if (p->y + l < N - 1 && p->x != 0)
			{
				for (i = 1, r = 0; i <= l; i++)
				{
					r = r + chess[p->x][p->y + i] + chess[p->x - 1][p->y + i] + chess[p->x + 1][p->y + i];
				}
				if (r / (l * 3) == X)
				{
					f = 1;
				}
			}
			break;
		case LEFT:
			if (p->x - l > 1)
			{
				for (i = 1, r = 0; i <= l; i++)
				{
					r = r + chess[p->x - i][p->y] + chess[p->x - i][p->y - 1] + chess[p->x - i][p->y + 1];
				}
				if (r / (l * 3) == X)
				{
					f = 1;
				}
			}
			break;
		default:
			break;
		}
		if (f)
		{
			break;
		}
		if (k > 10)
		{
			k = 0;
			/*if ((p->x > 1 && p->y > 1 && p->x < N - 2 && p->y < N - 2) &&
				(chess[p->x + 1][p->y] + chess[p->x + 1][p->y - 1] + chess[p->x + 1][p->y + 1] + chess[p->x + 2][p->y] + chess[p->x + 2][p->y - 1] + chess[p->x + 2][p->y + 1] == X * 6
				|| chess[p->x - 1][p->y] + chess[p->x - 1][p->y - 1] + chess[p->x - 1][p->y + 1] + chess[p->x - 2][p->y] + chess[p->x - 2][p->y - 1] + chess[p->x - 2][p->y + 1] == X * 6
				|| chess[p->x][p->y + 1] + chess[p->x - 1][p->y + 1] + chess[p->x + 1][p->y + 1] + chess[p->x][p->y + 2] + chess[p->x + 1][p->y + 2] + chess[p->x - 1][p->y + 2] == X * 6
				|| chess[p->x][p->y - 1] + chess[p->x + 1][p->y - 1] + chess[p->x - 1][p->y - 1] + chess[p->x][p->y - 2] + chess[p->x + 1][p->y - 2] + chess[p->x - 1][p->y - 2] == X * 6))
			{
				continue;
				fs = 1;
			}
			else */if (p->x == 0 && p->y == 1)
			{
				*length = 0;
				return UNVALID;
			}
			else
			{
				*p = pop(x);
			}
		}
	}
	*length = l;
	return d;
}

point schess(char chess[N][N], enum direction d, int length, point p, stack *x)
{
	int i;
	switch (d)
	{
	case UP:
		for (i = 0; i < length; i++)
		{
			point tp = {p.x, p.y - i};
			chess[p.x][p.y - i] = ' ';
			push(x, tp);
			//if (p.x == N - 2 && p.y == N - 2)
			//{
			//	return p;
			//}
		}
		p.y -= length;
		break;
	case RIGHT:
		for (i = 0; i < length; i++)
		{
			point tp = {p.x + i, p.y};
			chess[p.x + i][p.y] = ' ';
			push(x, tp);
			//if (p.x == N - 2 && p.y == N - 2)
			//{
			//	return p;
			//}
		}
		p.x += length;
		break;
	case DOWN:
		for (i = 0; i < length; i++)
		{
			point tp = {p.x, p.y + i};
			chess[p.x][p.y + i] = ' ';
			push(x, tp);
			//if (p.x == N - 2 && p.y == N - 2)
			//{
			//	return p;
			//}
		}
		p.y += length;
		break;
	case LEFT:
		for (i = 0; i < length; i++)
		{
			point tp = {p.x - i, p.y};
			chess[p.x - i][p.y] = ' ';
			push(x, tp);
			//if (p.x == N - 2 && p.y == N - 2)
			//{
			//	return p;
			//}
		}
		p.x -= length;
		break;
	default:
		break;
	}
	return p;
}

int build(char chess[N][N])
{
	point p = {0, 1};
	stack x;
	initstack(&x, sizeof(point) * N * N);
	while (1)
	{
		int l;
		enum direction d = valiable(&p, &l, chess, &x);
		//output(chess);
		//printf("%d, %d\n", p.x, p.y);
		if (d == UNVALID)
		{
			chess[N - 2][N - 2] = ' ';
			chess[N - 1][N - 2] = ' ';
			p.x = N - 1;
			p.y = N - 2;
			break;
		}
		p = schess(chess, d, l, p, &x);
		//if (p.x == N - 2 && p.y == N - 2)
		//{
		//	chess[p.x][p.y] = ' ';
		//	chess[p.x + 1][p.y] = ' ';
		//	break;
		//}
	}
	cleanstack(&x);
	return 0;
}

int main(int argc, char *argv[])
{
	srand(time(NULL));
	char chess[N][N];
	memset(chess, X, N * N);
	chess[0][1] = ' ';
	build(chess);
	output(chess);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值