テキストベースのアドベンチャー ゲームを作成していますが、実際に武器オブジェクトをプレイヤーに割り当てる方法について少し混乱しています。というテキスト ファイルから武器を入力していますweapons.txt
。
ID: 1.
Weapon Name: Katana.
Damage: 20.
Weight: 6.
ID: 2.
Weapon Name: Longsword.
Damage: 17.
Weight: 9.
ID: 3.
Weapon Name: WarAxe.
Damage: 22.
Weight: 20.
ID: 4.
Weapon Name: Staff.
Damage: 9.
Weight: 6.
そして、私はプレイヤーと武器のクラスを書きました:
#include "Weapons.h"
#include <iostream>
#include <string>
using namespace std;
DoublyLinkedList<Weapons> weaponsList;
DoublyLinkedListIterator<Weapons> itr = weaponsList.getIterator();
Weapons :: Weapons() {
this->weaponID = 0;
this->weaponName = "";
this->damage = 0;
this->weight = 0;
}
Weapons :: Weapons(int weaponID,string weaponName,int damage,int weight) {
this->weaponID = weaponID;
this->weaponName = weaponName;
this->damage = damage;
this->weight = weight;
}
int Weapons :: getWeaponID() {
return weaponID;
}
string Weapons :: getWeaponName() {
return weaponName;
}
void Weapons::setWeaponName(string weaponName) {
this->weaponName = weaponName;
}
int Weapons :: getDamage() {
return damage;
}
int Weapons :: getWeight() {
return weight;
}
void Weapons :: getWeapons() {
string fileName = "Weapons\\Weapons.txt";
ifstream infile(fileName);
string garbage;
int id;
string weapon;
int damage;
int weight;
while(infile >> garbage >> id >> garbage >> garbage
>> garbage >> weapon >> garbage >> damage >> garbage
>> garbage >> weight >> garbage) {
cout << "ID: \t\t\t"<< id << "\n";
cout << "Weapon Name: \t\t"<< weapon << "\n";
cout << "Damage: \t\t" << damage <<"\n";
cout << "Weight: \t\t" << weight << "\n";
Weapons w1 (id,weapon,damage,weight);
weaponsList.Append(w1);
}
}
void Weapons :: printWeapons() {
int index = 0;
//Loop through the iterator.
for(itr.Start();itr.Valid();itr.Forth()) {
index++;
cout << "------------------Weapon------------------\n";
cout << "--------------------------------\n";
cout << "Position:\t\t" << index << "\n";
cout << "--------------------------------\n";
cout << "Weapon ID:\t\t" << itr.Item().getWeaponID() << "\n";
cout << "Weapon Name:\t\t" << itr.Item().getWeaponName() << "\n";
cout << "Damage:\t\t\t" << itr.Item().getDamage() << "\n";
cout << "Weight:\t\t\t" << itr.Item().getWeight() << "\n";
cout << "------------------------------------------\n";
}
cout << "Weapons: \t\t" << weaponsList.getCount() << "\n";
}
#include "Player.h"
#include "Validators.h"
#include <iostream>
Validators validators;
//--------------------------------------------------------------------------------------
// Name: Default Constructor.
//--------------------------------------------------------------------------------------
Player :: Player() {
this->firstName = "";
this->secondName = "";
this->level = 0;
this->experience = 0;
this->strength = 0;
this->weapons = weapons;
}
//--------------------------------------------------------------------------------------
// Name: Full Constructor.
//--------------------------------------------------------------------------------------
Player :: Player(string firstName,string secondName,int level,int experience,int strength,Weapons weapons) {
this->firstName = firstName;
this->secondName = secondName;
this->level = level;
this->experience = experience;
this->strength = strength;
this->weapons = weapons;
}
//--------------------------------------------------------------------------------------
// Name: Getters.
// Description: Makes you able to access the member variables of the player class.
//--------------------------------------------------------------------------------------
string Player :: getFirstName() {
return firstName;
}
void Player:: setFirstName(string firstName) {
this->firstName = firstName;
}
string Player :: getSecondName() {
return secondName;
}
int Player :: getLevel() {
return level;
}
int Player :: getExperience() {
return experience;
}
int Player :: getStrength() {
return strength;
}
void Player :: playerCreation() {
string inputFirstName = "Please enter your first name: ";
firstName = validators.getString(inputFirstName);
string inputSecondName = "Please enter your second name: ";
secondName = validators.getString(inputSecondName);
level = 0;
experience = 0;
strength = 8;
weapons.getWeaponName();
}
void Player::loadPlayer() {
//string fileName = "Players\\" + getFirstName()+ getSecondName() + ".txt";
string fileName = "Players\\ConorPendlebury.txt";
ifstream infile(fileName);
string garbage;
string loadFirstName;
string loadLastName;
int loadLevel;
int loadExperience;
int loadStrength;
Weapons weapons;
while(infile>>garbage,infile>>garbage,infile>>loadFirstName,infile>>garbage,
infile>>garbage,infile>>loadLastName,infile>>garbage,infile>>loadLevel,
infile>>garbage,infile>>loadExperience,infile>>garbage,infile>>loadStrength,infile>>garbage) {
cout << "First Name: \t"<< loadFirstName << "\n";
cout << "Second Name: \t"<< loadLastName << "\n";
cout << "Level: \t\t" << loadLevel <<"\n";
cout << "Experience: \t" << loadExperience << "\n";
cout << "Strength: \t" << loadStrength << "\n";
}
//this->setFirstName(firstName);
}
void Player::savePlayer() {
string fileName = "Players\\" + getFirstName()+ getSecondName() + ".txt";
cout << "File: \t\t" << fileName << " was saved" << endl;
ofstream oFile(fileName);
oFile << "First Name: " << this->getFirstName() << endl;
oFile << "Second Name: " << this->getSecondName() << endl;
oFile << "Level: " << this->getLevel()<< endl;
oFile << "Experience: " << this->getExperience()<< endl;
oFile << "Strength: " << this->getStrength()<< endl;
oFile << "Weapon: " << weapons.getWeaponName();
}
ファイルから武器を取得して Linkedlist に追加することはできますが、ランタイムの前後に特定の武器をプレーヤーに割り当てる方法に行き詰まっています。
誰か助けてください。ありがとう。