-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cpp
171 lines (145 loc) · 4.94 KB
/
map.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
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int ROWS = 11;
const int COLS = 19;
void print_map(char map[ROWS][COLS])
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << map[i][j];
}
cout << endl;
}
}
pair<int, int> random_position(bool can_be_edge = false)
{
if (can_be_edge)
{
return make_pair(rand() % ROWS, rand() % COLS);
}
else
{
return make_pair(rand() % (ROWS - 2) + 1, rand() % (COLS - 2) + 1);
}
}
bool is_valid_position(pair<int, int> pos, vector<pair<int, int>> &occupied_positions)
{
return find(occupied_positions.begin(), occupied_positions.end(), pos) == occupied_positions.end();
}
vector<pair<int, int>> get_reachable_positions(char map[ROWS][COLS], pair<int, int> start)
{
queue<pair<int, int>> q;
q.push(start);
vector<pair<int, int>> visited;
visited.push_back(start);
vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!q.empty())
{
auto current = q.front();
q.pop();
for (const auto &dir : directions)
{
int new_row = current.first + dir.first;
int new_col = current.second + dir.second;
if (new_row >= 0 && new_row < ROWS && new_col >= 0 && new_col < COLS)
{
pair<int, int> new_pos = make_pair(new_row, new_col);
if (is_valid_position(new_pos, visited) && map[new_row][new_col] != '#')
{
visited.push_back(new_pos);
q.push(new_pos);
}
}
}
}
return visited;
}
int main()
{
srand(time(nullptr));
char map[ROWS][COLS];
// Initialize map with walls and empty spaces
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (i == 0 || i == ROWS - 1 || j == 0 || j == COLS - 1)
{
map[i][j] = '#';
}
else
{
map[i][j] = ' ';
}
}
}
vector<pair<int, int>> occupied_positions;
// Place player 'O'
auto player_pos = make_pair(1, 1);
map[player_pos.first][player_pos.second] = 'O';
occupied_positions.push_back(player_pos);
// Get reachable positions from the starting position
auto reachable_positions = get_reachable_positions(map, player_pos);
// Place destination 'E'
auto destination_pos = reachable_positions[rand() % reachable_positions.size()];
map[destination_pos.first][destination_pos.second] = 'E';
occupied_positions.push_back(destination_pos);
// Remove occupied positions from reachable_positions
reachable_positions.erase(remove(reachable_positions.begin(), reachable_positions.end(), destination_pos), reachable_positions.end());
// Place mini-game spots '*' with random occurrences between 7 and 13
int num_minigames = rand() % (13 - 7 + 1) + 7;
for (int i = 0; i < num_minigames; i++)
{
auto minigame_pos = random_position();
while (!is_valid_position(minigame_pos, occupied_positions))
{
minigame_pos = random_position();
}
map[minigame_pos.first][minigame_pos.second] = '*';
occupied_positions.push_back(minigame_pos);
}
// Place random teleport spots '?' with random occurrences between 7 and 13
int num_teleports = rand() % (13 - 7 + 1) + 7;
for (int i = 0; i < num_teleports; i++)
{
auto teleport_pos = reachable_positions[rand() % reachable_positions.size()];
while (!is_valid_position(teleport_pos, occupied_positions))
{
teleport_pos = reachable_positions[rand() % reachable_positions.size()];
}
map[teleport_pos.first][teleport_pos.second] = '?';
occupied_positions.push_back(teleport_pos);
reachable_positions.erase(remove(reachable_positions.begin(), reachable_positions.end(), teleport_pos), reachable_positions.end());
}
// Place random walls '#' inside the map
int num_walls = rand() % (40 - 32 + 1) + 27;
for (int i = 0; i < num_walls; i++)
{
auto wall_pos = random_position();
while (!is_valid_position(wall_pos, occupied_positions))
{
wall_pos = random_position();
}
map[wall_pos.first][wall_pos.second] = '#';
occupied_positions.push_back(wall_pos);
}
// Create a new vector called valid_transfer_positions with the remaining reachable_positions
vector<pair<int, int>> valid_transfer_positions = reachable_positions;
// Print the map
print_map(map);
// Output valid_transfer_positions to standard error
cerr << "Valid Transfer Positions:" << endl;
for (const auto &pos : valid_transfer_positions)
{
cerr << "(" << pos.first << ", " << pos.second << ")" << endl;
}
return 0;
return 0;
}