我正在監督一個科技營,其中一個營員爲基於文本的視頻遊戲創建了一些代碼,他無法顯示結果。當程序編譯並正確運行時,當選擇「治癒」時,它不會增加玩家的健康,當用戶選擇「攻擊」時,我們也會得到零。我在編程方面的知識有限,並且盡力幫助他盡我所能,以便他在這裏的體驗將令人愉快和滿足。如果你能提供任何幫助或建議,我們會非常感激。下面是代碼:基於文本的冒險遊戲
// 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;
}
告訴他們在程序開始時只使用'srand'一次。 – chris
'user.getHeal'對健康沒有任何作用,您不會使用它。我不明白健康狀況會如何改變。 – chris
只給regen和regen尚未連接到健康。基於回合的迭代在哪裏?雷根應該在那裏工作。 –