I am trying to write a combat.. module, I guess would be the word.. that will be able to be 100% stand-alone, not needing any global variables or anything else of that nature - EG, could be included in a header file.
The problem. I wrote a program (this happened to just be a test), but I ended up with some global variables no matter what I tried to do. I was wondering if I could get some help / advice in how to keep everything self-contained and accessable to everything.
Thank you in advance.
Code:
#include <iostream>
#include <ctime> // For srand(time(NULL))
#include <windows.h> // For Sleep()
class Creature
{
public:
Creature();
~Creature();
int GetStat(char* stat);
int SetStat(char* stat, int value);
private:
int HP;
int MP;
int DMG;
int DEF;
int Armor;
int Dodge;
};
Creature::Creature()
{
HP = 10;
MP = 10;
DMG = 6;
DEF = 6;
Armor = 0;
Dodge = 1;
}
Creature::~Creature() {}
Creature::GetStat(char* stat)
{
if (strcmp(stat, "HP") == 0) return(HP);
else if (strcmp(stat, "MP") == 0) return(MP);
else if (strcmp(stat, "DMG") == 0) return(DMG);
else if (strcmp(stat, "DEF") == 0) return(DEF);
else if (strcmp(stat, "Armor") == 0) return(Armor);
else if (strcmp(stat, "Dodge") == 0) return(Dodge);
}
Creature::SetStat(char* stat, int value)
{
if (strcmp(stat, "HP") == 0) HP = value;
else if (strcmp(stat, "MP") == 0) MP = value;
else if (strcmp(stat, "DMG") == 0) DMG = value;
else if (strcmp(stat, "DEF") == 0) DEF = value;
else if (strcmp(stat, "Armor") == 0) Armor = value;
else if (strcmp(stat, "Dodge") == 0) Dodge = value;
return(0);
}
// Globals
int Attack(int target);
int fight();
Creature play; // Needed because more than one function accesses the class?
Creature enemy; // Needed because more than one function accesses the class?
int turn = 1; // Needed because you can't pass a value back to main() to pass back to another function?
int dead = 0; // Needed because this is read by more than one function, can't pass back to main()?
int main()
{
std::cout << "The fight will continue until one of the two are dead.\n\n";
enemy.SetStat("HP", 12);
while ((dead != 1) && (dead != -1))
{
fight();
srand(time(NULL));
Sleep(rand()%500);
}
if (dead == -1) std::cout << "You win!";
else std::cout << "You lose.";
std::cin.get();
return(0);
}
int Attack(int target)
{
srand(time(NULL));
if (target == 1)
{
int HP = play.GetStat("HP");
int MP = play.GetStat("MP");
int DMG = enemy.GetStat("DMG");
int DEF = play.GetStat("DEF");
int Armor = play.GetStat("Armor");
int Dodge = play.GetStat("Dodge");
if (rand()%Dodge >= (DEF * Dodge))
{
std::cout << "Miss.\n";
return(-1);
}
else if (DMG < Armor)
{
std::cout << "Could not break through target's armor.\n";
}
HP = (HP - ((rand()%DMG + 1) - Armor));
play.SetStat("HP", HP);
std::cout << HP << std::endl;
if (HP <= 0)
{
dead = 1;
return(-1);
}
return(0);
}
else if (target == -1)
{
int HP = enemy.GetStat("HP");
int MP = enemy.GetStat("MP");
int DMG = play.GetStat("DMG");
int DEF = enemy.GetStat("DEF");
int Armor = enemy.GetStat("Armor");
int Dodge = enemy.GetStat("Dodge");
if (rand()%Dodge >= (DEF * Dodge))
{
std::cout << "Miss.\n";
return(-1);
}
else if (DMG < Armor)
{
std::cout << "Could not break through target's armor.\n";
}
HP = (HP - ((rand()%DMG + 1) - Armor));
enemy.SetStat("HP", HP);
std::cout << HP << std::endl;
if (HP <= 0)
{
dead = -1;
return(1);
}
return(0);
}
return(0);
}
int fight()
{
if (turn == 1)
{
std::cout << "Enemy's HP: ";
Attack(-1);
turn = -1;
return(0);
}
else if (turn == -1)
{
std::cout << "Player's HP: ";
Attack(1);
turn = 1;
return(0);
}
return(0);
}