基于文本的冒险游戏

use*_*892 7 c++ class adventure game-engine

我正在监督一个技术营地,其中一个营员为一个基于文本的视频游戏创建了一些代码,他无法显示结果.当程序编译并正确运行时,当选择"治疗"时它不会增加玩家的健康状况,当用户选择"攻击"时我们也会得到零.我对编程知识有限,并且我尽力帮助他,以便他在这里的经历将是愉快和充实的.如果您能提供任何帮助或建议,我们将非常感激.这是代码:

// Test for hard stuff.cpp : Defines the entry point for the console application.
//
// Bigger proj
// Constructors will make characters with rolling statistics

#include "stdafx.h"
#include <iostream>
#include <string> 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
// declaring function for hit power
//int power( int str, int def);

    int command;


class character
{
public:
    character();
    //~character();
    string name;
    float str;
    float def;
    float health;   // hit points
    float regen;    // health regen amount
    float roll;     // for random value
    float ouch;     // amount of attack damage
    float getAttack(void);
    float getHeal(void);
    void setRegen(float reg);
    //void setHeal(float healAmt);

private:


};

character::character()
{
    srand(time_t(NULL));
    str = rand() % 30 + 5;
    def = rand() % 30 + 5;
    health = 100;
    //Output to check the constructor is running properly
    cout<< "Character has been created.\n";
}

void character::setRegen( float reg )
{
    regen = reg;
}


float character::getAttack()
{
//defines the magnitude/power of attack
    //function shows how much damage is inflicted


    // ouch is how much damage is done
    roll = rand() % 20 + 1; // range between 1 &20

    if (roll <= 11)
    {
        ouch = str - (def /2);
    }

    else if ((roll <= 17) && (roll >= 12))
    {
        ouch = (str * 2) - (def / 2);
    }

    else if ((roll <= 20) && (roll >= 18))
    {
        ouch = (str * 3) - (def / 2);
        //cout << "CRITICAL HIT!!";
    }

    return ouch;

}

float character::getHeal()
{
    //this is what happens when you chose to heal
    regen = rand() % 20 + 3;
    cout << "regen value= " << regen<< ".\n";
    return regen;
}

/*character::~character()
{
    str = 0;
    def = 0;
    health = 0;
    // Output to check the destructor is running properly
    cout << "Character has been destroyed\n";
} */


int _tmain(int argc, _TCHAR* argv[])
{
    //Class objects
    character user, computer;
    //Hard code in a name for the computer's player
    computer.name = "ZOID\n";

    float attackDamage;
    float healthAdded;

    user.setRegen(void);

    //Recieve data for the user's player
    cout<< "Please enter a name for your character:\n";
    cin>> user.name;

    //Output name and stats to the user 
    cout<< "\nYour name is: " << user.name << endl;
    cout << "here are your statistics: \n"
        << "strength:   " << user.str << endl
        << "Defense:    " << user.def << endl
        << "Health:     " << user.health << endl;

    cout<< "oh no an oppenent appeared!!!\n";
        cout<< "you will have to fight him!" << endl<< endl;

    cout << "opponent's health: 100"  << endl

        << "what would you like to do: heal (1), attack(2), or run(3).\n";
    cin>> command;




        switch(command)
        {
        case 1 :

            healthAdded = user.getHeal();

            cout<< ""<<user.name <<" has regenerated " << healthAdded << " health.\n";

            break;

        case 2 :

            attackDamage = user.getAttack();

            cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";

            break;

        case 3:

            cout<< ""<<user.name<<" got away!\n";

            break;

        default:
            cout<< "Please enter a valid choice!";

        } //end switch

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ing 6

我会尽力帮助,尽我所能.我的行号可能与您的略有不同,所以请随意环顾四周.

在:

 115     user.setRegen(void);
Run Code Online (Sandbox Code Playgroud)

setRegen被宣布为float:

 20 class character
 21 {
 22 public:
 .
 .
 .
 34     void setRegen(float reg);
Run Code Online (Sandbox Code Playgroud)

所以你无法通过void.顺便说一句,在C++中,习惯上在调用不带参数的函数时不传递任何内容,而不是传递显式void.但是,显式void是可以的.

getHeal()函数计算随机ammount以修复字符,但它实际上不会增加health成员变量.您可以通过这种方式实现修复,请参阅第92行:

 87 float character::getHeal()
 88 {   
 89     //this is what happens when you chose to heal
 90     regen = rand() % 20 + 3;
 91     cout << "regen value= " << regen<< ".\n";
 92     health += regen;
 93     return regen;
 94 }   Z
Run Code Online (Sandbox Code Playgroud)

当你发动攻击时,你也不会降低对手的生命值.你可以这样做的一种方法是将对象传递给对手getAttack()并在那里修改它:

 58 float character::getAttack(character& opponent)
 59 {
 60 //defines the magnitude/power of attack
 61     //function shows how much damage is inflicted
 62 
 63 
 64     // ouch is how much damage is done
 65     roll = rand() % 20 + 1; // range between 1 &20
 66 
 67     if (roll <= 11)
 68     {
 69         ouch = str - (def /2);
 70     }
 71 
 72     else if ((roll <= 17) && (roll >= 12))
 73     {
 74         ouch = (str * 2) - (def / 2);
 75     }
 76 
 77     else if ((roll <= 20) && (roll >= 18))
 78     {
 79         ouch = (str * 3) - (def / 2);
 80         //cout << "CRITICAL HIT!!";
 81     }
 82 
 83     opponent.health -= ouch;
 84 
 85     return ouch;
 86 
 87 }
Run Code Online (Sandbox Code Playgroud)

您还需要更改声明(原型)getAttack():

 20 class character
 21 {
 22 public:
 .
 .
 .
 32     float getAttack(character& opponent);
Run Code Online (Sandbox Code Playgroud)

......以及如何调用它main():

152         case 2 :    
153     
154             attackDamage = user.getAttack(computer);
155     
156             cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";
157 
158             break;
Run Code Online (Sandbox Code Playgroud)

我也注意到该程序根本没有循环.它只接受一个动作,执行它并终止.如果游戏循环直到其中一个玩家死亡,游戏可能会更有趣.

最后一件事,当使用随机数时,srand通常在程序运行开始时调用一个.您每次character创建时都会调用它.

是我之前关于使用的一个回答的无耻插件rand.

我为你做了一些修改.以下是ideone链接,代码如下:

// Test for hard stuff.cpp : Defines the entry point for the console application.
//
// Bigger proj
// Constructors will make characters with rolling statistics

//#include "stdafx.h"
#include <iostream>
#include <string> 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
// declaring function for hit power
//int power( int str, int def);

    int command;


class character
{
public:
    character();
    //~character();
    string name;
    float str;
    float def;
    float health;   // hit points
    float regen;    // health regen amount
    float roll;     // for random value
    float ouch;     // amount of attack damage
    float getAttack(character& opponent);
    float getHeal(void);
    void setRegen(float reg);
    bool IsAlive() const;
    //void setHeal(float healAmt);

private:


};

character::character()
{
    str = rand() % 30 + 5;
    def = rand() % 30 + 5;
    health = 100;
    //Output to check the constructor is running properly
    cout<< "Character has been created.\n";
}

bool character::IsAlive() const
{
    return health > 0.0f;
}

void character::setRegen( float reg )
{
    regen = reg;
}


float character::getAttack(character& opponent)
{
//defines the magnitude/power of attack
    //function shows how much damage is inflicted


    // ouch is how much damage is done
    roll = rand() % 20 + 1; // range between 1 &20

    if (roll <= 11)
    {
        ouch = str - (def /2);
    }

    else if ((roll <= 17) && (roll >= 12))
    {
        ouch = (str * 2) - (def / 2);
    }

    else if ((roll <= 20) && (roll >= 18))
    {
        ouch = (str * 3) - (def / 2);
        //cout << "CRITICAL HIT!!";
    }

    opponent.health -= ouch;

    return ouch;

}

float character::getHeal()
{
    //this is what happens when you chose to heal
    regen = rand() % 20 + 3;
    cout << "regen value= " << regen<< ".\n";
    health += regen;    
    return regen;
}
/*character::~character()
{
    str = 0;
    def = 0;
    health = 0;
    // Output to check the destructor is running properly
    cout << "Character has been destroyed\n";
} */


int main()
{
    srand(time_t(NULL));
    //Class objects
    character user, computer;
    //Hard code in a name for the computer's player
    computer.name = "ZOID\n";

    float attackDamage;
    float healthAdded;

    user.setRegen(42.0);

    //Recieve data for the user's player
    cout<< "Please enter a name for your character:\n";
    cin>> user.name;

    //Output name and stats to the user 
    cout<< "\nYour name is: " << user.name << endl;
    cout << "here are your statistics: \n"
        << "strength:   " << user.str << endl
        << "Defense:    " << user.def << endl
        << "Health:     " << user.health << endl;

    cout<< "oh no an oppenent appeared!!!\n";
        cout<< "you will have to fight him!" << endl<< endl;

    cout << "opponent's health: 100"  << endl;


    while (user.IsAlive() && computer.IsAlive())
    {
        cout << "Str: " << user.str << "\t"
            << "Def: " << user.def << "\t"
            << "Health: " << user.health << "\t"
            << "\n";

        cout << "what would you like to do: heal (1), attack(2), or run(3).\n";
        cin>> command;

        switch(command)
        {
        case 1 :

            healthAdded = user.getHeal();

            cout<< ""<<user.name <<" has regenerated " << healthAdded << " health.\n";

            break;

        case 2 :

            attackDamage = user.getAttack(computer);

            cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";

            break;

        case 3:

            cout<< ""<<user.name<<" got away!\n";

            break;

        default:
            cout<< "Please enter a valid choice!";

        } //end switch
    }
    return 0;

}
Run Code Online (Sandbox Code Playgroud)


小智 2

一般来说,解决此类问题的方法之一是逐行检查发生的情况并确定每行的作用。有时它很长(很多时候,真的),但也能很好地确保你不会错过任何东西。在这种特殊情况下,让我们看看治疗问题。

当您输入 switch 语句并命中情况 1(治疗)时,代码所做的第一件事是将 user.getHeal() 的结果分配给 healthAdded。从这里您要做的就是“步入” getHeal() 并查看它的作用。getHeal() 获取一个再生数,并将其分配给再生。然后它打印 regen,最后返回您存储在 regen 中的值。

现在我们知道了 getHeal() 的作用,我们可以跳回案例 1:并完整地说明第一行的作用。它获取 getHeal() 中内置的 regen 值并将其分配给 healthAdded。

情况1:然后打印break之前healthAdded中的值;陈述。休息时间;案例1结束。

那么您的代码以快速列表形式执行的操作是:

  • 产生治疗值
  • 打印两次

您想要做的是根据 regen 值修改用户的健康状况,因此您缺少一个步骤:使用您在 getHeal() 中构建的 regen 值更改 user.health 值。

攻击损害的问题是类似的,尝试将您希望代码以类似目标的方式执行的操作与您看到的代码实际执行的操作进行比较。