-
Notifications
You must be signed in to change notification settings - Fork 2
/
map.cpp
70 lines (65 loc) · 1.66 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
#include "map.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
using namespace std;
Map::Map()
{
}
void Map::setMap(const char *mapString)
{
ifstream is(mapString);
if(!is.is_open())
return;
string widthHeight;
getline(is, widthHeight);
int width = 0;
int height = 0;
sscanf(widthHeight.c_str(), "%d %d\n",&width, &height);
m_nodes.allocateNodes(width, height);
for(int y = 0; y < height; y++){
string tmp;
getline(is, tmp);
for(int x = 0; x < width; x++){
char c = tmp.at(x);
if(c == ' '){
NodeSavingF* node = m_nodes.getNode(x, y);
assert(node);
node->init(x,y,false);
}
else if(c == '*'){
NodeSavingF* node = m_nodes.getNode(x, y);
assert(node);
node->init(x,y,true);
}
else{
assert(0);
}
}
}
is.close();
}
void Map::dumpMap(ofstream& os, const std::vector<const NodeSavingF*>& path)
{
for(std::vector<const NodeSavingF*>::const_iterator it = path.begin(); it != path.end(); ++it){
os << "(" <<(*it)->getX() << "," << (*it)->getY() << ")->";
}
os << endl;
for(int y = 0; y < m_nodes.getHeight(); y++){
for(int x = 0; x < m_nodes.getWidth(); x++){
const NodeSavingF* node = getNode(x,y);
if(node->getParent() != NULL){
os << "-";
}
else if(node->isBlock()){
os << "*";
}
else{
os << " ";
}
}
os << endl;
}
}