-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
172 lines (125 loc) · 2.76 KB
/
Map.cpp
File metadata and controls
172 lines (125 loc) · 2.76 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
166
167
168
169
170
171
172
#include "Map.h"
#include <thread>
#include <chrono>
#include <random>
#include <unistd.h>
using namespace std;
// Constructor
Map::Map()
{
for (int i = 0; i < MAP_SIZE; i++)
{
vector<Cell*> v;
for (int j = 0; j < MAP_SIZE; j++)
v.push_back(new Cell(INITIAL_DUST_SPAWN_RATE, INITIAL_JEWEL_SPAWN_RATE));
map.push_back(v);
}
//cout << "\nProcess finished with exit code -1073741819 (0xC0000005)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
}
// Destructor
Map::~Map() {
for (auto & i : map)
for (auto & j : i)
delete j;
}
int Map::getMapSize() {
return MAP_SIZE;
}
Cell* Map::getCell(int x, int y) {
return map[x][y];
}
// Updator
void Map::mapUpdator(){
for (auto & i : map)
for (auto & j : i)
j->spawnUpdate();
}
void Map::objSpawn(int wait_time){
int tick = 0;
while(true) {
this_thread::sleep_for(chrono::milliseconds (1));
mapUpdator();
tick++;
if (tick > 50){
this_thread::sleep_for(chrono::seconds (wait_time));
tick = 0;
}
}
}
void Map::clean() {
getCell(getVacuum().first, getVacuum().second)->m_dust = false;
getCell(getVacuum().first, getVacuum().second)->m_jewel = false;
}
void Map::pickup() {
getCell(getVacuum().first, getVacuum().second)->m_jewel = false;
}
pair<int, int> Map::getVacuum() {
for (int i = 0; i < MAP_SIZE; i++)
for (int j = 0; j < MAP_SIZE; j++)
if(map[i][j]->hasVacuum())
return make_pair(i,j);
return make_pair(0,0);
}
ostream &operator<<(ostream & output, const Map& mp) {
for (auto & i : mp.map)
{
for (auto & j : i)
output <<*j << " ";
output << endl;
}
return output;
}
// Constructor
Cell::Cell(double dustSpawnRate, double jewelSpawnRate) {
setVacuum(false);
int dustProb = rand() % 100 + 1;
int jewelProb = rand() % 100 + 1;
if (dustProb < dustSpawnRate)
setDust(true);
if (jewelProb < jewelSpawnRate)
setJewel(true);
}
Cell::~Cell() {
}
void Cell::spawnUpdate() {
int dustProb = rand() % 100 + 1; // FIX IS HERE
int jewelProb = rand() % 100 + 1; // FIX IS HERE
if (dustProb < DUST_SPAWN_RATE)
setDust(true);
if (jewelProb < JEWEL_SPAWN_RATE)
setJewel(true);
}
bool Cell::hasDust() const {
return m_dust;
}
bool Cell::hasJewel() const {
return m_jewel;
}
bool Cell::hasVacuum() const {
return m_vacuum;
}
void Cell::setDust(bool arg) {
m_dust = arg;
}
void Cell::setJewel(bool arg) {
m_jewel = arg;
}
void Cell::setVacuum(bool arg) {
m_vacuum = arg;
}
ostream& operator<<(ostream& output, const Cell& c) {
string res;
if (c.hasDust())
res.append("D");
if (c.hasVacuum())
res.append("V");
if (c.hasJewel())
res.append("J");
if (c.hasDust() && c.hasJewel() && c.hasVacuum())
res = "A";
if (res.empty())
res = ".";
res.append(" ");
output << res;
return output;
}