Value of int doesnt decrease

Nov 30, 2015 at 3:31am
Hello, I made a simple game, there are two unit(player and enemy) and has three skills(Basic Attack,Heal,and Special Attack),here's my problem whenever a unit uses a skill,the hp of its opponent wont change.Any help pls?Below is my code in main and my header files for each class.

main.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
97
98
99
100
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Unit.h"
#include "Player.h"
#include "Enemy.h"

using namespace std;

string answer;
int PLAYER_HP = 10000;
int ENEMY_HP = 10000;

int main() 
{
	srand(time(0));
	int damage = rand() % 100;
	int BraveBurst = rand() % 1000;
	int x = rand() % 3;
	string enemyChoices[] = {"a","b","c"};
	string enemyChoice = enemyChoices[x];
	Unit* unit[2];

	unit[0] = new Player;
	unit[1] = new Enemy;

	cout << "OneOnOne" << endl; //Name of Game
	cout << endl;
	system("pause");
	system("cls");

	for(int i=0;i<100;i++) //100 Rounds
	{
	cout << "Your HP: " << PLAYER_HP << endl;
	cout << "Enemy HP: " << ENEMY_HP << endl;
	cout << endl;
	system("pause");
	system("cls");

	cout << "What do you want to do?" << endl;
	cout << "[a]Basic Attack" << endl;
	cout << "[b]Heal" << endl;
	cout << "[c]Special Attack" << endl;
	cin >> answer;
	system("cls");

	if(answer == "a" || answer == "A") //Basic Attack
	{
		unit[0]->basicMove("Attack",damage,ENEMY_HP);
	}
	else if(answer == "b" || answer == "B") //Heal
	{
		unit[0]->healMove("Heal",damage,PLAYER_HP);
	}
	else if(answer == "c" || answer == "C") //Special Attack
	{
		unit[0]->braveburstMove("Brave Burst",BraveBurst,ENEMY_HP);
	}

	cout << "Enemy's Turn .. " << endl;
	cout << endl;
	system("pause");
	system("cls");

	if(enemyChoice == "a") //Basic Attack
	{
		unit[1]->basicMove("Attack",damage,PLAYER_HP);
	}
	else if(enemyChoice == "b") //Heal
	{
		unit[1]->healMove("Heal",damage,ENEMY_HP);
	}
	else if(enemyChoice == "c") //Special Attack
	{
		unit[1]->braveburstMove("Brave Burst",BraveBurst,PLAYER_HP);
	}
	}
	if(PLAYER_HP < 0) //If player's hp is zero (dies)
	{
		delete unit[0];
		cout << "You died (GAME OVER)" << endl;
		cout << endl;
		system("pause");
		exit(0);
	}
	if(ENEMY_HP < 0) //if enemy's hp is zero(dies)
	{
		delete unit[1];
		cout << "Enemy died (Congratulation!)" << endl;
		cout << "Here's a Smiley Face for you" << endl;
		cout << endl;
		cout << "O                        O" << endl;
		cout << " ________________________ " << endl;
		cout << endl;
		system("pause");
		exit(0);
	}
	return 0;
}


Unit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <iostream>
#include <string>

using namespace std;

class Unit
{
public:
	Unit(void);
	~Unit(void);

	virtual void basicMove(string name,int damage,int target) {}
	virtual void healMove(string name,int damage,int target) {}
	virtual void braveburstMove(string name,int damage,int target) {}
};



Player.h
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
#pragma once
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Unit.h"

using namespace std;

class Player : public Unit
{
public:
	Player(void);
	~Player(void);

	void basicMove(string name,int damage,int target) 
	{
		target -= damage;


		cout << "You did " << name << " to " << target << endl;
		cout << "Damage: " << damage << endl;
		cout << endl;
		system("pause");
		system("cls");
	}
	void healMove(string name,int damage,int target)
	{
		target -= damage; //Heal

		cout << "You used " << name << endl;
		cout << "Heal Pts: " << damage << endl;
		cout << endl;
		system("pause");
		system("cls");
	}
	void braveburstMove(string name, int damage, int target)
	{
		target -= damage; // Special Attack(BraveBurst Move)

		cout << "You did " << name << endl;
		cout << "Damage: " << damage << endl;
		cout << endl;
		system("pause");
		system("cls");
	}
};



Enemy.h
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
#pragma once
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Unit.h"

using namespace std;

class Enemy : public Unit
{
public:
	Enemy(void);
	~Enemy(void);

		void basicMove(string name,int damage,int target) 
	{
		target -= damage;

		cout << "Enemy did " << name << " to " << target << endl;
		cout << "Damage: " << damage << endl;
		cout << endl;
		system("pause");
		system("cls");
	}
	void healMove(string name,int damage,int target)
	{
		target -= damage; //Heal

		cout << "Enemy used " << name << endl;
		cout << "Heal Pts: " << damage << endl;
		cout << endl;
		system("pause");
		system("cls");
	}
	void braveburstMove(string name, int damage, int target)
	{
		target -= damage; // Special Attack(BraveBurst Move)

		cout << "Enemy did " << name << endl;
		cout << "Damage: " << damage << endl;
		cout << endl;
		system("pause");
		system("cls");
	}
};



NOTE: I didnt code anything in the cpp files of the classes(Unit,Player,Enemy).
Nov 30, 2015 at 4:05am
Hi,

A few things going on here:

target is just an argument to the function, you change it's value but that doesn't affect anything else.

Consider having an enemy as a the function argument. Have an interface function so that the health value will change. The health value needs to be a private variable.

Your functions are very similar, you could define them once in the base class. Only override them if necessary.

Don't have using namespace std; in header files, in fact don't have it at all - put std:: before each std thing - it's the best way :+) Google it.

Do put your class functions into their own cpp file. It is much more tidy and better that way.

Good Luck !!
Nov 30, 2015 at 6:56am
Hello TheIdeasMan, thank you for your reply,greatly appreciate it :)
Topic archived. No new replies allowed.