-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldMap.cpp
More file actions
94 lines (94 loc) · 2.86 KB
/
WorldMap.cpp
File metadata and controls
94 lines (94 loc) · 2.86 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
#include "WorldMap.h"
#include <sstream>
#include <fstream>
#include <vector>
#include <iostream>
WorldMap::~WorldMap()
{
for(auto it=maps.begin();it!=maps.end();it++)
{
delete *it;
}
}
WorldMap& WorldMap::operator=(const WorldMap &wm)
{
maps.assign(wm.maps.begin(),wm.maps.end());
mapNameList.assign(wm.mapNameList.begin(),wm.mapNameList.end());
return *this;
}
bool WorldMap::ReadMapFile(const char filename []) {
std::ifstream fin;
fin.open(filename);
if (!fin){
std::cout << filename << " not exist!" << std::endl;
return false;
}
else{
for (int id=0;fin.peek()!=EOF;id++){
std::string line;
getline(fin, line);
std::istringstream iss(line);
char type;
iss >> type;
std::string name;
iss >> name;
mapNameList.push_back(name);
if(type=='J'){
Jail *j = new Jail(type,id,name,0);
maps.push_back(j);
continue;
}
int cost=0;
iss >> cost;
if(type=='U')
{
int upgradeCost=0;
iss >> upgradeCost;
std::vector<int> fineList;
int fine=0;
while(iss >> fine){
fineList.push_back(fine);
}
Upgradable *upg = new Upgradable(type,id,name,cost,upgradeCost,fineList);
maps.push_back(upg);
}
else if(type=='C'){
int fine=0;
iss >> fine;
Collectable *col = new Collectable(type,id,name,cost,fine);
maps.push_back(col);
}
else{
int fine=0;
iss >> fine;
RandomCost *ran = new RandomCost(type,id,name,cost,fine);
maps.push_back(ran);
}
}
}
fin.close();
return true;
}
void WorldMap::PrintMapFile() const
{
for (std::vector<MapUnit*>::const_iterator it=maps.begin();it!=maps.end();++it)
{
if((*it)->GetType()=='U')
{
std::cout << (*it)->GetType() << " " << (*it)->GetName() << " "<< (*it)->GetCost() << " " << static_cast<Upgradable*>(*it)->GetUpgradeCost();
for (std::vector<int>::const_iterator it_=static_cast<Upgradable*>(*it)->GetFineList().begin();it_!=static_cast<Upgradable*>(*it)->GetFineList().end();++it_)
{
std::cout << " " << *it_;
}
std::cout << std::endl;
}
else if((*it)->GetType()!='J')
{
std::cout << (*it)->GetType() << " " << (*it)->GetName() << " "<< (*it)->GetCost() << " " << (*it)->GetFine() << std::endl;
}
else
{
std::cout << (*it)->GetType() << " " << (*it)->GetName() << std::endl;
}
}
}