-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomCharMap.cpp
104 lines (93 loc) · 1.99 KB
/
RandomCharMap.cpp
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
const char chars[4] = { ' ', '~', '|', '^' };
const int Y = 30;
const int X = 120;
int mapBase[Y][X] = { 0 }; //yx
char map[Y][X] = {};
void genLakes(int radius, int n)
{
int modFacX = X - 1 - (2 * radius);
int modFacY = Y - 1 - (2 * radius);
while (n != 0) {
int x = rand() % modFacX + radius;
int y = rand() % modFacY + radius;
if (mapBase[y][x] == 0) {
for (int i = y - radius; i <= y + radius; i++)
{
for (int j = x - radius; j <= x + radius; j++)
{
if (rand() % 5 == 0 && (j == x + radius || i == y + radius || i == y - radius))
{
break;
}
mapBase[i][j] = 1;
}
}
n--;
}
}
}
void genMountains(int n)
{
int x, y;
while (n != 0)
{
x = abs(rand() % X - 1);
y = abs(rand() % Y - 1);
if (mapBase[y - 1][x] != 1 && mapBase[y + 1][x] != 1 && mapBase[y][x - 1] != 1 && mapBase[y][x + 1] != 1 && (mapBase[y][x] == 0 || mapBase[y][x] == 2))
{
mapBase[y][x] = 3;
n--;
}
}
}
void genTreeClusters(int n)
{
int x, y;
while (n != 0)
{
x = abs(rand() % X - 1);
y = abs(rand() % Y - 1);
if (mapBase[y - 1][x] != 1 && mapBase[y + 1][x] != 1 && mapBase[y][x - 1] != 1 && mapBase[y][x + 1] != 1 && mapBase[y][x] == 0)
{
mapBase[y][x] = 2;
for (int i = 0; i < 2; i++)
{
mapBase[abs((y + (rand() % 2)) % Y)][(abs(x + (rand() % 2)) % X)] = 2;
mapBase[abs((y - (rand() % 2)) % Y)][(abs(x + (rand() % 2)) % X)] = 2;
mapBase[abs((y + (rand() % 2)) % Y)][(abs(x - (rand() % 2)) % X)] = 2;
mapBase[abs((y - (rand() % 2)) % Y)][(abs(x - (rand() % 2)) % X)] = 2;
}
n--;
}
}
}
void convertBaseToMap()
{
for (int i = 0; i < Y; i++)
{
for (int j = 0; j < X; j++)
{
map[i][j] = chars[mapBase[i][j]];
}
}
}
int main()
{
srand(time(NULL));
genLakes(2, 6);
genMountains(30);
genTreeClusters(40);
convertBaseToMap();
for (int i = 0; i < Y; i++)
{
for (int j = 0; j < X; j++)
{
std::cout << map[i][j];
}
std::cout << '\n';
}
}