-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.cpp
140 lines (132 loc) · 3.86 KB
/
Generator.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
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
#include "stdafx.h"
#include "Generator.h"
Generator::Generator(int x, int y)
{
maxx = x;
maxy = y;
m = (int **)malloc(maxy * sizeof(int*));
for (int i = 0; i < maxy; i += 1)
{
m[i] = (int *)malloc(maxx * sizeof(int));
for (int j = 0; j < maxx; j += 1)
{
m[i][j] = 1;
}
}
}
Generator::~Generator()
{
}
void Generator::print_mas()
{
for (int j = 0; j < maxy; j += 1)
{
for (int i = 0; i < maxx; i += 1)
{
if (m[j][i] == 1)
printf("%c", 127);
else if (m[j][i] == 0)
printf(" ");
else if (m[j][i] == 2)
printf("*");
}
printf("\n");
}
printf("\n");
}
void Generator::generate(int x, int y, int ox, int oy)
{
if(x + 1 < maxx && y + 1 < maxy && x > 0 && y > 0){
if (m[y - 1][x] != 0 || (x == ox && y - 1 == oy)){
if(m[y][x - 1] != 0 || (x - 1 == ox && y == oy)){
if(m[y][x + 1] != 0 || (x + 1 == ox && y == oy)){
if(m[y + 1][x] != 0 || (x == ox && y + 1 == oy)){
m[y][x] = 0;
call(x, y, ox, oy);
}
}
}
}
}
}
void Generator::call(int x, int y, int ox, int oy)
{
srand(time(NULL));
int i = rand() % 10;
if(i == 0){
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
}else if(i == 1){
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
}else if(i == 2){
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
}else if(i == 3){
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
}else if(i == 4){
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
}else if(i == 5){
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
}else if(i == 6){
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
}else if(i == 7){
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
}else if(i == 8){
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
}else if(i == 9){
if(x != ox || y + 1 != oy) generate(x, y + 1, x, y);
if(x - 1 != ox || y != oy) generate(x - 1, y, x, y);
if(x != ox || y - 1 != oy) generate(x, y - 1, x, y);
if(x + 1 != ox || y != oy) generate(x + 1, y, x, y);
}
}
void Generator::print_mas2()
{
FILE * f = fopen("maze.txt", "w");
if (f == NULL) return;
for (int j = 0; j < maxy; j += 1)
{
for (int i = 0; i < maxx; i += 1)
{
if (m[j][i] == 1) fputc('1', f);
else if (m[j][i] == 0) fputc('0', f);
}
fputc('\n', f);
}
fclose(f);
}
void Generator::mazeGen()
{
srand(time(NULL));
int rx, ry;
rx = rand() % maxx;
srand(time(NULL));
ry = rand() % maxy;
generate(rx, ry, maxx, maxy);
print_mas2();
}