I'm working on a simple console application to prototype a method of calculating battle between two large units of medieval soldiers. The idea is I'll set up the calculations, and run the program several times with different parameters to get a feel for it and if it's what I want. Anyways, on to the problem.
Each of the two units is represented by a Unit class. The class includes variables for "attack", "defense", "tactics", and "unit size" as well as a few others which I'm commenting out and ignoring until I fix this issue. Here's the code for the class header:
#ifndef UNITS_H
#define UNITS_H
#include <string>
#include <iostream>
class Unit
{
public:
int attack;
int defense;
int charge;
int brace;
int tactics;
int unit_size;
public:
bool isAlive;
std::string name;
Unit(std::string name_target)
{
name = name_target;
attack = 1;
defense = 1;
charge = 1;
brace = 1;
tactics = 1;
unit_size = 1;
isAlive = true;
}
float GetAttack() {return (float)attack;}
float GetDefense() {return (float)defense;}
float GetCharge() {return (float)charge;}
float GetBrace() {return (float)brace;}
float GetTactics() {return (float)tactics;}
float GetSize() {return (float)unit_size;}
void SetAttack(int change) {attack = change;}
void SetDefense(int change) {defense = change;}
void SetCharge(int change) {charge = change;}
void SetBrace(int change) {brace = change;}
void SetTactics(int change) {tactics = change;}
void SetSize (int change) {unit_size = change;}
void TakeCasualties (int casualties);
int Defend(Unit enemy, float base_chance, float base_multiplier);
void DisplayStats();
};
#endif // UNITS_H
Sorry if it's sloppy, but I'm a bit of an amateur.
I would include the .cpp code for the class, but everything in there works fine. I've done test runs with just the default values with no issue. The only problem I have is changing the values after they're constructed.
In main.cpp I have a function AssignStats(Unit unit_target) which I call once the two Units are constructed. I call it on each one in turn. The code for that is as follows:
void AssignStats (Unit unit_target)
{
int x;
cout << unit_target.name << endl;
cout << "Value for attack?" << endl;
cin >> x;
cout << x << endl;
unit_target.SetAttack(x);
cout << "Value for defense?" << endl;
cin >> x;
unit_target.SetDefense(x);
//unit_target.SetCharge(x);
//unit_target.SetBrace(x);
cout << "Value for tactics?" << endl;
cin >> x;
unit_target.SetTactics(x);
cout << "Value for size?" << endl;
cin >> x;
unit_target.SetSize(x);
}
As far as I can tell, this code should work. However, when I display the stats of each Unit afterwards, it shows the default values put in by the constructor. For the life of me I can't figure out why my Set functions aren't working. I rewrote the code a little earlier to check if they were getting input right, like so:
void Unit::SetAttack()
{
int x;
std::cout << "Input target value for attack." << std::endl;
std::cin >> x;
std::cout << x;
attack = x;
std::cout << attack;
}
I would input 10, and each of the couts would show me 10 right back, even the one that supposedly displayed the attack variable of the class, but later on when I called DisplayStats(), it would show everything to be at default values again.
Can someone more experienced than me please clear this up?
I think that the problem is that function AssignStats
accepts the argument by value
void AssignStats (Unit unit_target);
That is the function deals with a copy of the original object. The original object itself is not changed.
Change it the following way
void AssignStats (Unit &unit_target);