-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.cpp
More file actions
146 lines (124 loc) · 3.64 KB
/
map.cpp
File metadata and controls
146 lines (124 loc) · 3.64 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
#include "map.h"
Map::Map()
{
width=-1;
height=-1;
startx=-1;
starty=-1;
finishx=-1;
finishy=-1;
obstacles.clear();
}
bool Map::parseMap(TiXmlElement *elem, QString& error)
{
TiXmlElement *subelem=elem->FirstChildElement("width");
if(!subelem)
{
error=QString::fromUtf8("Width tag not found");
return false;
}
width=QString(subelem->GetText()).toInt();
subelem=elem->FirstChildElement("height");
if(!subelem)
{
error=QString::fromUtf8("Height tag not found");
return false;
}
height=QString(subelem->GetText()).toInt();
subelem=elem->FirstChildElement("startx");
if(!subelem)
{
error=QString::fromUtf8("Startx tag not found");
return false;
}
startx=QString(subelem->GetText()).toInt();
subelem=elem->FirstChildElement("starty");
if(!subelem)
{
error=QString::fromUtf8("Starty tag not found");
return false;
}
starty=QString(subelem->GetText()).toInt();
subelem=elem->FirstChildElement("finishx");
if(!subelem)
{
error=QString::fromUtf8("Finishx tag not found");
return false;
}
finishx=QString(subelem->GetText()).toInt();
subelem=elem->FirstChildElement("finishy");
if(!subelem)
{
error=QString::fromUtf8("Finishy tag not found");
return false;
}
finishy=QString(subelem->GetText()).toInt();
subelem=elem->FirstChildElement("grid");
if(!subelem)
{
error=QString::fromUtf8("Grid tag not found");
return false;
}
TiXmlElement *row=subelem->FirstChildElement();
if(!row)
{
error=QString::fromUtf8("Incorrect grid rows");
return false;
}
int rownumber=0;
while(row)
{
QStringList rowelements=QString(row->GetText()).split(" ");
if(rowelements.count()!=width)
{
error=QString::fromUtf8("Length of row #%1 is incorrect").arg(QString::number(rownumber));
return false;
}
if(rowelements.contains(QString("1")))
{
int colnumber=0;
for(QStringList::iterator iter=rowelements.begin();iter!=rowelements.end();iter++,colnumber++)
if(iter->compare(QString("1"))==0)
obstacles.push_back(qMakePair(rownumber, colnumber));
}
rownumber++;
row=row->NextSiblingElement();
}
if(rownumber!=height)
{
error=QString::fromUtf8("Number of rows and height are not equal");
return false;
}
return true;
}
QGraphicsScene* Map::drawclearmap(Colors *curcolors)
{
QGraphicsScene *myscene=new QGraphicsScene();
for(int i=0;i<=width;i++)
myscene->addLine(i*cellsize, 0, i*cellsize, height*cellsize);
for(int i=0;i<=height;i++)
myscene->addLine(0, i*cellsize, width*cellsize, i*cellsize);
for(int i=0;i<obstacles.size();i++)
{
myscene->addRect(obstacles[i].second*cellsize, obstacles[i].first*cellsize, cellsize, cellsize, QPen(QColor("black")), curcolors->obst);
}
myscene->addRect(startx*cellsize, starty*cellsize, cellsize, cellsize, QPen(QColor("black")), curcolors->start);
myscene->addRect(finishx*cellsize, finishy*cellsize, cellsize, cellsize, QPen(QColor("black")), curcolors->goal);
return myscene;
}
void Map::setcellsize(int size)
{
cellsize=size;
}
int Map::getcellsize()
{
return cellsize;
}
int Map::getmapwidth()
{
return cellsize*width;
}
int Map::getmapheight()
{
return cellsize*height;
}