-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.cpp
113 lines (109 loc) · 4.48 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
#include "map.h"
CMap::CMap(int _h, int _w, int _rad, int _plot, int _gold)
{
height = _h;
width = _w;
numberOfCells = 0;
map = new int*[width];
for(int i = 0; i <width; i++)
map[i] = new int[height];
index = new int*[width];
for(int i = 0; i <width; i++)
index[i] = new int[height];
reversePos.clear();
cells.clear();
activeCells.clear();
forwardPos.clear();
for(int i = 0; i <width; i++)
for(int j = 0; j < height; j++)
{
map[i][j] = (i+j)%3;
if(map[i][j])
{
index[i][j] = numberOfCells;
cells[numberOfCells].owner = -1;
cells[numberOfCells].mineType = Neutral;
cells[numberOfCells].value = 0;
reversePos[numberOfCells] = QPair<int,int>(i,j);
forwardPos[QPair<int,int>(i,j)] = numberOfCells;
activeCells.append(QPair<int,int>(i,j));
numberOfCells++;
}
}
links.clear();
{
QSet< QPair<int, int> > mySet;
mySet.clear();
for(QMap<int, QPair<int, int> >::iterator it = reversePos.begin(); it!=reversePos.end(); ++it)
{
if(activeCells.contains(QPair<int, int>(it->first+1, it->second)))
mySet.insert(QPair<int, int>(it.key(), forwardPos[QPair<int,int>(it->first+1,it->second)]));
if(it->first%2 && activeCells.contains(QPair<int, int>(it->first+1, it->second+1)))
mySet.insert(QPair<int, int>(it.key(), forwardPos[QPair<int,int>(it->first+1,it->second+1)]));
if(!it->first%2 && activeCells.contains(QPair<int, int>(it->first+1, it->second-1)))
mySet.insert(QPair<int, int>(it.key(), forwardPos[QPair<int,int>(it->first+1,it->second-1)]));
if(activeCells.contains(QPair<int, int>(it->first, it->second+1)))
mySet.insert(QPair<int, int>(it.key(), forwardPos[QPair<int,int>(it->first,it->second+1)]));
if(activeCells.contains(QPair<int, int>(it->first, it->second-1)))
mySet.insert(QPair<int, int>(it.key(), forwardPos[QPair<int,int>(it->first,it->second-1)]));
if(activeCells.contains(QPair<int, int>(it->first-1, it->second)))
mySet.insert(QPair<int, int>(it.key(), forwardPos[QPair<int,int>(it->first-1,it->second)]));
if(it->first%2 && activeCells.contains(QPair<int, int>(it->first-1, it->second+1)))
mySet.insert(QPair<int, int>(it.key(), forwardPos[QPair<int,int>(it->first-1,it->second+1)]));
if(!it->first%2 && activeCells.contains(QPair<int, int>(it->first-1, it->second-1)))
mySet.insert(QPair<int, int>(it.key(), forwardPos[QPair<int,int>(it->first-1,it->second-1)]));
}
for(QSet< QPair<int, int> >::iterator it = mySet.begin(); it != mySet.end(); ++it)
if(!links.contains(QPair<int, int>(it->second, it->first)))
links.append(*it);
}
while(_rad && checkCells())
{
int selected = rand()%(height*width);
if( cells.count(selected) && cells[selected].mineType == Neutral)
{
int toAdd = std::min(_rad, rand()%6+1);
_rad -= toAdd;
cells[selected].mineType = Rhodium;
cells[selected].value = toAdd;
}
qDebug() << "Rad : " << _rad << selected;
}
while(_plot && checkCells())
{
int selected = rand()%(height*width);
if( cells.count(selected) && cells[selected].mineType == Neutral)
{
int toAdd = std::min(_plot, rand()%6+1);
_plot -= toAdd;
cells[selected].mineType = Platinum;
cells[selected].value = toAdd;
}
qDebug() << "Plat : " << _plot<< selected;
}
while(_gold && checkCells())
{
int selected = rand()%(height*width);
if( cells.count(selected) && cells[selected].mineType == Neutral)
{
int toAdd = std::min(_gold, rand()%6+1);
_gold -= toAdd;
cells[selected].mineType = Gold;
cells[selected].value = toAdd;
}
qDebug() << "Gold : " << _gold << selected;
}
error = "";
if(!checkCells())
error = "Could'nt fit all the resources in the map !";
}
CMap::~CMap()
{
}
bool CMap::checkCells(bool player)
{
for(QMap<int, cell>::iterator it = cells.begin(); it != cells.end(); ++it)
if(it->mineType == Neutral && (player?(it->owner == -1):true))
return true;
return false;
}