-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadMonsterData.cpp
135 lines (132 loc) · 3.22 KB
/
ReadMonsterData.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
#include "ReadMonsterData.h"
#include <iostream>
// Intent: To read file containing data of monster and save into game memory.
// Pre: The file must be in regulated format.
// Post: The function doesn't return anything but saves data into memory in needed format(structure).
vector<MonsterData> readMonsterData(string fileName)
{
//bool for checking whether EOF is read
bool end = false;
//input file
ifstream monsterFile;
monsterFile.open(fileName);
if (!monsterFile.is_open())
{
cout << "Error: " << endl;
cout << "Can't open " << fileName << endl;
exit(1);
}
//input parameters
vector<MonsterData> monsters;
int number;
monsterFile >> number;
string name;
monsterFile >> name;
//while there are still monsters to read in
for (int i = 0; i < number; i++)
{
//temporary monster data for later pushback
MonsterData temp;
//parameter inputting
temp.name = name;
int nHP, nATK, nRange, eHP, eATK, eRange;
monsterFile >> nHP >> nATK >> nRange >> eHP >> eATK >> eRange;
temp.normalLargestHP = nHP;
temp.normalBaseATK = nATK;
temp.normalBaseRange = nRange;
temp.eliteLargestHP = eHP;
temp.eliteBaseATK = eATK;
temp.eliteBaseRange = eRange;
//for checking of next monster
string cardOwner;
monsterFile >> cardOwner;
//if card belongs to previous name
while (cardOwner == name)
{
//input parameters
int number, agility;
monsterFile >> number >> agility;
//temporary card for later pushback
MonsterActCard card;
card.number = number;
card.agility = agility;
//ignore one character for later getline
monsterFile.ignore();
string content;
getline(monsterFile, content);
//throw into stringstream, determine actions
stringstream ss;
ss.clear();
ss << content;
string action;
//while not end of line
while (ss >> action)
{
if (action == "move")
{
string cardMove;
ss >> cardMove;
card.move = cardMove;
}
if (action == "attack")
{
int cardAttack;
ss >> cardAttack;
card.attack = cardAttack;
card.isAttack = true;
}
if (action == "range")
{
int cardRange;
ss >> cardRange;
card.range = cardRange;
card.isRange = true;
}
if (action == "shield")
{
int cardShield;
ss >> cardShield;
card.shield = cardShield;
card.isShield = true;
}
if (action == "heal")
{
int cardHeal;
ss >> cardHeal;
card.heal = cardHeal;
card.isHeal = true;
}
if (action == "r")
{
card.reuse = true;
}
if (action == "d")
{
card.reuse = false;
}
}
//pushback action card
temp.actions.push_back(card);
//check if end of file, if true break
if (!(monsterFile >> cardOwner)) {
end = true;
break;
}
}
//set new name equal to previous input
if (end != true) {
name = cardOwner;
}
sort(temp.actions.begin(), temp.actions.end(), compareMonsterCard);
monsters.push_back(temp);
}
return monsters;
}
/*compareMonsterCard()*/
// Intent: To compare stats for implementation of sort function in readMonsterData().
// Pre: Two monsterActCards for comparision.
// Post: The function return true or false for sorting.
bool compareMonsterCard(MonsterActCard a, MonsterActCard b)
{
return a.number < b.number;
}