-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
165 lines (140 loc) · 4.48 KB
/
Map.cpp
File metadata and controls
165 lines (140 loc) · 4.48 KB
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
/*
* File: Map.cpp
* Author: Delunado
*
* Created on 22 de marzo de 2018, 19:07
*/
#include "Map.h"
#include <ctime>
Map::Map(int xStart, int yStart, int totalRooms):
xStart(xStart), yStart(yStart), totalRooms(totalRooms), numRooms(0)
{
for (int i = 0; i < Map::MAX_ROOM; i++){
this->vRoom[i] = 0;
}
for (int i = 0; i < Map::MAX_ROOM; i++){
for (int j = 0; j < Map::MAX_ROOM; j++){
this->vMap[i][j] = 0;
}
}
srand(time(0));
}
void Map::AddRoom() {
Room* h = new Room();
this->vRoom[this->numRooms] = h;
this->numRooms++;
}
void Map::DeleteRoom(int pos) {
delete this->vRoom[0];
vRoom[0] = this->vRoom[this->numRooms-1];
this->vRoom[this->numRooms-1] = 0;
this->numRooms--;
}
void Map::GenerateRandomMap(int tRooms) {
/**
* @toDo it doesnt work properly sometimes. fix it.
* @param tRooms
*/
//if (tRooms = MAX_ROOM)
//throw std::out_of_range("The number of rooms is greater than permitted");
//First you have to generate the rooms.
for (int i = 1; i < tRooms; i++){
AddRoom();
}
//Here you fill the first position in the new map with the first room.
Room* h = new Room(*this->vRoom[0]);
this->vMap[this->xStart][this->yStart] = h;
tRooms--;
this->MatrixRoomFiller(tRooms, this->xStart, this->yStart);
}
void Map::MatrixRoomFiller(int auxRooms, int auxX, int auxY) {
//Here you fill the matrix with rooms, spreading in horizontal or vertical position
int direction = 0;
while(this->numRooms != 0){
if (this->vMap[auxX+1][auxY] != 0 && this->vMap[auxX-1][auxY] != 0 &&
this->vMap[auxX][auxY+1] != 0 && this->vMap[auxX][auxY-1] != 0){
return;
}
direction = rand() % 4 + 1;
//1 = Up, 2 = Down, 3 = Left, 4 = Right
switch (direction){
case 1:
if (auxY-1 < 0){
break;
}
if (this->vMap[auxX][auxY-1] == 0){
auxY -= 1;
Room* h = new Room(*this->vRoom[0]);
h->SetShape('X');
this->vMap[auxX][auxY] = h;
this->DeleteRoom(0);
} else {
break;
}
break;
case 2:
if (auxY+1 > Map::MAX_ROOM){
break;
}
if (vMap[auxX][auxY+1] == 0){
auxY += 1;
Room* h = new Room(*this->vRoom[0]);
h->SetShape('X');
this->vMap[auxX][auxY] = h;
this->DeleteRoom(0);
} else {
break;
}
break;
case 3:
if (auxX-1 < 0){
break;
}
if (vMap[auxX-1][auxY] == 0){
auxX -= 1;
Room* h = new Room(*this->vRoom[0]);
h->SetShape('X');
this->vMap[auxX][auxY] = h;
this->DeleteRoom(0);
} else {
break;
}
break;
case 4:
if (auxX+1 > Map::MAX_ROOM){
break;
}
if (vMap[auxX+1][auxY] == 0){
auxX += 1;
Room* h = new Room(*this->vRoom[0]);
h->SetShape('X');
this->vMap[auxX][auxY] = h;
this->DeleteRoom(0);
} else {
break;
}
break;
}
}
}
void Map::DrawMap() {
for (int i = 0; i < this->totalRooms; i++){
for (int j = 0; j < this->totalRooms; j++){
if (this->vMap[i][j] == 0)
std::cout << " . ";
else
std::cout << " " << this->vMap[i][j]->GetShape() << " ";
}
std::cout << std::endl;
}
}
void Map::CleanMap(){
for (int i = 0; i < Map::MAX_ROOM; i++){
this->vRoom[i] = 0;
}
for (int i = 0; i < Map::MAX_ROOM; i++){
for (int j = 0; j < Map::MAX_ROOM; j++){
this->vMap[i][j] = 0;
}
}
}