2

我正在创建一个基于文本的冒险游戏,我对如何将武器对象实际分配给玩家有点困惑。我正在从一个名为的文本文件中输入武器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();
}

我可以从文件中取出武器并将它们添加到链接列表中,但我一直在思考如何在运行时间之前和之后将某些武器分配给玩家。

请问有人可以帮忙吗?谢谢。

4

0 回答 0