[open source] C + + implementation of console random maze

Time:2021-11-19

I use the TCHAR series of functions throughout the whole process. I can test it without changing the code. It is compatible with Unicode / ANSI development environment, and the function is normal. There are about 100 lines of code from the network. I have also made changes. Please contact to delete the infringement. Author szx0427, published onCSDNAndBlog Garden

This code is not perfect, but there will still be a slight flicker. You are too lazy to add double cache. You can modify it yourself. The setconsolecursor position function and CLS are used here to refresh the screen.

All right, code! Vs2015 compilation passed without warning. Other versions should be OK

// C++ Maze main code
// Copyright (c) 2020 szx0427

#include 
#include 
#include 
#include 
#include 
using namespace std;

#ifdef _UNICODE
#include 
#include 
#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 ("you hit the wall more than the limit, this game fails!");
		_ Putts (_t ("if you want to start again, press the [R] key! Otherwise, press other keys to exit!");
		ch = _gettch();
		if (ch == 'R' || ch == 'r') {
			goto start;
		}
	} else if (bWin) {
		_tsystem(_T("cls"));
		_ Putts (_t ("Congratulations, you won! Do you want another game?");
		_ Putts (_t ("if you want to start again, press the [R] key! Otherwise, press other keys to exit!");
		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 ("allocating memory...);

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

	_ Tprintf (_t ("done! \ n"));

	_ Tprintf (_t ("loading maze...");

	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 ("done! \ 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 ("up, down, left and right direction keys are used to move, press ESC to exit, and press R to start again.);
	_ Tprintf (_t ("remaining times of hitting the wall:% d"), g_lives);
}

This is C + + style code. Because of the use of C + + features such as inline functions, it may not be directly compatible with the C language environment, but it can be perfectly compatible with a little change. (PS: at least you don’t have to worry about character set settings XD)

effect:
image

The length macro specifies the side length. This is 30.

image
If you want to change the side length, just change the 30.

Where global variablesg_RankThe difficulty is specified. The smaller the value, the greater the difficulty, and the minimum value is 0.

image

The same is true. If you change the difficulty, just change this directly.


Any questions and suggestions are welcome to correct!!!!