服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现控制台随机迷宫的示例代码

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

2021-12-21 16:35Icys C/C++

本文主要介绍了C++实现控制台随机迷宫的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 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++ 控制台随机迷宫内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/Icys/p/15147267.html

延伸 · 阅读

精彩推荐