Text RPG Help

Hey guys, wondering if some can help me (im sure you guys will be able)

So im starting to make a C++ RPG, and i need some clarity.

iv started out the battle function like so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Enemies.h --

struct Creature_Snake{
unsigned HP = 10;
unsigned ATK = 1;
unsigned DEF = 1;
...
...
};

struct Creature_Snail{
unsigned HP = 20;
unsigned ATK = 1;
unsigned DEF = 6;
...
...
};

//Functions.cpp --
...
Battle(Creature_Snake){
//Enter battle with a snake
}

Battle(Creature_Snail){
//Enter battle with a snail
}


So i was wondering how i would go about only having ONE battle function that would work properly no matter what the enemy was. That way i culd give all the enemies base stats in my "Enemies.h" and then send them all to a single function

I want something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Enemies.h --

Creature Snake{
//level one Stats
}

Creature Snail{
//level one Stats
}

//Functions.cpp --

Battle(Creature enemy)
{
//Enter battle with an enemy
}


I feel like the answer is right under my fingertips, but i have never gotten to the point in my C++ exercises that i would make a class that had the properties of another class.

Or a class within a class.... idk how to even word it.




ANOTHER thing (Please dont answer this without looking at my previous problem)

A small tedious thing about ints.
I want to figure out how many numbers are in an integer without jumping through too many hoops.

EX:
How would i go about simplifying this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
...
unsigned i = 0 , number = 0;
std::cin >> i;

//Digits 0-9 (only 1 number)
if(i < 10)
  number = 1;

if(i >9 && i < 100)
  number = 2;

if(i > 99 && i < 1000)
  number = 3;

...
...
...

std::cout << "There are " << number << " numbers in the digit " << i <<std::endl 



Thank you in advanced my experienced idols ^_^
Or a class within a class.... idk how to even word it.


Go look up the page on classes again, it is basically exactly what you want.

Thank you in advanced my experienced idols ^_^


Instead of getting the number as an integer, get it as a string and look at it's length.
Yeah, for the enemy part I think you are looking to make a Creature class where the specific types of enemies inherit from it.
closed account (N36fSL3A)
Why not just make a single function that takes your two creature structs as a reference?

1
2
3
4
void Battle(Creature &c1, Creature &c2)
{
    // Code?
}


==Now that I'm thinking about it, you might want each creature to have a separate class and just inherit from a base creature class like above==
Upon a clearer state of mind, I found the solution to my problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Creature.h
struct Creature{
  unsigned HP;
  unsigned ATK;
  unsigned DEF;

  Set_Stats(unsigned health, unsigned attack, unsigned defence)
  {
    HP = health;
    ATK = attack;
    DEF = defence;
  
};

//StageOne.cpp

  Creature snake;
  snake.Set_Stats(10, 1, 1);
  
  Creature snail;
  snail.Set_Stats(20, 1, 6);

  ...
  ...
  if(x == 1)
  {
     Battle(snake)
  }
  if(x ==2)
    Battle(snail)


//Functions.cpp --
  Battle(Creature enemy);
 



Instead of getting the number as an integer, get it as a string and look at it's length.

So this will work?

1
2
3
4
5
6
7
8
int HP = 1000;
std::string check_length = (std::string)HP;

-- check_length.size();

//Attacked - Lost 500 HP

check_length = (std::string)HP;


I didnt mention that its not something that the user will be entering. its an int that changes by the program.
So this will work?


No. If you have an integer, you will need to convert it somehow, such as boost::lexical_cast.
Yeah, for the enemy part I think you are looking to make a Creature class where the specific types of enemies inherit from it.


To clarify you want to look up polymorphism Very important to know for game programming.
about the first question:
i recommend that you implement the creatures as classes not structures, classes provide more advanced functionality.
open the reference you're learning from, press (ctrl+f), a search dialog will pop, type "Inheritance" then hit Enter.
if the book doesn't contain anything about inheritance, change it, there are plenty of free references online.

after you finish inheritance, search for "Polymorphism", and start learning that.

afer you've finished both topics, you'll be able to finish the game yourself and understand every word in it.

about the second question:
try this code:
1
2
unsigned int digits , i ;
for( i = number , digits = 1 ; i>=10 ; i/=10 , digits++ ) ;
Thanks guys. I looked into Polymorphism and thats exactly what i needed.
Topic archived. No new replies allowed.