我全程使用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++ 控制台随机迷宫内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!