2013-07-10 13 views
7

Nadzoruję obóz techniczny, w którym jeden z obozowiczów stworzył kod do gry tekstowej, w której ma problemy z wyświetlaniem wyników. Podczas, gdy program kompiluje się i działa poprawnie, nie zwiększa stanu zdrowia gracza, gdy wybrana jest opcja "leczyć", a także dostaje zero, gdy użytkownik wybierze "atak". Mam ograniczoną wiedzę w programowaniu i staram się pomóc mu najlepiej, jak potrafię, aby jego doświadczenie było przyjemne i satysfakcjonujące. Gdybyś mógł zaoferować jakąkolwiek pomoc lub radę, bylibyśmy wdzięczni. Oto kod:Gra przygodowa tekstowa

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

Poinformuj ich, aby użyli 'srand' tylko raz, na początku programu. – chris

+1

'user.getHeal' nie robi nic dla zdrowia i nie używasz go w inny sposób. Nie rozumiem, jak może się zmienić zdrowie. – chris

+1

Daje tylko regenerację, a regeneracja nie jest jeszcze połączona ze zdrowiem. Gdzie jest turowa iteracja? Regen powinien tam pracować. –

Odpowiedz

2

Ogólnie jeden ze sposobów podejścia do tego rodzaju kwestii jest zbadanie, co się dzieje, wiersz po wierszu i ustalić, co robi każda linia. Czasami jest to wiele razy (wiele razy), ale sprawdza się również w tym, że niczego nie przeoczysz. W tym konkretnym przypadku przyjrzyjmy się kwestii leczenia.

Po wprowadzeniu instrukcji switch i hit case 1 (healing), pierwszą rzeczą, którą robi kod, jest przypisanie wyników user.getHeal() do healthAdded. To, co robisz, to "wkrocz do" getHeal() i zobacz, co robi. getHeal() pobiera numer regen i przypisuje go do regeneracji. Następnie drukuje regen, a na końcu zwraca wartość zapisaną w regen.

Teraz, gdy wiemy, co robi getHeal(), możemy wrócić do naszego przypadku 1: i w pełni powiedzieć, co robi pierwsza linia. Zajmuje wartość regen zbudowaną w getHeal() i przypisuje ją do healthAdded.

sprawa 1: następnie drukuje wartość w healthAdded przed przerwą; komunikat. Przerwa; kończy przypadek 1.

Więc co Twój kod zrobiłem w prosty formularz liście było:

  • generować leczyć wartość
  • drukiem to dwukrotnie

co chciał zrobić, to zmodyfikować zdrowia użytkownika na podstawie wartości regen, więc masz brakujący krok: zmiana wartości user.health z numerem Regen, który zbudowałeś w getHeal().

Problem z obrażeniami o ataku jest podobny, spróbuj porównać to, co kod ma zrobić w warunkach podobnych do celu z tym, co widzisz w rzeczywistości.

6

Postaram się pomóc, jak najlepiej potrafię kawałek na raz. Moje numery linii mogą się nieznacznie różnić od twoich, więc możesz się trochę rozejrzeć.

w:

115  user.setRegen(void); 

setRegen jest uznane wziąć float:

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

Więc nie można przejść void. Nawiasem mówiąc, w C++ zwyczajowo po prostu nie przekazujemy niczego podczas wywoływania funkcji, która nie przyjmuje żadnych parametrów, zamiast przekazywać jawne void. Jednak wyraźne void jest OK.

Funkcja getHeal() oblicza losową ilość, aby wyleczyć znak, ale nie zwiększa wartości zmiennej składowej health.W ten sposób możesz wdrożyć leczenie, zobacz linię 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 

Nie zmniejszasz też zdrowia przeciwnika podczas ataku. Jednym ze sposobów można to zrobić przekazując odwołanie do przeciwnika, aby getAttack() i modyfikując go tam:

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 } 

Będziesz także musiał zmienić deklarację (prototyp) dla getAttack():

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

... i jak to się nazywa w 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; 

zauważyłem również, że program nie robi pętlę w ogóle. Akceptuje tylko jedną akcję, wykonuje ją i kończy. Gra może być fajniejsza, jeśli zapętli się, dopóki jeden z graczy nie będzie martwy.

Ostatnia rzecz przy korzystaniu z liczb losowych nazywamy dokładnie jedną, zwykle na początku uruchomienia programu. Dzwonisz za każdym razem, gdy tworzony jest character.

Here to bezwstydna wtyczka dla jednej z moich poprzednich odpowiedzi na temat korzystania z rand.

Wprowadziłem dla ciebie kilka modyfikacji. Oto link to ideone z tym samym kodem, co poniżej:

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

Bardzo dziękuję za pomoc @John Dibling, Szkoda, że ​​nie widziałaś jak ekstatyczny był kamper, kiedy wprowadziliśmy twoje zmiany i dodaliśmy jeszcze kilka rzeczy, żeby to zadziałało. Wszystko było tego warte! – user2569892

+1

niesamowite. Powiedz kamperowi, że jestem pod wielkim wrażeniem jego kodu. może mieć prawdziwą przyszłość w programowaniu! –