My Dynamic Memory is affecting my non-dynamic values.

[tt]Okay, so i'm implementing a playerclass for a game.. in the game there are multiple player types, weapons ect.. i just wanted to turn my players weapons into a dynamically allocated c_str. once i added my: Destructor, Copy Constructor and Overloaded Assignment Operator. My initial values became corrupted and i cannot fix them.. any suggestions?

Player2.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <string.h>
#include <iostream>
#include <cstdlib>
#include <iostream>
#include "player2.h"
#include "dice.h"
#include "gamespace.h"

using namespace std;

PlayerClass::PlayerClass(){
    string name = "DEFAULT";
    SetSpider(name);
    weaponName = NULL;    
    return;}

PlayerClass::~PlayerClass()
{
    delete[] weaponName;
}
PlayerClass::PlayerClass(string player_name, char kind = 'S'){
    int i = 0;    
    if((player_name.length() > 20) || (player_name.length() < 1)) 
    player_name = "DEFAULT";    
    while(isspace(player_name.at(i)))
    i++;    
    player_name = player_name.substr(i,player_name.length());
      
    while(isspace(player_name[player_name.length()-1]))
    player_name.erase(player_name.length()-1);
 

    return;
}

bool PlayerClass::Active(){
    active = false;
    if(health>0)
    active = true;
    return active;}
bool PlayerClass::InActive(){
    active = false;    
    return active;}
bool PlayerClass::Dead(){
    health = 0;
    willpower = 0;
    active = false;
    return true;
}
bool PlayerClass::Heal(){
    if(active)    {
    health += 10;
    if(health>maxHealth)
        health = maxHealth;
    currentSpeed = maxSpeed;   }
    return active;
}
int PlayerClass::ResetMomentum(){
    momentum = currentSpeed;
    return momentum;}
int PlayerClass::UseMomentum(int num){
    if((momentum - num)<0){
    num = momentum;
    momentum = 0;  }
    else
    momentum -= num;
    return num;}

string PlayerClass::Name() const
{
    return name;
}
char PlayerClass::Type() const
{
    char returnChar;
}
int PlayerClass::Health() const
{
    return health;
}
int PlayerClass::WillPower() const
{
    return willpower;
}
int PlayerClass::MaxSpeed() const
{
    return maxSpeed;
}
int PlayerClass::CurrentSpeed() const
{
    return currentSpeed;
}
int PlayerClass::Power() const
{
    return power;
}
int PlayerClass::Armor() const
{
    return defArmorValue;
}
int PlayerClass::Momentum() const
{
    return momentum;
}
bool PlayerClass::IsAlive() const
{
    return (health>0);
}
bool PlayerClass::IsDead() const
{
    return (health<=0);
}
bool PlayerClass::IsActive() const
{
    return active;
}

PlayerClass::PlayerClass(const PlayerClass& p){
    weaponName = new char;
}

PlayerClass& PlayerClass::operator= (const PlayerClass& p){
    if(this != &p){
        delete[] weaponName;
        weaponName = NULL;
        if(p.weaponName != NULL){
            weaponName = new char[strlen(p.weaponName)+1];
            strcpy(weaponName, p.weaponName);
        }
    }
    return *this;
}


bool PlayerClass::operator == (const PlayerClass& p)//COMPLETE
{
    string first, second;
    
    for(int i=0;i<name.length();i++)
    first += tolower(name[i]);
    
    for(int i=0;i<p.name.length();i++)
    second += tolower(p.name[i]);
    
    return first == second;
}
bool PlayerClass::operator != (const PlayerClass& p)//COMPLETE
{
    string first, second;
    
    for(int i=0;i<name.length();i++)
    first += tolower(name[i]);
    
    for(int i=0;i<p.name.length();i++)
    second += tolower(p.name[i]);
    
    return first != second;
}
bool PlayerClass::operator < (const PlayerClass& p)
{
    string first, second;
    
    for(int i=0;i<name.length();i++)
    first += tolower(name[i]);
    
    for(int i=0;i<p.name.length();i++)
    second += tolower(p.name[i]);
    
    return first < second;
}
bool PlayerClass::operator > (const PlayerClass& p)
{
    string first, second;
    
    for(int i=0;i<name.length();i++)
    first += tolower(name[i]);
    
    for(int i=0;i<p.name.length();i++)
    second += tolower(p.name[i]);
    
    return first > second;
}
bool PlayerClass::operator <= (const PlayerClass& p)
{
    string first, second;
    
    for(int i=0;i<name.length();i++)
    first += tolower(name[i]);
    
    for(int i=0;i<p.name.length();i++)
    second += tolower(p.name[i]);
    
    return first <= second;
}
bool PlayerClass::operator >= (const PlayerClass& p)
{
    string first, second;
    
    for(int i=0;i<name.length();i++)
    first += tolower(name[i]);
    
    for(int i=0;i<p.name.length();i++)
    second += tolower(p.name[i]);
    
    return first >= second;
}


void PlayerClass::SetWarrior(string str){
    health = 81;
    maxHealth = 81;
    willpower = 9;
    maxSpeed = 5;
    currentSpeed = 5;
    momentum = 0;
    power = 9;
    physicalDiceNumber = 3;
    physicalDiceSides = 6;
    defArmorValue = 8;
    weaponSkill = 5;
    isHuman = true;
    active = false;
    weapon = ASSAULTRIFLE;
    wepName = "Assault Rifle";
    weaponName = new char[(wepName.length() + 1)];
    strcpy(weaponName, wepName.c_str());
    type = WARRIOR;
    name = str;
    SetCell(0,0);
}
void PlayerClass::SetExplorer(string str) 
    health = 42;
    maxHealth = 42;
    willpower = 7;
    maxSpeed = 8;
    currentSpeed = 8;
    momentum = 0;
    power = 6;
    physicalDiceNumber = 1;
    physicalDiceSides = 6;
    defArmorValue = 0;
    weaponSkill = 10;
    isHuman = true;
    active = false;
    weapon = PISTOL;
    wepName = "Pistol";
    weaponName = new char[(wepName.length() + 1)];
    strcpy(weaponName, wepName.c_str());
    type = EXPLORER;
    name = str;
    SetCell(0,0);       
}

int PlayerClass::SlowDown(int hazard) //COMPLETE
{
    currentSpeed = currentSpeed - hazard;
    
    if(currentSpeed <= 0)
        currentSpeed = 0;
    if(momentum > currentSpeed)
        momentum = currentSpeed;
    
    return currentSpeed;
}

int PlayerClass::Wounded(int damage) //COMPLETE{
    int Health = health - damage;
    if(Health <= 0 || willpower <=0){
        Health = 0;
        Dead();
    } else {
        health = Health;    }
    
    return Health;}

int PlayerClass::Damage() //COMPLETE
{
    int damage = 0;
    if(damage < 1){ //weaponName != NULL){
        damage = WPNDAM + weaponSkill;
        cout << endl << endl << "WEAPON DAMAGE DEALT: ";  //for testing purposes ONLY!!!!!
    } else {
        cout << endl << endl << "PHYSICAL DAMAGE DEALT: "; //for testing purposes ONLY!!!!!
        damage = Dice::Roll(name, IMPACT, physicalDiceNumber, physicalDiceSides);
    }
    return damage;
}

const int PlayerClass::WeaponSkill() //COMPLETE
{
    int skill = 0;
    
    if(isHuman)
        skill = weaponSkill;
        
    return skill;
}
const bool PlayerClass::HasWeapon(){
    bool hasWeapon = false;    
    if(weaponName != NULL)
        hasWeapon = true;    
    return hasWeapon;}
int PlayerClass::Impact() //COMPLETE{
    int impact = 0;
    int diceRoll = Dice::Roll(name, IMPACT, physicalDiceNumber, physicalDiceSides);
    int tmpPower = power;
    if(active = true)    {
        if( power > 1)        {
            if(HasWeapon())
                tmpPower = WPNPOW;
            if(0 < diceRoll || diceRoll <= 12){
                impact = IMPACT_TABLE[tmpPower][diceRoll];
                cout << endl << endl << "Impact: ";  //for testing purposes ONLY!!!!!
                if( 10 < impact < 0)
                    impact = -1;    }        }        }
    return impact;
}

bool PlayerClass::CriticalWound() //COMPLETE
{    
    if(health > 10){
        int diceRoll = Dice::Roll(name, WOUND, physicalDiceNumber, physicalDiceSides);
            int critical = WOUND_TABLE[WPNPOW][diceRoll];
            
    return IsAlive();
}
//*******************************************\\

bool PlayerClass::DropWeapon() //COMPLETE
{
    delete[] weaponName;
    weaponName = NULL;
    return isHuman;
}
bool PlayerClass::GrabWeapon(string weapon, int skill)
{
    bool weaponGrabbed = false;
    if(IsActive() & isHuman & skill > 0)
        DropWeapon();
        if((weapon.length()) > 0){
            weaponName = new char[(weapon.length() + 1)];
            strcpy(weaponName, weapon.c_str());
            weaponGrabbed = true;}
    return weaponGrabbed;
}

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef PLAYER_2_H
#define PLAYER_2_H

#include <iostream>
#include <string>

using namespace std;

enum PlayerTypeT{EXPLORER, WARRIOR, HORNET, KILLERANT, SPIDER}; //The type of the player
enum WeaponT{PISTOL, ASSAULTRIFLE, GRENADE, HEAVYWEAPON, NULLWEAPON}; //The weapons that// players can use

static const int WPNDAM = 6; //Temporary
static const int WPNPOW = 5; //Temporary

static const int IMPACT_TABLE[13][13] = {
/*Power:      0  1  2  3  4  5  6  7  8  9  10 11 12 */
/*Roll: 0*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/*Roll: 1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/*Roll: 2*/ { 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
/*Roll: 3*/ { 0, 0, 1, 2, 3, 4, 5, 6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*/ { 0, 0, 1, 1, 1,-1,-1,-1,-1,-1,-1,-1,-1},
/*Roll:10*/ { 0, 0, 1, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*Roll:11*/ { 0, 0, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*Roll:12*/ { 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
}; //(note: 1 = kill)

struct CellT{int col; int row;}; //Used to store where the player is on the game board

class PlayerClass{

    public:
	PlayerClass(string player_name, char kind); //Constructor; sets stats to those of the type and the name to str
		
	~PlayerClass();//Destructor, Removes all dynamic portions of the class
				
	PlayerClass(const PlayerClass& p);
	
	PlayerClass& operator = (const PlayerClass& p);
	
	bool Active(); //Sets the player to active, but only if they are alive
		    
	bool InActive(); //Sets the player to inactive
			
	bool Dead(); //Sets a players health and willpower to 0 and sets them to inactive
		
	bool Heal(); //Raises a players health by 10. If the new health would exceed max health, health is set to max health
		
	int ResetMomentum(); //Sets a player's momentum equal to their current speed
			
	int UseMomentum(int num); //Adjusts a player's momentum after an action is completed
			
	bool SetCell(int newRow, int newCol); //Places a player at a valid coordinate on the board
		    
	int Row() const;//Observer for the current row of the player
		
	int Col() const;//Observer for the current column of the player
		
	string Name() const;//Observer for the name of the player
			
	char Type() const;//Observer for the type of the player; returns a corresponding capital letter to their type
			
	int Health() const;//Observer for the health of the player
		
	int WillPower() const;//Observer for the willpower of the player
		
	int MaxSpeed() const;//Observer for the maximum speed of the player
		
	int CurrentSpeed() const;//Observer for the current speed of the player

	int Power() const;//Observer for the power of the player

	int Armor() const;//Observer for the armor of the player

	int Momentum() const;//Observer for the momentum of the player

	bool IsAlive() const;//Observer for the player's condition; returns true if alive, false if dead

	bool IsDead() const;//Observer for the player's condition; returns true if dead, false if alive
	
	bool IsActive() const;//Observer for the player's activity; returns true if active, false if inactive
	
	bool IsMyEnemy(PlayerClass *p);//Form of comparing players; true if one player is human and the other is a bug or if
			//both players are bugs & they are different types of bugs

	int SlowDown(int hazard);//Reduces the players current speed by the hazard
currentspeed=momentum
	int ResetCurrentSpeed();//Sets players currentspeed to MaxSpeed
		
	int Wounded(int damage);//Removes the damage from the players health if possible
	int Damage();//Determines the amount of damage dealt by a player
		
	int Impact();//Determines the impact of a critical wound done on a player

	const int WeaponSkill();//Returns the value of a players WeaponSkill
	
	const string WeaponName();//Returns the name of the weapon currently being held
			
	const bool HasWeapon();//Verifies if the player is holding a weapon or not
			
	bool CriticalWound();//If a critical wound is hit, this applies the results 
			
	bool DropWeapon();//Drops the weapon if the player has one
			
	bool GrabWeapon(string weapon, int skill);//Grabs a weapon if the player is elidgable
			
	

//    private:
    PlayerClass(); //Default Constructor; sets stats to Spider with name "Default"
	void CopyPlayer(PlayerClass* to, const PlayerClass* from); //Copy Constructor for this class.
		
	string wepName;
	string name;
	int health;
	int maxHealth;
	int willpower;
	int maxSpeed;
	int currentSpeed;
	int momentum;
	int power;
	int physicalDiceNumber
	int physicalDiceSides;
	int defArmorValue;
	int weaponSkill;
	char * weaponName;
	bool isHuman;					
	bool active;					
	WeaponT weapon;					
	PlayerTypeT type;				
	void SetWarrior(string str);//Sets the players stats to those of a warrior with name of str
	void SetExplorer(string str);//Sets the players stats to those of a explorer with name of str
	void SetKillerAnt(string str);//Sets the players stats to those of a killerant with name of str
	void SetSpider(string str);//Sets the players stats to those of a spider with name of str
	void SetHornet(string str);//Sets the players stats to those of a hornet with name of str
	
};

#endif 





TestDriver.C
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
#include "player2.h"
#include "gamespace.h"
#include <iostream>
using namespace GameSpace;
using namespace std;
void Display(PlayerClass p, string Change){
        cout << endl << "Testing: " << Change << endl << endl;
        cout << "Name: " <<  p.name  << endl;
        cout << "Health: " << p.health << endl;
        cout << "Max Health: " << p.maxHealth << endl;
        cout << "Max Speed: " <<  p.maxSpeed << endl;
        cout << "Current Speed: " <<p.currentSpeed << endl;
        cout << "Power: " << p.power << endl;
        cout << "Willpower: " <<  p.willpower << endl;
        cout << "Active: "  <<p.active << endl;
        cout << "Momentum: " << p.momentum << endl;
//      cout << "Player Has Weapon: " << p.HasWeapon() << endl;
        cout << "Weapon Skill: " << p.weaponSkill << endl;
//      if(p.HasWeapon())
//              cout << "Weapon Name: " << p.weaponName << endl;
        cout << endl << endl << endl;

        return;
}


int main(){

        PlayerClass WARRIOR = PlayerClass("Warrior", 'W');
        WARRIOR.name = "SAMMY";
        Display(WARRIOR, "STATS");
Topic archived. No new replies allowed.