#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
class Monster {
public:
int id;
string imagePath;
int hp;
int attack;
int defense;
int exp;
int gold;
Monster(int _id, string _img, int _hp, int _atk, int _def, int _exp, int _gold)
: id(_id), imagePath(_img), hp(_hp), attack(_atk),
defense(_def), exp(_exp), gold(_gold) {
}
void print() const {
cout << "ID:" << id << " | 图片:" << imagePath
<< " | 血量:" << hp << " | 攻击:" << attack
<< " | 防御:" << defense << " | 经验:" << exp
<< " | 金币:" << gold << endl;
}
};
vector<Monster> loadMonsters(const string& filename) {
vector<Monster> monsters;
ifstream file(filename);
if (!file.is_open()) {
cerr << "错误:无法打开文件 " << filename << endl;
return monsters;
}
string line;
getline(file, line);
while (getline(file, line)) {
istringstream iss(line);
int id, hp, atk, def, exp, gold;
string img;
if (iss >> id >> img >> hp >> atk >> def >> exp >> gold) {
monsters.emplace_back(id, img, hp, atk, def, exp, gold);
}
else {
cerr << "警告:解析失败的行: " << line << endl;
}
}
file.close();
return monsters;
}
int main() {
vector<Monster> monsterList = loadMonsters("C:/Users/24203/source/repos/0413/monsters.txt");
cout << "已加载 " << monsterList.size() << " 个怪物数据:" << endl;
for (const auto& m : monsterList) {
m.print();
}
return 0;
}