From e8a656e61e46635d319b2ba491fbb7c75e6eb496 Mon Sep 17 00:00:00 2001 From: "techdragon.nguyen@gmail.com" Date: Sun, 6 Feb 2011 12:42:55 +0000 Subject: [PATCH] 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 --- projects/mtg/src/ManaCost.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/projects/mtg/src/ManaCost.cpp b/projects/mtg/src/ManaCost.cpp index d052be601..0ab72045c 100644 --- a/projects/mtg/src/ManaCost.cpp +++ b/projects/mtg/src/ManaCost.cpp @@ -223,12 +223,11 @@ ManaCost::ManaCost() { init(); } + ManaCost::ManaCost(int _cost[], int nb_elems) { init(); - int i; - int total = nb_elems; - for (i = 0; i < total; i++) + for (int i = 0; i < nb_elems; i++) { cost[_cost[i * 2]] = _cost[i * 2 + 1]; } @@ -238,8 +237,7 @@ ManaCost::ManaCost(int _cost[], int nb_elems) ManaCost::ManaCost(ManaCost * _manaCost) { init(); - int i; - for (i = 0; i <= Constants::MTG_NB_COLORS; i++) + for (int i = 0; i <= Constants::MTG_NB_COLORS; i++) { cost[i] = _manaCost->getCost(i); } @@ -286,6 +284,10 @@ void ManaCost::init() FlashBack = NULL; Retrace = NULL; morph = NULL; + + // why is hybrids hardcoded to 10? + for (i = 0; i < 10; i++) + hybrids[i] = NULL; } void ManaCost::copy(ManaCost * _manaCost) @@ -425,9 +427,9 @@ int ManaCost::add(ManaCost * _cost) } for (unsigned int i = 0; i < _cost->nbhybrids; i++) { - hybrids[nbhybrids] = NEW ManaCostHybrid((*_cost->hybrids[i])); - nbhybrids++; + hybrids[i] = NEW ManaCostHybrid((*_cost->hybrids[i])); } + nbhybrids = _cost->nbhybrids; return 1; } @@ -640,7 +642,8 @@ string ManaCost::toString() for (unsigned int i = 0; i < nbhybrids; i++) { - oss << hybrids[i]; + if ( hybrids[i] != NULL ) + oss << hybrids[i]; } return oss.str(); } @@ -660,7 +663,6 @@ ostream& operator<<(ostream& out, ManaCost& m) return out << m.toString(); } - ostream& operator<<(ostream& out, ManaCost* m) { return out << m->toString();