1

我正在创建某种 rpg 战斗,程序从 .txt 文件中读取输入。我创建了代码,但是当我想开始战斗时,它给了我一个错误向量下标超出范围。谁能帮我解决这个问题?非常感谢:) 这是代码。我包含了所有内容,以便您可以获得完整的上下文,但我认为主要问题在于主 cpp 中的 while 循环,如果您只想跳到那里。

所以我们在同一条轨道上,lamanite txt 文件的内容(命中点和再生点)是

8 2

7 3

6 1

为 nephite

10 3

12 4

11 5

这是我的战士.h 文件

#pragma once
#include <string>

using namespace std;

class warrior
{
public:

    warrior ();
    warrior (int h, int r);
    int getDamage() const;
    void takeDamage(int damage);
    int getCurrentHP() const;
    void regenerate();
    string tostring(int h, int r);

private:
    int HitPoints;
    int RegPoints;
    int damage;


};

这是我的勇士cpp

#include "warrior.h"
#include <string>
#include <iostream>

warrior::warrior(int h, int r)
{
    HitPoints = h;
    RegPoints = r;
}

int warrior::getDamage() const
{
    int damage = rand () % HitPoints;
    return damage;
}

void warrior::takeDamage(int damage)
{
    HitPoints = HitPoints - damage;
}

int warrior::getCurrentHP() const
{
    return HitPoints;
}

void warrior::regenerate() 
{
    HitPoints = HitPoints + rand () % (RegPoints);
}

string warrior::tostring(int h, int r)
{
    return 0;
}

我的主文件

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <ctime>

#include "warrior.h"

using namespace std;

void main ()
{
    srand(time(0));
    ifstream input1;
    cout << "input file name nephite: ";
    string filename;
    cin >> filename;

    input1.open(filename);

    int HP1, RP1;
    vector <warrior*> nephites;

    while (input1 >> HP1 >> RP1)
    {
        nephites.push_back(new warrior(HP1, RP1));
    }

    cout << nephites.size() << endl;

    ifstream input2;
    cout << "input file name lamanite : ";
    string filename2;
    cin >> filename2;

    input2.open(filename2);
    int HP2, RP2;
    vector <warrior*> lamanites;

    while (input2 >> HP2 >> RP2)
    {
        lamanites.push_back(new warrior(HP2, RP2));
    }
    cout << lamanites.size() << endl;

    cout << endl << "Battle" << endl;

    warrior nephitesw  = warrior (HP1,RP1);
    warrior lamanitesw = warrior (HP2,RP2);

    while ((nephites.size() > 0) && (lamanites.size() > 0))
    {

        int rN = rand () % nephites.size();
        int rL = rand () % lamanites.size();
        cout << rN << "xx" << rL << endl; // so that i know what rN and rL is

        while((nephites[rN]->getCurrentHP() > 0) && (lamanites[rL]->getCurrentHP() > 0)) // the program can't execute this part of the code
        {
            nephites[rN]->takeDamage(lamanites[rL]->getDamage());
            lamanites[rL]->takeDamage(nephites[rN]->getDamage());

            if(lamanites[rL]->getCurrentHP() > 0)
            {
                lamanites[rL]->regenerate();
            }
            else
            {
                lamanites.erase(lamanites.begin() + (rL));
            } 

            if(nephites[rN]->getCurrentHP() > 0) 
            {
                nephites[rN]->regenerate();
            } 
            else
            {
                nephites.erase(nephites.begin() + (rN));
            }
        }

        cout << "NEP HP: " << nephites[rN]->getCurrentHP() << " " << "LAM HP: " << lamanites[rL]->getCurrentHP() << endl;
    }

    system ("Pause");
}
4

2 回答 2

3

您有一个while循环来测试nephites[rN] and的某些属性lamanites[rL]

while((nephites[rN]->getCurrentHP() > 0) && (lamanites[rL]->getCurrentHP() > 0)
{
    // ...
}

但在该循环中,您可能会删除这些元素:

{lamanites.erase(lamanites.begin() + (rL));} 

// ...

{nephites.erase(nephites.begin() + (rN));}  

至少,在其中一个擦除操作之后,您将在下一次循环迭代(可能是也可能不是您想要的)中测试不同的nephite 或 lamanite 对象,但是如果您已经擦除了最后一个元素容器,你有索引现在超出范围的问题。

于 2013-06-03T21:32:16.080 回答
2

您正在循环直到nephites[rN]->getCurrentHP() <= 0lamanites[rL]->getCurrentHP() <= 0。但是,无论哪个先掉到0第一个都将从vector

// ...
{lamanites.erase(lamanites.begin() + (rL));} 

// ...            
{nephites.erase(nephites.begin() + (rN));}

如果rN == nephites.size()rN == lamanites.size()(当大小为时肯定会发生1并且可能更早随机发生),这将导致您在vector测试循环时将索引排除在外。

要快速解决问题,请将删除warrior(s)的代码vector移出循环:

while((nephites[rN]->getCurrentHP() > 0) && (lamanites[rL]->getCurrentHP() > 0))
{
    nephites[rN]->takeDamage(lamanites[rL]->getDamage());
    lamanites[rL]->takeDamage(nephites[rN]->getDamage());

    if(lamanites[rL]->getCurrentHP() > 0)
    {
        lamanites[rL]->regenerate();
    }

    if(nephites[rN]->getCurrentHP() > 0) 
    {
        nephites[rN]->regenerate();
    } 
}

cout << "NEP HP: " << nephites[rN]->getCurrentHP() << " " << "LAM HP: " << lamanites[rL]->getCurrentHP() << endl;

// *****
// Move the erasures out of the loop
// *****

if(lamanites[rL]->getCurrentHP() <= 0)
{
    lamanites.erase(lamanites.begin() + (rL));
} 

if(nephites[rN]->getCurrentHP() <= 0)
{
    nephites.erase(nephites.begin() + (rN));
}
于 2013-06-03T21:33:13.117 回答