-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainerCenter.cpp
145 lines (115 loc) · 3.08 KB
/
TrainerCenter.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
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
#include "TrainerCenter.h"
TrainerCenter::TrainerCenter(const std::string& inputFileName)
{
name=inputFileName.c_str();
ifstream file;
string line,trainername,arena,pokename,power,num;
int id,index,index2,trainerNum,pokeNum,delim,delim2;
Trainer* trainer;
Pokemon* pokemon;
vector<Pokemon *> pokemons;
file.open(name);
if(file.is_open())
{
getline(file,line);
index=line.find("Trainer Count:")+14;
trainerNum=stoi(num.assign(line,index,line.length()-index));
getline(file,line);
index=line.find("Pokemon Count:")+14;
pokeNum=stoi(num.assign(line,index,line.length()-index));
for(int i=0; i<trainerNum && !file.eof(); i++)
{
getline(file,line);
if(line=="")//Empty line skip
{
getline(file,line);
}
delim=line.find("--");
index=delim+2;
delim2=line.rfind("--");
index2=delim2+2;
id=stoi(num.assign(line,0,delim));
trainername.assign(line,index,delim2-index);
arena.assign(line,index2,line.length()-index2);
for(int j=0; j<pokeNum; j++)
{
getline(file,line);
delim=line.find("::");
index=delim+2;
delim2=line.rfind("::");
index2=delim2+2;
id=stoi(num.assign(line,0,delim));
pokename.assign(line,index,delim2-index);
power.assign(line,index2,line.length()-index2);
if(power=="FIRE" || power=="Fire")
{
pokemon=new PokeFire(id,pokename);
}
else if(power=="WATER" || power=="Water")
{
pokemon=new PokeWater(id,pokename);
}
else if(power=="ELECTRIC" || power=="Electric")
{
pokemon=new PokeElectric(id,pokename);
}
else if(power=="GRASS" || power=="Grass")
{
pokemon=new PokeGrass(id,pokename);
}
else if(power=="FLYING" || power=="Flying")
{
pokemon=new PokeFlying(id,pokename);
}
pokemons.push_back(pokemon);
}
trainer=new Trainer(id,trainername,convertArena(arena),pokemons);
trainerlst.push_back(trainer);
pokemons.clear();
}
}
file.close();
}
std::vector<Trainer*>& TrainerCenter::getAllTrainers()
{
return trainerlst;
}
Trainer* TrainerCenter::getTrainerWithID(int trainerID)
{
vector<Trainer*>::iterator it;
for(it=trainerlst.begin(); it!=trainerlst.end(); it++)
{
if((*it)->getTrainerID()==trainerID)
return *it;
}
return NULL;
}
int TrainerCenter::stoi(string s)
{
int i, number=0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; i++)
number = 10 * number + (s[i]-'0');
return number;
}
Arena TrainerCenter::convertArena(string arena)
{
if(arena=="Stadium")
return STADIUM;
else if(arena=="Magma")
return MAGMA;
else if(arena=="Ocean")
return OCEAN;
else if(arena=="ElectriCity")
return ELECTRICITY;
else if(arena=="Forest")
return FOREST;
else if(arena=="Sky")
return SKY;
}
TrainerCenter::~TrainerCenter()
{
vector<Trainer *>::iterator it;
for(it=trainerlst.begin(); it!=trainerlst.end(); it++)
delete (*it);
trainerlst.clear();
}