2013-07-10 168 views
7

我正在監督一個科技營,其中一個營員爲基於文本的視頻遊戲創建了一些代碼,他無法顯示結果。當程序編譯並正確運行時,當選擇「治癒」時,它不會增加玩家的健康,當用戶選擇「攻擊」時,我們也會得到零。我在編程方面的知識有限,並且盡力幫助他盡我所能,以便他在這裏的體驗將令人愉快和滿足。如果你能提供任何幫助或建議,我們會非常感激。下面是代碼:基於文本的冒險遊戲

// 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; 

} 
+1

告訴他們在程序開始時只使用'srand'一次。 – chris

+1

'user.getHeal'對健康沒有任何作用,您不會使用它。我不明白健康狀況會如何改變。 – chris

+1

只給regen和regen尚未連接到健康。基於回合的迭代在哪裏?雷根應該在那裏工作。 –

回答

2

一般而言,接近這種問題的方法之一是研究什麼的線發生線,並確定各行的作用。它有時很長(很多次,真的),但也確保你不會錯過任何東西。在這個特殊情況下,讓我們看看治癒問題。

當你輸入switch語句並打到case 1(修復)時,代碼所做的第一件事就是將user.getHeal()的結果賦給healthAdded。你從這裏做的是「步入」getHeal()並看看它做了什麼。 getHeal()獲得一個regen數字,並將其分配給regen。然後打印regen,最後返回您在regen中存儲的值。

現在我們知道getHeal()做了什麼,我們可以跳回到案例1:並且完全說出第一行的作用。它需要在getHeal()中構建的regen值並將其分配給healthAdded。

情況1:然後在休息之前打印healthAdded中的值;聲明。休息;完成情況1.

那麼你的代碼快速列表形式所做的是:

  • 產生癒合值兩次

什麼你想要做

  • 打印是修改用戶的健康基於regen值,所以你有一個缺失的步驟:使用你在getHeal()中建立的regen編號來改變user.health值。

    與攻擊破壞相關的問題是相似的,請嘗試比較您希望代碼在目標方面與您看到的代碼實際執行的內容。

  • 6

    我會盡力幫忙,我一次只能做一件作品。我的線路號碼可能與您的號碼略有不同,請隨時查看一下。

    在:

    115  user.setRegen(void); 
    

    setRegen宣佈採取float

    20 class character 
    21 { 
    22 public: 
    . 
    . 
    . 
    34  void setRegen(float reg); 
    

    所以,你不能傳遞void。順便提一下,在C++中,調用一個不帶參數的函數時,通常不傳遞任何東西,而不是傳遞一個明確的void。但是,明確void是確定的。

    getHeal()函數計算一個隨機的數量來治療角色,但實際上並沒有增加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 
    

    您還沒有減少對手的健康,當你的攻擊。你可以這樣做的一種方式是通過傳遞給了對手一個參考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 } 
    

    您還需要更改getAttack()聲明(原型):

    20 class character 
    21 { 
    22 public: 
    . 
    . 
    . 
    32  float getAttack(character& opponent); 
    

    ...以及它是如何叫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; 
    

    我還注意到,該方案不循環的。它只接受一個動作,執行並終止。如果比賽結束,直到其中一名球員死亡,比賽可能會更有趣。

    最後一件事情是,當使用隨機數時,您通常在程序運行的開始時調用srand。每次創建character時都會調用它。

    Here對於我以前關於使用rand的答案之一是一個無恥的插件。

    我爲你做了一些修改。這裏有一個link to 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; 
    
    } 
    
    +0

    非常感謝您的幫助@John Dibling,我希望您能看到一旦我們實施了您的更改並添加了更多事情來實現這一目標,露營者將會如何欣喜若狂。這一切都值得! – user2569892

    +1

    真棒。告訴露營者,他的代碼給我留下了非常深刻的印象。他可能在編程方面有真正的未來! –