-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadCharacterData.cpp
154 lines (151 loc) · 3.38 KB
/
ReadCharacterData.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
146
147
148
149
150
151
152
153
154
#include "ReadCharacterData.h"
#include "CharacterData.h"
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
vector<CharacterData> readCharacterData(string fileName)
{
vector<CharacterData> characters;
//input file
ifstream characterFile;
characterFile.open(fileName);
if (!characterFile.is_open())
{
cout << "Error: " << endl;
cout << "Can't open " << fileName << endl;
exit(1);
}
//parameter creating
int number;
characterFile >> number;
characterFile.clear();
//while there are characters for input
string name;
while (characterFile >> name)
{
//enter parameters
int characterHP, characterCardNumber;
characterFile >> characterHP >> characterCardNumber;
//temporary character data for later input
CharacterData temp;
temp.name = name;
temp.initCards = characterCardNumber;
temp.largestHP = characterHP;
int cardAmount;
characterFile >> cardAmount;
temp.HP = characterHP;
//input card data
for (int i = 0; i < cardAmount; i++)
{
int cardNum, cardAgility;
characterFile >> cardNum >> cardAgility;
//temporary card for later pushback
CharacterActCard card;
card.number = cardNum;
card.agility = cardAgility;
characterFile.ignore();
string content;
getline(characterFile, content);
stringstream ss;
ss.clear();
ss << content;
string action;
bool setRange = false;
//top phase
while (ss >> action)
{
if (action == "attack")
{
int cardAttack;
ss >> cardAttack;
card.topAttack.push_back(cardAttack);
}
if (action == "shield")
{
int cardShield;
ss >> cardShield;
card.topShield.push_back(cardShield);
}
if (action == "move")
{
int cardMove;
ss >> cardMove;
card.topMove.push_back(cardMove);
}
if (action == "heal")
{
int cardHeal;
ss >> cardHeal;
card.topHeal.push_back(cardHeal);
}
if (action == "range")
{
setRange = true;
int cardRange;
ss >> cardRange;
card.topRange = cardRange;
}
if (action == "-")
{
if (setRange == false)
card.topRange = 0;
break;
}
}
setRange = false;
//bottom phase
while (ss >> action)
{
if (action == "attack")
{
int cardAttack;
ss >> cardAttack;
card.botAttack.push_back(cardAttack);
}
if (action == "shield")
{
int cardShield;
ss >> cardShield;
card.botShield.push_back(cardShield);
}
if (action == "move")
{
int cardMove;
ss >> cardMove;
card.botMove.push_back(cardMove);
}
if (action == "heal")
{
int cardHeal;
ss >> cardHeal;
card.botHeal.push_back(cardHeal);
}
if (action == "range")
{
setRange = true;
int cardRange;
ss >> cardRange;
card.botRange = cardRange;
}
}
//pushback card
if (setRange == false)
card.botRange = 0;
temp.actions.push_back(card);
}
//sort card with number
sort(temp.actions.begin(), temp.actions.end(), compareCharacterCard);
//pushback character data
characters.push_back(temp);
}
return characters;
}
/*compareCharacterCard()*/
// Intent: To compare stats for implementation of sort function in readCharacterData().
// Pre: Two characterActCards for comparision.
// Post: The function return true or false for sorting.
bool compareCharacterCard(CharacterActCard a, CharacterActCard b)
{
return a.number < b.number;
}