Why is my basic battle system failing? [no compile errors]

Here's the source code from "battle.h" (which should probably be renamed to "animal.h")

Do you think I made my errors here or in one of the many other headers or cpp?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "Identity.h"
#include "stdafx.h"

#ifndef battle_h
#define battle_h

int numbers1 = rand() % 5;
int numbers2 = rand() % 7;
int numbers3 = rand() % 9;
int numbers4 = rand() % 11;

class animal {
public:
	string name;
	int strength;
	int dexterity;
	int intellect;
	int health;
	void randomstat();
	void fight();
};

void animal::randomstat(){
	strength = numbers4;
	dexterity = numbers4;
	intellect = numbers1;
	health = 50;
}

void randomanimal() {
	srand((unsigned)time(0));
	numbers2;
	if (numbers2 == 0){
		extern animal lion;
		lion.randomstat();
		lion.name = "Lion";
		cout << "Lion ";
	}
	else if (numbers2 == 1){
		extern animal bear;
		bear.randomstat();
		bear.name = "Bear";
		cout << "Bear ";
	}
	else if (numbers2 == 2){
		extern animal wolf;
		wolf.randomstat();
		wolf.name = "Wolf";
		cout << "Wolf ";
	}
	else if (numbers2 == 3){
		extern animal idk;
		idk.randomstat();
		idk.name = "Lion";
		cout << "Lion ";
	}
	else if (numbers2 == 4){
		extern animal ireallyshouldthinkofsomething;
		ireallyshouldthinkofsomething.randomstat();
		ireallyshouldthinkofsomething.name = "Bear";
		cout << "Bear ";
	}
	else if (numbers2 == 5){
		extern animal insertgenericanimal;
		insertgenericanimal.randomstat();
		insertgenericanimal.name = "Wolf";
		cout << "Wolf ";
	}
	else {
		extern animal ilostpathfinderhandbook;
		ilostpathfinderhandbook.randomstat();
		ilostpathfinderhandbook.name = "Invisi-Lion gonna eat your babies";
		cout << "Invisible Lion lol ";
	}
}


void animal::fight() {
	srand((unsigned)time(0));
	randomanimal();
	cout << " attacks for " << strength << endl;
	strength + numbers1 - player.health;
	hurtfunction();
	player.attack - health;
}

void hurtfunction() {
	if (player.health >= 50) {
		cout << "You're enraged, yet still able." << endl;
	}
	else if (player.health <= 49) {
		cout << "You're bleeding and slowly blacking out." << endl;
	}
}

#endif 


Any help criticism or suggestions at all would be very appreciated!
Last edited on
what do you think the 'extern' keywords are doing?
what do you think the 'extern' keywords are doing?


I think they're making the animals usable when I talk about them in the actual cpp document, but I'm not 100% sure.

Everything I know about c++ is from strangers and tutorials mostly from this site. I haven't had formal education on this.
Last edited on
See here for extern:
http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how

But, i don't really think you need to use them.

Declare stuff in your .h, but write the implementation in your .cpp.

Lines 7 - 10, globals are bad. They should be member variables really.
Similarly is 'hurtFunction()' specific to your animal class? if so this should be a class member too.

Line 32 isn't doing anything.
You should initialise your member variables.

in no particular order
- function definitions in header
- variable definitions in header
- functions that don't receive or return anything
- seeding the random number generator over and over again
- statements that have no effect (like lines 82, 84)
- ¿what is `player'?
- asking about code that we can't see
- no self-contained code http://www.eelis.net/iso-c++/testcase.xhtml
- no description of the problem
Declare stuff in your .h, but write the implementation in your .cpp.


This is very good advice. I remember learning this a few weeks ago, but drifted back into the bad habit of declaring in .h I've run myself into issues from making this mistake a few too many time :)

Lines 7 - 10, globals are bad. They should be member variables really.
Similarly is 'hurtFunction()' specific to your animal class? if so this should be a class member too.


Hurt function is actually more relevant to my Player Controller Class and not so much to the animals. I will definitely move it to the Player Controller header and I can definitely put the globals into the animal class, definitely.

Line 32 isn't doing anything.
You should initialise your member variables.


Ok I'm going to figure this out. It's left me a little confused and though that it may be the main culprit for the odd behavior of the battle sequencing. I obviously need to change line 32 as it is key to the random fighting. I am new to the class Segment of my self education, so I know I've been making plenty of mistakes and will be until I have enough practice.

But, i don't really think you need to use them.


After I get the other good stuff working I will definitely see if I can get by without the externs.

Thanks for your help.

numbers1-4
----------
you could write them as a function instead

e.g.

int getRandom(int max) { return rand() % max; }
then in your program you can have a local variable

1
2
3
4
randomanimal() {
...
  numbers2 = getRandom(7);
...


and so on.

design
------
It is generally better to put your definitions in the .cpp file instead
of having them in the header. When you look at a class for the first time
you want to see what functions are there, not how the implementation
looks like. If you then are interested you look in the .cpp file.

It is also recommended to put your classes/functions in a namespace
that way you keep things together and do not pollute the global namespace.

e.g.

1
2
3
4
5
6
7
8
9
10
11
12
namespace mygame
{
  class animal {
  ...
  }
  
  void randomanimal() {
  ...
  }
  
  ...
}

You may also want to lose the global functions and make them member
functions in some class instead, then declare those animals you have
as member variables in that class instead of having extern animal lion etc.
Ninjaedit: Anders I really wish I could plus 1 you right now lol.

ne555: Player is identified in "Identity.h"

By the way. I am writing this huge program as an attempt to make a Pathfinder simulator. I am currently trying to just accomplish the base to build on later.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "Identity.h"
#include "stdafx.h"

#ifndef battle_h
#define battle_h

class animal {
public:
	string name;
	int strength;
	int dexterity;
	int intellect;
	int health;
	void randomstat();
	void fight();
	int numbers1 = rand() % 5;
	int numbers2 = rand() % 7;
	int numbers3 = rand() % 9;
	int numbers4 = rand() % 11;
};

void animal::randomstat(){
	strength = numbers4;
	dexterity = numbers4;
	intellect = numbers1;
	health = 50;
}

void randomanimal() {
	srand((unsigned)time(0));
	int createdanimal = rand() % 7;
	if (createdanimal == 0){
		extern animal lion;
		lion.randomstat();
		lion.name = "Lion";
		cout << "Lion ";
	}
	else if (createdanimal == 1){
		extern animal bear;
		bear.randomstat();
		bear.name = "Bear";
		cout << "Bear ";
	}
	else if (createdanimal == 2){
		extern animal wolf;
		wolf.randomstat();
		wolf.name = "Wolf";
		cout << "Wolf ";
	}
	else if (createdanimal == 3){
		extern animal idk;
		idk.randomstat();
		idk.name = "Lion";
		cout << "Lion ";
	}
	else if (createdanimal == 4){
		extern animal ireallyshouldthinkofsomething;
		ireallyshouldthinkofsomething.randomstat();
		ireallyshouldthinkofsomething.name = "Bear";
		cout << "Bear ";
	}
	else if (createdanimal == 5){
		extern animal insertgenericanimal;
		insertgenericanimal.randomstat();
		insertgenericanimal.name = "Wolf";
		cout << "Wolf ";
	}
	else {
		extern animal ilostpathfinderhandbook;
		ilostpathfinderhandbook.randomstat();
		ilostpathfinderhandbook.name = "Invisi-Lion gonna eat your babies";
		cout << "Invisible Lion lol ";
	}
}


void animal::fight() {
	srand((unsigned)time(0));
	randomanimal();
	cout << " attacks for " << strength << endl;
	strength + numbers1 - player.health;
	player.hurtfunction();
	player.attack - health;
}

#endif 


These are the changes I could quickly make. I am trying to think out the others as we speak.
Last edited on
Since other people gave you a lot of good advices. I will give you this.

You are doing many things wrong. And if you find yourself doing so many things wrong stop doing things and start questioning things.
Topic archived. No new replies allowed.