[開源]C++實現控制台隨機迷宮

我全程使用TCHAR系列函數,親測可以不改動程式碼兼容Unicode/ANSI開發環境,功能正常。大概有100行程式碼是來自網路的,我也做了改動,侵權請聯繫刪除。本文作者szx0427,只發佈於CSDN部落格園

這個程式碼不能算是完美,還是會有輕微的閃屏現象,懶得再加雙快取了,大家可以自行修改。這裡用的是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)

效果:
image

其中LENGTH宏規定了邊長。這裡是30。

image
想改邊長,直接更改那個30就可以了。

其中全局變數g_Rank規定了難度,數值越小難度越大,最小值為0。

image

也是一樣,改難度直接改這個就OK。


有什麼問題和建議,歡迎大家指正!!!!