C++實現控制臺隨機迷宮的示例代碼

我全程使用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)

效果:

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

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

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

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

到此這篇關於C++實現控制臺隨機迷宮的示例代碼的文章就介紹到這瞭,更多相關C++ 控制臺隨機迷宮內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: