欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++实现控制台随机迷宫的示例代码

程序员文章站 2022-06-30 21:51:01
我全程使用tchar系列函数,亲测可以不改动代码兼容unicode/ansi开发环境,功能正常。大概有100行代码是来自网络的,我也做了改动,侵权请联系删除。这个代码不能算是完美,还是会有轻微的闪屏现...

我全程使用tchar系列函数,亲测可以不改动代码兼容unicode/ansi开发环境,功能正常。大概有100行代码是来自网络的,我也做了改动,侵权请联系删除。

这个代码不能算是完美,还是会有轻微的闪屏现象,懒得再加双缓存了,大家可以自行修改。这里用的是setconsolecursorposition函数和cls刷新屏幕。

好了,上代码!vs2015编译通过无警告。其他版本应该也没问题

// c++ maze main code
// copyright (c) 2020 szx0427

#include <cstdio>
#include <windows.h>
#include <conio.h>
#include <tchar.h>
#include <ctime>
using namespace std;

#ifdef _unicode
#include <io.h>
#include <fcntl.h>
#define ch_rect   l'■' // a rectangle (wall)
#define ch_player l'○' // a circle (player)
#define ch_space  l' ' // a space (route)
#else
#define ch_rect   '#'
#define ch_player 'o'
#define ch_space  ' '
#endif // _unicode

#define length (30 + 2 * 2)
#define wall   0
#define route  1
#define player 2

static uint   g_rank  = 0;
static short  g_lives = 3;
static bool** g_maze  = nullptr;

void _create(
	__in    const int x, 
	__in    const int y );

void _print(void);

int _createandprint(void);

inline void _die(void)
{
	--g_lives;
	for (int n = 1; n <= 2; n++) {
		_tsystem(_t("color fc"));
		sleep(70);
		_tsystem(_t("color 07"));
		sleep(70);
	}
}


int _tmain(void)
{
	console_cursor_info cci;
	getconsolecursorinfo(getstdhandle(std_output_handle), &cci);
	cci.bvisible = false;
	setconsolecursorinfo(getstdhandle(std_output_handle), &cci);

#ifdef _unicode
	_setmode(_fileno(stdout), _o_u16text);
#endif // _unicode

	srand((uint)time(null));
	int k;

start:
	k = _createandprint();

	tchar ch;
	bool bexit = false;
	bool bwin = false;
	int x = 2, y = 1;
	while (!bexit && g_lives >= 0 && !bwin) {
		ch = _gettch();
		switch (ch) {
		case _t('r'):
		case _t('r'):
			_tsystem(_t("cls"));
			x = 2; y = 1;
			for (int l = 0; l < length; l++) {
				free(g_maze[l]);
			}
			free(g_maze);
			k = _createandprint();
			break;
		case vk_escape:
			bexit = true;
			break;
		case tchar(0xe0):
			switch (ch = _gettch()) {
			case tchar(72):
				if (g_maze[x - 1][y] != wall) {
					g_maze[x][y] = route;
					--x;
					g_maze[x][y] = player;
				} else {
					_die();
				} break;
			case tchar(80):
				if (g_maze[x + 1][y] != wall) {
					g_maze[x][y] = route;
					++x;
					g_maze[x][y] = player;
				} else {
					_die();
				} break;
			case tchar(75):
				if (g_maze[x][y - 1] != wall && !(x == 2 && y == 1)) {
					g_maze[x][y] = route;
					--y;
					g_maze[x][y] = player;
				} else {
					_die();
				} break;
			case tchar(77):
				if (g_maze[x][y + 1] != wall) {
					g_maze[x][y] = route;
					++y;
					g_maze[x][y] = player;
				} else {
					_die();
				} break;
			default: break;
			}
			if (x == k && y == length - 2) {
				bwin = true;
			}
			_print();
			break;

		default: break;
		}
	}

	x = 2; y = 1;

	for (int l = 0; l < length; l++) {
		free(g_maze[l]);
	}
	free(g_maze);

	if (g_lives == -1) {
		_tsystem(_t("cls"));
		_putts(_t("你撞墙次数超过限制,本局游戏失败!"));
		_putts(_t("如果要再开局,请按下[r]键!否则,按下其他键以退出!"));
		ch = _gettch();
		if (ch == 'r' || ch == 'r') {
			goto start;
		}
	} else if (bwin) {
		_tsystem(_t("cls"));
		_putts(_t("恭喜,你赢了!是否要再来一局?"));
		_putts(_t("如果要再开局,请按下[r]键!否则,按下其他键以退出!"));
		ch = _gettch();
		if (ch == 'r' || ch == 'r') {
			goto start;
		}
	}

	return 0;
}

void _create(const int x, const int y)
{

	g_maze[x][y] = route;

	int dict[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };

	int r, tmp;
	for (int i = 0; i < 4; i++) {
		r = rand() % 4;
		tmp = dict[0][0];
		dict[0][0] = dict[r][0];
		dict[r][0] = tmp;
		tmp = dict[0][1];
		dict[0][1] = dict[r][1];
		dict[r][1] = tmp;
	}

	int dx, dy, range, count;
	for (int j = 0; j < 4; j++) {
		dx = x;
		dy = y;
		range = 1 + (g_rank == 0 ? 0 : rand() % g_rank);
		while (range > 0) {
			dx += dict[j][0];
			dy += dict[j][1];

			if (g_maze[dx][dy] == route) {
				break;
			}

			count = 0;
			for (int k = dx - 1; k < dx + 2; k++) {
				for (int l = dy - 1; l < dy + 2; l++) {
					if (abs(k - dx) + abs(l - dy) == 1 && g_maze[k][l] == route) {
						count++;
					}
				}
			}

			if (count > 1) {
				break;
			}

			--range;
			g_maze[dx][dy] = route;
		}

		if (range <= 0) {
			_create(dx, dy);
		}
	}
}

int _createandprint(void)
{
	_tprintf(_t("正在分配内存..."));

	g_maze = (int**)malloc(length * sizeof(int*));
	for (int i = 0; i < length; i++) {
		g_maze[i] = (int*)calloc(length, sizeof(int));
	}

	_tprintf(_t("完成!\n"));

	_tprintf(_t("正在加载迷宫..."));

	g_lives = 3;

	for (int j = 0; j < length; j++) {
		g_maze[j][0] = route;
		g_maze[0][j] = route;
		g_maze[j][length - 1] = route;
		g_maze[length - 1][j] = route;
	}

	_create(2, 2);
	g_maze[2][1] = player;

	int k;
	for (k = length - 3; k >= 0; k--) {
		if (g_maze[k][length - 3] == route) {
			g_maze[k][length - 2] = route;
			break;
		}
	}

	_tprintf(_t("完成!\n"));
	_print();

	return k;
}

void _print(void)
{
	setconsolecursorposition(getstdhandle(std_output_handle), { 0, 0 });

	for (int x = 0; x < length; x++) {
		for (int y = 0; y < length; y++) {
			switch (g_maze[x][y]) {
			case route:
				_puttch(ch_space); break;
			case wall:
				_puttch(ch_rect); break;
			case player:
				_puttch(ch_player); break;
			default: break;
			}
		}
		_tprintf(_t("\n"));
	}

	_putts(_t("上下左右方向键用来移动,按esc可退出,按r重新开局。"));
	_tprintf(_t("剩余可撞墙次数:%d"), g_lives);
}

这是c++风格的代码。因为用到了内联函数等c++特性,可能不能直接兼容c语言环境,但是稍作改动即可完美兼容。(ps:至少大家不用为字符集设置发愁了 xd)

效果:

C++实现控制台随机迷宫的示例代码

其中length宏规定了边长。这里是30。

C++实现控制台随机迷宫的示例代码

想改边长,直接更改那个30就可以了。

其中全局变量g_rank规定了难度,数值越小难度越大,最小值为0。

C++实现控制台随机迷宫的示例代码

也是一样,改难度直接改这个就ok。

到此这篇关于c++实现控制台随机迷宫的示例代码的文章就介绍到这了,更多相关c++ 控制台随机迷宫内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: C++ 迷宫