i just wanted to post code for the start of this RPG i'm working on... all it is so far is a basic character class with 4 stats and a basic battle system.... i'm only a newbie programmer (i took a year of c++ in high school... ended up learning more on my own) and the code may be sloppy or can be done a better way... if anyone has advice or comments or suggestions please do share
(sorry if i posted this in the wrong section)
Code:
#include<iostream>
#include<string>
#include<stdlib>
#include<time>
enum STAT { iniHP, str, def, spd, agl };
//====Player Class Functions=======================================
class Player {
public:
Player(int iniStr, int iniDef, int iniSpd, int iniAgl);
~Player();
void SetNewStat(int statNum, int value);
void SetName(string iniName);
bool IsAlive();
string GetName() { return name; }
int GetStat(int statNum);
int GetHealth(int damage);
private:
string name;
bool alive;
int stats[5];
int health;
};
//-----------------------------------------------------------------
Player::Player(int iniStr, int iniDef, int iniSpd, int iniAgl) {
/* Constructor Function
sets defaults of class */
alive = true;
stats[str] = iniStr;
stats[def] = iniDef;
stats[spd] = iniSpd;
stats[agl] = iniAgl;
health = ((stats[def] + stats[agl]/2))/2 * 3;
stats[iniHP] = health;
}
Player::~Player() {
/* Destructor Function */
}
//-----------------------------------------------------------------
void Player::SetNewStat(int statNum, int value) {
/* Sets requested stat to
current stat plus value */
stats[statNum] += value;
if (statNum != iniHP) {
stats[iniHP] = ((stats[def] + stats[agl]/2))/2 * 3;
}
}
void Player::SetName(string iniName) {
name = iniName;
}
//-----------------------------------------------------------------
int Player::GetStat(int statNum) {
/* Returns value from stat array
0<= statNum <=4 if not returns 0 */
if (statNum <=4) return stats[statNum];
else return 0;
}
int Player::GetHealth(int damage = 0) {
/* Returns players health
after subtacting an amount of damage */
health -= damage;
if (health <= 0) {
health = 0;
}
else if (health >= stats[iniHP]) {
health = stats[iniHP];
}
return health;
}
bool Player::IsAlive() {
if (health <= 0) alive = false;
else alive = true;
return alive;
}
//=================================================================
//====Battle Fuctions==============================================
bool I****(Player attacker, Player defender) {
/* Returns true if character made a hit */
int accuracy = 50 + (random(attacker.GetStat(spd)/2)
- random(defender.GetStat(agl)/3));
cout << "accuracy: " << accuracy << endl;
int hit = random(101);
cout << "hit at: " << hit << endl;
if (hit > accuracy) return false;
else return true;
}
void Attack(Player attacker, Player &defender) {
int strike = (attacker.GetStat(str)/2) + random(attacker.GetStat(str)/2);
cout << attacker.GetName() << " can attack for " << strike << " points.\n";
int block = (random(strike)/4) + (random(defender.GetStat(def)/4));
cout << defender.GetName() << " will block for " << block << " points.\n";
strike-=block;
if(I****(attacker, defender)) {
defender.GetHealth(strike);
cout << attacker.GetName() << " hit " << defender.GetName() << " for " << strike << " points.\n";
cout << defender.GetName() << " health is now " << defender.GetHealth() << endl;
}
else cout << "Miss!" << endl;
}
//=================================================================
int main() {
randomize();
int pause;
Player Human(100, 100, 100, 100);
Human.SetName("Human");
Player Computer(100, 100, 100, 100);
Computer.SetName("Computer");
/* A bunch of tests to check everything */
cin >> pause;
switch (pause) {
case 1:
cout << "Strength:\t" << Human.GetStat(str) << endl;
cout << "Defense:\t" << Human.GetStat(def) << endl;
cout << "Speed:\t\t" << Human.GetStat(spd) << endl;
cout << "Agility:\t" << Human.GetStat(agl) << endl;
cout << "Health:\t\t" << Human.GetStat(iniHP) << endl;
cout << "Health - 50:\t" << Human.GetHealth(50) << endl;
cout << "Player alive:\t" << Human.IsAlive() << endl;
cout << "Health - 300:\t" << Human.GetHealth(300) << endl;
cout << "Player dead:\t" << Human.IsAlive() << endl;
cout << "Health + 300:\t" << Human.GetHealth(-300) << endl;
cout << "Player alive?:\t" << Human.IsAlive();
cout << "\n\n" << Human.GetName() << " - Defense stat + 20\n\n";
Human.SetNewStat(def,20);
cout << "Strength:\t" << Human.GetStat(str) << endl;
cout << "Defense:\t" << Human.GetStat(def) << endl;
cout << "Speed:\t\t" << Human.GetStat(spd) << endl;
cout << "Agility:\t" << Human.GetStat(agl) << endl;
cout << "New Max Health:\t" << Human.GetStat(iniHP) << endl;
cout << "Current Health:\t" << Human.GetHealth() << endl;
cout << "Player alive:\t" << Human.IsAlive() << endl;
cout << "Health - 300:\t" << Human.GetHealth(300) << endl;
cout << "Player dead:\t" << Human.IsAlive() << endl;
cout << "Health + 600:\t" << Human.GetHealth(-600) << endl;
cout << "Player alive?:\t" << Human.IsAlive();
cin >> pause;
case 2:
do {
Attack(Human, Computer);
Attack(Computer, Human);
cin >> pause;
} while (pause == 1);
}
return 0;
}
---wes