ManaCost hybrids memeber array wasn't being initialized giving each element of the array an undefined value.

Initialized hybrids array to contain NULL elements and toString now checks for NULL pointers before attempting to print out hybrids.
Issue: 586
This commit is contained in:
techdragon.nguyen@gmail.com
2011-02-06 12:42:55 +00:00
parent a717bae839
commit e8a656e61e
+10 -8
View File
@@ -223,12 +223,11 @@ ManaCost::ManaCost()
{ {
init(); init();
} }
ManaCost::ManaCost(int _cost[], int nb_elems) ManaCost::ManaCost(int _cost[], int nb_elems)
{ {
init(); init();
int i; for (int i = 0; i < nb_elems; i++)
int total = nb_elems;
for (i = 0; i < total; i++)
{ {
cost[_cost[i * 2]] = _cost[i * 2 + 1]; cost[_cost[i * 2]] = _cost[i * 2 + 1];
} }
@@ -238,8 +237,7 @@ ManaCost::ManaCost(int _cost[], int nb_elems)
ManaCost::ManaCost(ManaCost * _manaCost) ManaCost::ManaCost(ManaCost * _manaCost)
{ {
init(); init();
int i; for (int i = 0; i <= Constants::MTG_NB_COLORS; i++)
for (i = 0; i <= Constants::MTG_NB_COLORS; i++)
{ {
cost[i] = _manaCost->getCost(i); cost[i] = _manaCost->getCost(i);
} }
@@ -286,6 +284,10 @@ void ManaCost::init()
FlashBack = NULL; FlashBack = NULL;
Retrace = NULL; Retrace = NULL;
morph = NULL; morph = NULL;
// why is hybrids hardcoded to 10?
for (i = 0; i < 10; i++)
hybrids[i] = NULL;
} }
void ManaCost::copy(ManaCost * _manaCost) void ManaCost::copy(ManaCost * _manaCost)
@@ -425,9 +427,9 @@ int ManaCost::add(ManaCost * _cost)
} }
for (unsigned int i = 0; i < _cost->nbhybrids; i++) for (unsigned int i = 0; i < _cost->nbhybrids; i++)
{ {
hybrids[nbhybrids] = NEW ManaCostHybrid((*_cost->hybrids[i])); hybrids[i] = NEW ManaCostHybrid((*_cost->hybrids[i]));
nbhybrids++;
} }
nbhybrids = _cost->nbhybrids;
return 1; return 1;
} }
@@ -640,6 +642,7 @@ string ManaCost::toString()
for (unsigned int i = 0; i < nbhybrids; i++) for (unsigned int i = 0; i < nbhybrids; i++)
{ {
if ( hybrids[i] != NULL )
oss << hybrids[i]; oss << hybrids[i];
} }
return oss.str(); return oss.str();
@@ -660,7 +663,6 @@ ostream& operator<<(ostream& out, ManaCost& m)
return out << m.toString(); return out << m.toString();
} }
ostream& operator<<(ostream& out, ManaCost* m) ostream& operator<<(ostream& out, ManaCost* m)
{ {
return out << m->toString(); return out << m->toString();