C++ 32767基于文本的游戏

问题描述:

帮助,每当我运行我的游戏,每当我调用武器的display()函数时,它总是以32767的形式返回!我该如何做到这一点,以便在调用显示功能时,将该武器电源打印到控制台上?提前致谢!C++ 32767基于文本的游戏


//a SETIsoft game 

#include <fstream> 
#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <string> 
using namespace std; 

// GLOBAL VARIABLES 
string charname; 

//NAV CLASS 
class nav 
{ 
public: 
    int navatk; 
    int navdef; 
    int navspd; 
    int navluk; 
    int navhpo; 
    int navepo; 

//CLASS MEMBER FUNCTIONS 
    void INIT(int navattack, int navdefense, int navspeed, int navhp, int navluck, int navenergy); 
    void display(); 
}; 
//NAV MEMBER FUNCTION DEFINITIONS 
void nav::INIT(int navattack, int navdefense, int navspeed, int navhp, int navluck, int navenergy) 
{ 
    navatk = navattack; 
    navdef = navdefense; 
    navspd = navspeed; 
    navhpo = navhp; 
    navluk = navluck; 
    navepo = navenergy; 

} 
void nav::display() 
{ 
    cout<<"attack: "<< navatk <<endl; 
    cout<<"defense: "<< navdef <<endl; 
    cout<<"speed: "<< navspd <<endl; 
    cout<<"health points: "<< navhpo <<endl; 
    cout<<"luck: "<< navluk <<endl; 
    cout<<"energy points: "<< navepo <<endl; 

} 
//BUG CLASS 
class bug 
{ 
public: 
    int bugatk; 
    int bugdef; 
    int bugluk; 
    int bugspd; 
    int bughpo; 
    int bugepo; 

//CLASS MEMBER FUNCTIONS 
    void INIT(int bugattack, int bugdefense, int bugspeed, int bughp, int bugluck, int bugenergy); 
    void display(); 
}; 
//BUG MEMBER FUNCTION DEFINITIONS 

void bug::INIT(int bugattack, int bugdefense, int bugspeed, int bughp, int bugluck, int bugenergy) 
{ 
    bugatk = bugattack; 
    bugdef = bugdefense; 
    bugspd = bugspeed; 
    bughpo = bughp; 
    bugluk = bugluck; 
    bugepo = bugenergy; 

} 

void bug::display() 
{ 
    cout<<"attack: "<< bugatk <<endl; 
    cout<<"defense: "<< bugdef <<endl; 
    cout<<"speed: "<< bugspd <<endl; 
    cout<<"health points: "<< bughpo <<endl; 
    cout<<"luck: "<< bugluk <<endl; 
    cout<<"energy points: "<< bugepo <<endl;  
} 

//WEAPON CLASS 
class userweap 
{ 
public:  
    int id; 
    int power; 
    int cost; 
    string descrip; 

//CLASS MEMBER FUNCTIONS 
    void INIT(int id, int weappower, int weapcost); 
    void display(); 
    string equip(); 
}; 
//CLASS MEMEBER FUNCTION DEFINITIONS 

void userweap::INIT(int id, int weappower, int weapcost) 
{ 
    weappower = power; 
    weapcost = cost; 
} 
void userweap::display() 
{ 
    cout<<"power: " << power <<endl; 

} 
string userweap::equip() 
{ 
    string equipped; 
    equipped = id; 
    return equipped; 
} 
//GLOBAL HELPER FUNCTIONS 
void intro(); 
string choice(nav & user); 
void btutorial(); 
string allgood(); 
void checkstats(bug & simbug, nav & user); 
void simbugmake(bug & simbug); 
void regcannonmake(userweap & regcannonweap); 
void beamswordmake(userweap & beamsword); 
void laserbowmake(userweap & laserbow); 
void flyingfistmake(userweap & flyingfist); 
void razordiscmake(userweap & razordisc); 
void blockpunchmake(userweap & blockpunch); 
void flaremake(userweap & flare); 
void wavemake(userweap & wave); 
void whipmake(userweap & whip); 
void mindshotmake(userweap & mindshot); 
void weapons(userweap & regcannon); 

//MAIN FUNCTION 
int main(){ 
    nav dummynav; 
    userweap dummyweap; 
    intro(); 
    while(choice(dummynav) == "no") cout<< "let me show you them again" << endl; 
    btutorial(); 
    while(allgood() == "no"); 
    sleep(1); 
    weapons(dummyweap); 
    return 0; 
} 

void intro() 
{ 
    cout<<"what's your name again?"<<endl; 
    cin>>charname; 
    cout<<"hello, "<<charname<<endl; 
    sleep(2); 
    cout<<"welcome to the pentagon"<<endl; 
    sleep(1); 
    cout<<"year: 2050 C.E." << endl; 
    sleep(1); 
    cout<<"you have been nominated to control a 'navigator.' a digital fighting warrior that we use to fight the rebellion."<<endl; 
    sleep(1); 
    cout<<"..."<<endl; 
    sleep(1); 
    cout<<"what's this? you've never heard of the rebellion before?"<<endl; 
    sleep(1); 
    cout<<"the year was 2015, when the citizens of the united states of america rose up against the government. little did they know, the government had installed a top-secret project called 'SEVEN' (or s.ecretly e.liminate v.iolent e.lectronic n.ews) into the world wide web . they used this successfully to break up riots by intercepting internet-bound messages. soon after, the rebellion's top scientists figured out the programs weak-spots, and created viruses 'bugs' to destroy said program."<<endl; 
    sleep(12); 
    cout<<"that's where you come in." << endl; 
    sleep(1); 
    cout<<"we've created an elite squad of web 'navigators' to destroy any viruses we encounter." <<endl; 
    sleep(1); 
    cout<<"you will be tasked with controlling one of these 'navigators' to end the rebellion once and for all."<<endl; 
    sleep(1); 
    cout<<"let me show you them"<<endl; 
} 

string choice(nav & user) 
{ 
    sleep(1); 
    cout<<"..."<<endl; 
    sleep(1); 
    cout<<"bladenav: a well balanced navigator that can wield a multitude of de-bugging weapons"<<endl; 
    cout<<"arrownav: a speed-oriented navigator that can attack stealthily from far range"<<endl; 
    cout<<"fistsnav: an attack-heavy navigator with low speed but with a variety of high-power weapons"<<endl; 
    cout<<"shellnav: an extremely high defense navigator with low speed and attack "<<endl; 
    cout<<"golemnav: a high attack, high defense navigator with low speed, and a narrow range of attacks "<< endl; 
    cout<<"flamenav: a navigator with flame-based attacks with relatively large luck"<<endl; 
    cout<<"aqueonav: a navigator with aqua-based attacks with relatively large luck"<<endl; 
    cout<<"plantnav: a navigator with plant-based attacks with relatively large luck"<<endl; 
    cout<<"psychnav: a high hit points navigator with indirect attacking abilities"<<endl; 
    cout<<"chicknav: a navigator with terrible stats all around and no redeeming qualities whatsoever except for it's 100% luck stat."<<endl; 
    sleep(3); 
    cout<<"you may choose one and only one to control. pick wisely"<<endl; 
    string navchoice; 
    cin>> navchoice; 
    if (navchoice == "bladenav") user.INIT(50, 50, 50, 100, 10, 40); 
    else if (navchoice =="arrownav") user.INIT(55, 30, 65, 100, 10, 40); 
    else if (navchoice =="fistsnav") user.INIT(80, 55, 10, 115, 10, 30); 
    else if (navchoice =="shellnav") user.INIT(25, 80, 25, 120, 10, 40); 
    else if (navchoice =="golemnav") user.INIT(70, 70, 10, 100, 10, 40); 
    else if (navchoice =="flamenav") user.INIT(50, 50, 40, 100, 15, 45); 
    else if (navchoice =="aqueonav") user.INIT(50, 50, 40, 100, 15, 45); 
    else if (navchoice =="plantnav") user.INIT(50, 50, 40, 100, 15, 45); 
    else if (navchoice =="psychnav") user.INIT(20, 30, 30, 150, 10, 50); 
    else if (navchoice =="chicknav") user.INIT(20, 20, 20, 100, 100, 40); 
    sleep(1); 
    user.display(); 
    sleep(1); 
    cout<<"is this you're final selection, "<< charname <<"?"<<endl; 
    string yesno; 
    cin >> yesno; 
    return yesno; 
} 

void btutorial() 
{ 
    userweap dummyweap; 
    regcannonmake(dummyweap); 
    beamswordmake(dummyweap); 
    laserbowmake(dummyweap); 
    flyingfistmake(dummyweap); 
    razordiscmake(dummyweap); 
    blockpunchmake(dummyweap); 
    flaremake(dummyweap); 
    wavemake(dummyweap); 
    whipmake(dummyweap); 
    mindshotmake(dummyweap);  
    cout<<"now that you've chosen your navigator, you should probably learn how to use it."<<endl; 
    sleep(1); 
    cout<<"..."<<endl; 
    sleep(1); 
    cout<<"when you find an enemy 'bug', battle mode is initiated."<<endl; 
    sleep(1); 
    cout<<" ______________________________________________________"<<endl; 
    cout<<"|ATTACK| WEAPONS | ITEMS | FLEE | CHECK STATS | DEFEND |"<<endl; 
    cout<<"|______|_________|_______|______|_____________|________|"<<endl; 
    sleep(1); 
    cout<<"then you select an option. choosing 'attack' attacks the opponent. The damage you deal is equal to the weapon's power added to your attack, then subtracted by the enemy's defense. 'weapons' accesses your de-bugging arsenal, 'items' allows you to view and use your one-use items, 'check stats' checks both your and the enemy's health points and stats, and 'defend' doubles your defense for the next turn, but takes up a turn. activating 'flee' will end the encounter unless your speed is lower than the 'bug's'."<<endl; 
    sleep(10); 
    cout<<"then, after your turn is over, your oppenent will either attack or defend."<< endl; 
} 

string allgood() 
{ 
    sleep(5); 
    cout<< "all good?" <<endl; 
    string yesno; 
    cin >> yesno; 
    return yesno; 
} 

//WEAPON CREATION 
void regcannonmake(userweap & regcannon) 
{ 
    regcannon.INIT(1,5,0); 
} 
void beamswordmake(userweap & beamsword) 
{ 
    beamsword.INIT(2,7,1); 
} 
void laserbowmake(userweap & laserbow) 
{ 
    laserbow.INIT(3,6,1); 
} 
void flyingfistmake(userweap & flyingfist) 
{ 
    flyingfist.INIT(4,8,1); 
} 
void razordiscmake(userweap & razordisc) 
{ 
    razordisc.INIT(5,4,1); 
} 
void blockpunchmake(userweap & blockpunch) 
{ 
    blockpunch.INIT(6,5,1); 
} 
void flaremake(userweap & flare) 
{ 
    flare.INIT(7,5,1); 
} 
void wavemake(userweap & wave) 
{ 
    wave.INIT(8,5,1); 
} 
void whipmake(userweap & whip) 
{ 
    whip.INIT(9,5,1); 
} 
void mindshotmake(userweap & mindshot) 
{ 
    mindshot.INIT(10,7,1); 
} 
//CHECKSTATS 
void checkstats(bug & simbug, nav & user) 
{ 
    cout<<"your stats:"<< endl; 
    user.display(); 
    cout<<""<< endl; 
    cout<<"bug's stats:"<< endl; 
    simbug.display(); 
} 
void weapons(userweap & regcannon) 
{ 

    sleep(1); 
    cout<<"weapons:"<<endl; 
    cout<<"regcannon"<<endl; 
    sleep(1); 
    cout<< "pick one" <<endl; 
    string weapchoice; 
    cin >> weapchoice; 
    if (weapchoice == "regcannon") 
    { 
     regcannon.display(); 
     regcannon.equip(); 
     while(allgood() == "no"); 
     cout<< charname << " equipped the regcannon" <<endl; 
    } 

} 
void flee(); 
void flee() 
{ 

} 
void defend(nav & user); 
void defend(nav & user) 
{ 

} 
void items(); 
void items() 
{ 

} 
bool battle(nav & user); 
bool battle(nav & user) 
{ 
    bug simbug; 
    simbug.INIT(10,10,10,10,10,10); 
    cout<<simbug.bugatk<<endl; 

    return true; 
} 
+2

请把这个减少到最小的例子来说明你的问题。 – ChrisInEdmonton

当你的代码开始建立这样的,它变得更容易,更容易错过一些小的细微差别。在这种情况下,你已经遇到了两组变量的问题,这两个变量意味着相同的事物,但没有区分谁是特定操作的哪一方。

在您的userweap::INIT函数中,您正在接收两个值来初始化具有weappower和weaponcost的对象。但不是初始化它们,而是将对象中的数据复制到参数中,然后返回。

void userweap::INIT(int id, int weappower /*input*/, int weapcost /*input*/) 
{ 
    weappower /*input*/ = power /*member value*/; 
    weapcost /*input*/ = cost /*member value*/; 
} 

这和你打算做的事完全相反。因此,您的成员值powercost从来没有有意分配的值,您的32767是来自“未定义行为”的随机数据。

帮助自己避免这种情况:给成员变量的前缀,比如“M_”或“M”

int m_power; // member variable that stores power. 

现在,你的代码会进行一些额外的指示,你在做什么:

void userweap::INIT(int id, int weappower, int weapcost) 
{ 
    weappower = m_power; 
    weapcost = m_cost; 
} 

再加上你将能够避免“WEAP”字头的位置:

void userweap::INIT(int id, int power, int cost) 
{ 
    power = m_power; 
    cost = m_cost; 
} 

更容易看到雾ake,正确的代码是:

void userweap::INIT(int id, int power, int cost) 
{ 
    m_power = power; 
    m_cost = cost; 
} 

更改线路:

weappower = power; 
weapcost = cost; 

他们应该是:

power = weappower; 
cost = weapcost; 

目前的情况是,你永远不分配给power,所以这是不确定的。

void userweap::INIT(int id, int weappower, int weapcost) 
{ 
    weappower = power; 
    weapcost = cost; 
} 
void userweap::display() 
{ 
    cout<<"power: " << power <<endl; 

} 

在您的INIT()中,您将成员变量的值分配给参数。它需要是相反的。

您设置的参数不是您的成员,因此power未初始化。

void userweap::INIT(int id, int weappower, int weapcost) 
{ 
    weappower = power; 
    weapcost = cost; 
} 

应该是:

void userweap::INIT(int id, int weappower, int weapcost) 
{ 
    power = weappower; 
    cost = weapcost; 
} 

另一件事情:使用构造函数!

userweap::userweap(int id, int weappower, int weapcost) 
: power(weappower), 
    cost(weapcost) 
{ 
} 

而接下来的时间请去点(不要发布“代码墙”)