Hi, I'm trying to make a set of monsters that have values that can be loaded. Like a Goblin having enemyhealth = 50, enemydamage = 15, etc. Here's what I kinda have an idea of doing... But I really have no idea how to use it.

(File containing values for enemies)
Monsterreader:

#include <iosteam>
using namespace std;

struct Archer {
       char enemyname[25] = "Archer";
  int enemyhealth = 55;
int enemydamage = 15;
int enemydefense = 5;
} ;
struct Goblin {
       char enemyname[25] = "Goblin";
  int enemyhealth = 30;
int enemydamage = 13;
int enemydefense = 10;
} ;

And the actual battle function:

// <strong class="highlight">include</strong> the declaration
#include "battle.h"
#include <iostream>
#include <windows.h>
char battlechoice[10];
char enemyname[25];
char name[50];
int enemyhealth;
int enemydamage;
int enemydefense;
int strength;
 int intelligence;
 int dexterity;
 int health;
 int mana;
 int damage;
 int defense;
 int hitchance;
int roll;
int rollcalc;
int rollcalchit;
int hitroll;
int rand_0toN1(int rollcalc);
int randd_0toN1(int rollcalchit);
using namespace std;

// Function definition



void battleFunction() {
     system("Color 05");
     
     
     srand(time(NULL));
 

     
     system("Cls");
  std::cout << "Battle Initiated";   
  Sleep(2500);
  system("Cls");
  // 17
  std::cout << " _________________       _________________ " << endl;
  std::cout << "|     Battle      |         " << enemyname << endl;
  std::cout << "|-----------------|      _________________ " << endl;
  std::cout << "|     Attack      |                "  <<endl;
  std::cout << "|     Skill       |        Health  " << enemyhealth  <<endl;
  std::cout << "|     Defend      |        Defense " << enemydefense << endl;
  std::cout << "|     Item        |        Attack  " << enemydamage << endl;
  std::cout << "|     Run         |                        " << endl;
  std::cout << "|_________________|                        " << endl <<endl <<"Health: "
  << health  <<endl <<"Mana: " << mana << endl <<endl;
  battlecommand:
  std::cout << "Command: ";
  cin.getline(battlechoice, 10);
  if (battlechoice == "Attack" || "attack" || "atk" || "1" || "A")
  {
                   roll = rand_0toN1(5) + 1;
                   hitroll = randd_0toN1(10) + 1;
                   damage = strength*roll*1.5-enemydefense*1.8;
                   hitchance = dexterity*hitroll*0.5;
                   if (hitchance >= 14)
                   {
                                 enemyhealth = enemyhealth-damage;
                                 cout << name <<" did " << damage << " damage!" <<endl;
                                 }
                                 else
                                 { 
                                     cout << name <<" Missed!" <<endl;
                                     Sleep(1500);
                                     
                                     goto battlecommand;
                                     }
                   
                   
                   
                   }
                   
  
  
  Sleep(3000);
  
  
}
int rand_0toN1(int rollcalc) {
    return rand() % rollcalc;
}
int randd_0toN1(int rollcalchit) {
    return rand() % rollcalchit;
}

Thanks ahead of time - Valestrom

Couple of points.

1) Remove char arrays and use std::string.
2) Looking at the problem you can see that every different enemy will have different stats but in general will have same functions(i.e attack defend ), hence this suggest to create a polymorphic behavior. So I would suggest to do something like so,

//using struct for demonstration
enum EnemyType{ ARCHER, GOBLIN };
struct IEnemy{
 virtual void attack(IEnemy& opponent);
 virtual void takeDamage(int damageToTake); 
 virtual void getHealth();
 virtual void getPower();
 bool isDead()const{ return getHealth() <= 0; }
 EnemyType getType()const;
 virtual IEnemy() {}
};
struct Archer : IEnemy{
 int health;
 int power;
 
 Archer(): health(100) , power(10){}
 int getHealth()const{ return health; }
 int getPower()const{ return power; }
 EnemyType getType()const{ return ARCHER; }

 void attack(IEnemy& opponent){
     opponent.takeDamage( power ); //assume Archer give 10 points of damage
 }
 void takeDamage(int damageToTake){
    health -= damageToTake;
 }

}

//..and so on for other enemies
//..and go on from there

Couple of points.

1) Remove char arrays and use std::string.
2) Looking at the problem you can see that every different enemy will have different stats but in general will have same functions(i.e attack defend ), hence this suggest to create a polymorphic behavior. So I would suggest to do something like so,

//using struct for demonstration
enum EnemyType{ ARCHER, GOBLIN };
struct IEnemy{
 virtual void attack(IEnemy& opponent);
 virtual void takeDamage(int damageToTake); 
 virtual void getHealth();
 virtual void getPower();
 bool isDead()const{ return getHealth() <= 0; }
 EnemyType getType()const;
 virtual IEnemy() {}
};
struct Archer : IEnemy{
 int health;
 int power;
 
 Archer(): health(100) , power(10){}
 int getHealth()const{ return health; }
 int getPower()const{ return power; }
 EnemyType getType()const{ return ARCHER; }

 void attack(IEnemy& opponent){
     opponent.takeDamage( power ); //assume Archer give 10 points of damage
 }
 void takeDamage(int damageToTake){
    health -= damageToTake;
 }

}

//..and so on for other enemies
//..and go on from there

This looks great! Thanks man!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.