-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.cpp
More file actions
executable file
·554 lines (476 loc) · 15 KB
/
Copy pathmap.cpp
File metadata and controls
executable file
·554 lines (476 loc) · 15 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
* Project name: Food collection
* Version 4
* Student : Albert Eduard Merino Pulido
*/
#include "map.h"
using namespace std;
const int Map::initX = 1;
const int Map::initY = 1;
float Map::seed = 1;
bool Map::isSeed = false;
// Constructors
Map::Map(){ }
Map::Map(int height, int width)
: height(height), width(width){
scorePlayer = 0;
scoreEnemy = 0;
totalFood = -2;
bulletEnable = true;
}
Map::Map(char fname[]){ getMapFromFile(fname); }
// Getters
int Map::getHeight(){ return height; }
int Map::getWidth(){ return width; }
void Map::setSeed(float seed){
this->isSeed = true;
this->seed = seed;
}
float Map::getSeed(){
return (this->isSeed) ? this->seed : rand();
}
vector<vector<Cell *> > Map::getMap(){ return map; }
string Map::toString(){
ostringstream mapString;
for (unsigned int i = 0; i < map.size(); i++) {
for (unsigned int j = 0; j < map[i].size(); j++) {
mapString << map[i][j]->getSymbol() << " ";
}
mapString << endl;
}
return mapString.str();
}
// Methods
void Map::generate(){
srand(getSeed());
initCells();
connectCells();
inside();
middle();
inferiorRandom();
middleRandom();
mirror();
connectCells();
initMap();
}
void Map::initMap(){
for (unsigned int i = 0; i < map.size(); i++)
for (unsigned int j = 0; j < map[0].size(); j++)
if (map[i][j]->isCorridor()) {
map[i][j]->setCellType(FOOD);
totalFood += 1;
}
}
Cell * Map::getInitPosition(CellType agent){
return (agent == PLAYER) ? map[Map::initX][Map::initY] : map[Map::initX][width - 1 - Map::initY];
}
void Map::setPosition(CellType agent, Cell * c){
if (agent == PLAYER) {
player = c;
} else if (agent == ENEMY) {
enemy = c;
} else {
bullet = c;
}
}
/*
* Create and put all cells in the Vector<<Vector<Cell>>
*/
void Map::initCells(){
for (int i = 0; i < height; i++) {
vector<Cell *> aux;
for (int j = 0; j < ceil(width / 2.0); j++)
aux.push_back(new Cell(i, j, WALL));
map.push_back(aux);
}
}
/*
* Connect the Cells - Top, Left, Right, Bottom.
*/
void Map::connect(Cell * c){
if ((c->getX() - 1) >= 0) c->setUp(map[c->getX() - 1][c->getY()]);
if ((c->getX() + 1) <= height - 1) c->setDown(map[c->getX() + 1][c->getY()]);
if ((c->getY() - 1) >= 0) c->setLeft(map[c->getX()][c->getY() - 1]);
if ((c->getY() + 1) <= width + 1) c->setRight(map[c->getX()][c->getY() + 1]);
}
/*
* Analyze one by one the Cells use connect function.
*/
void Map::connectCells(){
for (unsigned int i = 0; i < map.size(); i++) {
for (unsigned int j = 0; j < map[0].size(); j++) {
connect(map[i][j]);
}
}
}
/*
* Logic to put the white positions
* Ex: 11111
* 10101
* 11111
* 10101
* 11111
*/
void Map::initWhitePositionCells(){
int decision;
decision = floor(width / 2.0);
if (width % 2 != 0) decision += 1;
for (int i = 1; i < height - 1; i += 2) {
vector<Cell *> aux;
for (int j = 1; j < decision; j += 2) {
map[i][j]->setCellType(CORRIDOR);
aux.push_back(map[i][j]);
}
visited.push_back(aux);
}
}
/*
* Will random the for positions and look which position choose. (top/bottom/left/right)
*/
Cell * Map::randomDiscoverPath(Cell * c){
Cell * tempCell = NULL;
int x, y;
vector<Direction> shuffle;
shuffle.push_back(UP);
shuffle.push_back(DOWN);
shuffle.push_back(LEFT);
shuffle.push_back(RIGHT);
random_shuffle(shuffle.begin(), shuffle.end());
for (unsigned int i = 0; i < shuffle.size(); i++) {
x = floor((c->getX() - 1) / 2.0);
y = floor((c->getY() - 1) / 2.0);
if ((shuffle[i] == UP) && (insideCondition(x - 1, y))) {
x--;
tempCell = c->getUp();
} else if ((shuffle[i] == DOWN) && (insideCondition(x + 1, y))) {
x++;
tempCell = c->getDown();
} else if ((shuffle[i] == LEFT) && (insideCondition(x, y - 1))) {
y--;
tempCell = c->getLeft();
} else if ((shuffle[i] == RIGHT) && (insideCondition(x, y + 1))) {
y++;
tempCell = c->getRight();
}
if (!visited[x][y]->isVisited()) {
tempCell->setCellType(CORRIDOR);
return visited[x][y];
}
}
return NULL;
} // randomDiscoverPath
/*
* Analyze all the white positions and get one random position (top/bottom/left/right) and change wall to corridor.
* If don't have exit, will pop the previous positions to analyze again.
*/
void Map::inside(){
initWhitePositionCells();
vector<Direction> shuffle;
shuffle.push_back(UP);
shuffle.push_back(DOWN);
shuffle.push_back(LEFT);
shuffle.push_back(RIGHT);
int h = visited.size();
int w = visited[0].size();
// Primeiro elemento visitado.
Cell * position = visited[0][0];
position->setVisited(true);
int quantidadeVisitados = 1;
stack<Cell *> stack;
while (quantidadeVisitados < h * w) {
position = randomDiscoverPath(position);
if (position == NULL) {
position = stack.top();
openRandom(position, shuffle);
stack.pop();
} else if (!position->isVisited()) {
position->setVisited(true);
stack.push(position);
quantidadeVisitados++;
}
}
} // inside
/*
* If the map its odd put middle corridor.
*/
void Map::middle(){
int mid = floor(width / 2.0);
if (width % 2 == 1) {
for (int i = 1; i < height - 1; i++)
map[i][mid]->setCellType(CORRIDOR);
}
}
/*
* Put randoms in the wall inferiors to have path.
*/
void Map::inferiorRandom(){
if (height % 2 == 0) {
vector<Cell *> inferior = visited[visited.size() - 1];
vector<int> shuffle;
int nums = inferior.size();
vector<Direction> directions;
directions.push_back(LEFT);
directions.push_back(RIGHT);
for (int i = 0; i < nums; i++) shuffle.push_back(i);
random_shuffle(shuffle.begin(), shuffle.end());
for (int i = 0; i < (int) shuffle.size() * 0.6; i++) {
Cell * temp = inferior[shuffle[i]]->getDown();
temp->setCellType(CORRIDOR);
openRandom(temp, directions);
}
}
}
/*
* Put randoms in the wall middle to have path.
*/
void Map::middleRandom(){
if ((width - 2) % 4 == 0) {
vector<Cell *> middle;
vector<int> shuffle;
vector<Direction> directions;
directions.push_back(UP);
directions.push_back(DOWN);
for (unsigned int i = 0; i < visited.size(); ++i) {
middle.push_back(visited[i][visited[0].size() - 1]);
}
int nums = middle.size();
for (int i = 0; i < nums; i++) shuffle.push_back(i);
random_shuffle(shuffle.begin(), shuffle.end());
for (int i = 0; i < shuffle.size() * 0.6; ++i) {
Cell * temp = middle[shuffle[i]]->getRight();
temp->setCellType(CORRIDOR);
openRandom(temp, directions);
}
}
}
/*
* If analyze middle will be checking top and bottom if have one connection,
* if analyze inferior will be checking left and right.
*/
void Map::openRandom(Cell * c, vector<Direction> directions){
if (directions.size() > 0) {
random_shuffle(directions.begin(), directions.end());
int i = 0;
if ((directions[i] == UP) && (c->getUp()->getX() >= 1)) {
c->getUp()->setCellType(CORRIDOR);
} else if ((directions[i] == DOWN) && (c->getDown()->getX() <= height - 2)) {
c->getDown()->setCellType(CORRIDOR);
} else if ((directions[i] == LEFT) && (c->getLeft()->getY() >= 1)) {
c->getLeft()->setCellType(CORRIDOR);
}
}
}
vector<Direction> Map::getLegalActions(Cell * c){
vector<Direction> legalActions;
if (!c->getUp()->isWall()) legalActions.push_back(UP);
if (!c->getDown()->isWall()) legalActions.push_back(DOWN);
if (!c->getLeft()->isWall()) legalActions.push_back(LEFT);
if (!c->getRight()->isWall()) legalActions.push_back(RIGHT);
return legalActions;
}
Map Map::generateSuccessor(CellType agent, Direction action){
Map other(*this);
other.setPosition(agent, getNextState(getPosition(agent), action));
if (agent == PLAYER) {
other.scorePlayer -= 1;
} else {
other.scoreEnemy -= 1;
}
if (other.getPosition(agent)->hasFood()) other.eat(agent);
return other;
}
Cell * Map::getNextState(Cell * cell, Direction direction){
if (direction == UP) return cell->getUp();
else if (direction == DOWN) return cell->getDown();
else if (direction == LEFT) return cell->getLeft();
else return cell->getRight();
}
/*
* Logic the mirror the matriz.
*/
void Map::mirror(){
for (int i = 0; i <= height - 1; i++)
for (int j = floor(width / 2.0) - 1; j >= 0; j--) {
Cell * cell = map[i][j];
if (cell->isCorridor()) {
map[i].push_back(new Cell(i, width - j - 1, CORRIDOR));
} else if (cell->isWall()) {
map[i].push_back(new Cell(i, width - j - 1, WALL));
}
}
}
/*
* Function to analyze if the pointer its inside the matriz.
*/
bool Map::insideCondition(unsigned int x, unsigned int y){
return (x >= 0 && x < (visited.size())) && (y >= 0 && y < visited[0].size());
}
/*
* Receive one text path and store in the variable map.
*/
void Map::getMapFromFile(char * fname){
string s = "";
string saux;
ifstream in(fname); // Open for reading
while (getline(in, saux)) {
if (s == "") width = saux.size();
s += saux;
}
height = s.size() / width;
int w = 0;
for (int i = 0; i < height; i++) {
vector<Cell *> aux;
for (int j = 0; j < width; j++) {
if (s[w] == '0') {
aux.push_back(new Cell(i, j, WALL));
} else if (s[w] == '.') {
aux.push_back(new Cell(i, j, CORRIDOR));
}
w++;
}
map.push_back(aux);
}
}
vector<Cell *> Map::getFood(){
vector<Cell *> listCell;
for (unsigned int i = 0; i < map.size(); i++)
for (unsigned int j = 0; j < map[i].size(); j++)
if (map[i][j]->hasFood())
listCell.push_back(map[i][j]);
return listCell;
}
vector<Cell *> Map::getCandidateFood(){
vector<Cell *> listCell;
vector<Cell *> foods = getFood();
for (unsigned int i = 0; i < foods.size(); i++)
if (isCandidate(foods[i]))
listCell.push_back(foods[i]);
return listCell;
}
bool Map::isCandidate(Cell * c){
if (c->getUp()->isCorridor()) return true;
else if (c->getDown()->isCorridor()) return true;
else if (c->getLeft()->isCorridor()) return true;
else if (c->getRight()->isCorridor()) return true;
return false;
}
int Map::getScore(CellType agent){
return (agent == PLAYER) ? scorePlayer : scoreEnemy;
}
Cell * Map::getPosition(CellType agent){
if (agent == PLAYER) {
return player;
} else if (agent == ENEMY) {
return enemy;
} else {
return bullet;
}
}
void Map::setBulletEnable(bool enable){ bulletEnable = enable; }
bool Map::getBulletEnable(){ return bulletEnable; }
bool Map::isInInitialPosition(CellType agent){
return getInitPosition(agent) == getPosition(agent);
}
int Map::getFoodRemaining(){ return totalFood; }
bool Map::hasFood(){ return totalFood != 0; }
void Map::eat(CellType agent){
incrementScore(agent);
totalFood -= 1;
}
void Map::incrementScore(CellType agent){
int multiplier = 10;
if (agent == PLAYER) {
scorePlayer += 1 * multiplier;
} else {
scoreEnemy += 1 * multiplier;
}
}
void Map::crash(CellType agent){
int multiplier = 10;
if (agent == PLAYER && !isInInitialPosition(ENEMY)) {
scorePlayer += 0.5 * multiplier;
scoreEnemy -= 1 * multiplier;
} else if (agent == ENEMY && !isInInitialPosition(PLAYER)) {
scoreEnemy += 2 * multiplier;
scorePlayer -= 1 * multiplier;
}
}
void Map::getStateRepresentation(Map currentState, Map lastState){
// 2 x 5 x H x W
vector<vector<vector<vector<int> > > > all;
vector<vector<vector<int> > > a1;
vector<vector<int> > a2;
all.push_back(a1);
all.push_back(a1);
all[0].push_back(a2); // WALL
all[0].push_back(a2); // FOOD
all[0].push_back(a2); // PLAYER
all[0].push_back(a2); // ENEMY
all[0].push_back(a2); // BULLET
all[1].push_back(a2); // WALL
all[1].push_back(a2); // FOOD
all[1].push_back(a2); // PLAYER
all[1].push_back(a2); // ENEMY
all[1].push_back(a2); // BULLET
populateStateRepresentation(all, 0, currentState);
populateStateRepresentation(all, 1, lastState);
printState(all);
} // getStateRepresentation
void Map::populateStateRepresentation(vector<vector<vector<vector<int> > > > &all, int tensor, Map state){
vector<vector<Cell *> > map = state.getMap();
vector<int> a3;
for (unsigned int i = 0; i < map.size(); i++) {
all[tensor][0].push_back(a3);
all[tensor][1].push_back(a3);
all[tensor][2].push_back(a3);
all[tensor][3].push_back(a3);
all[tensor][4].push_back(a3);
for (unsigned int j = 0; j < map[i].size(); j++) {
Cell * c = map[i][j];
all[tensor][0][i].push_back(0);
all[tensor][1][i].push_back(0);
all[tensor][2][i].push_back(0);
all[tensor][3][i].push_back(0);
all[tensor][4][i].push_back(0);
switch (c->getType()) {
case WALL:
all[tensor][0][i].pop_back();
all[tensor][0][i].push_back(1);
break;
case CORRIDOR:
break;
case FOOD:
all[tensor][1][i].pop_back();
all[tensor][1][i].push_back(1);
break;
case PLAYER:
all[tensor][2][i].pop_back();
all[tensor][2][i].push_back(1);
break;
case ENEMY:
all[tensor][3][i].pop_back();
all[tensor][3][i].push_back(1);
break;
case BULLET:
all[tensor][4][i].pop_back();
all[tensor][4][i].push_back(1);
break;
}
}
}
} // populateStateRepresentation
void Map::printState(vector<vector<vector<vector<int> > > > all){
for (unsigned int i = 0; i < all.size(); i++) {
for (unsigned int j = 0; j < all[0].size(); j++) {
for (unsigned int q = 0; q < all[0][0].size(); q++) {
for (unsigned int w = 0; w < all[0][0][0].size(); w++) {
if (all[i][j][q][w] == 0) std::cout << "0 ";
else std::cout << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
std::cout << std::endl;
}
}