-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapgenerator.cpp
54 lines (48 loc) · 1.56 KB
/
mapgenerator.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
/*
Copyright (C) 2016 Meritxell Jordana
Copyright (C) 2016 Marc Sanchez
*/
#include "mapgenerator.h"
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
MapGenerator::MapGenerator(const int nRows, const int nCols) :
map(Map(nRows - ((nRows % 2 == 0) ? 1 : 0),
nCols / 2 + nCols % 2)), nRows(nRows), nCols(nCols) {
generate();
}
Map MapGenerator::getMap() {
Map m = Map(nRows, nCols, Position(1, 1), Position(1, nCols - 2));
m.copySubMap(map, Position(0, 0));
m.setSubColCellType(map.getNumberOfCols() - 1,
1, map.getNumberOfRows() - 1, Food);
m.copySymmetricLeftToRight();
m.initGame();
return m;
}
void MapGenerator::generate() {
srand(time(NULL));
set<Position> visited;
stack<Position> stk;
Position *current = new Position(1, 1);
stk.push(*current);
visited.insert(*current);
while(!stk.empty()) {
list<Position> unvisitedNeighbors = map.getUnvisitedNeighbors(*current, visited);
if (unvisitedNeighbors.empty()) {
current = new Position(stk.top());
list<Position> neighbors = map.getNeighbors(*current);
Position p = map.getRandomPositionOfList(neighbors);
map.removeWall(*current, p);
stk.pop();
} else {
Position *neighbor = new Position(map.getRandomPositionOfList(unvisitedNeighbors));
map.removeWall(*current, *neighbor);
delete(current);
current = neighbor;
visited.insert(*current);
stk.push(*current);
}
}
}