TGB/Tutorials/Fill Battle/Un-Common Code
From TDN
|
[edit] Un-Common CodeSo the class is done, and when you look over it, you see some ugliness. SHUDDER! Let's make some common functions and spruce things up. My biggest grievance is with all of the double arrays. In fillGridModel.h, let's add the following lines right below "DECLARE_CONOBJECT(FillGridModel);" protected: S32 **createArray(); void initArray( S32 **a, S32 value ); void assignArray( S32 **into, S32 **from ); void deleteArray( S32 **a ); "createArray" creates an array based upon the class's initialized size. "initArray" will fill the array with a single value. "assignArray" will copy the values from the "from" array into the "into" array. Finally, "deleteArray" will free the memory allocated from "createArray". Let's put these guys in the fillGridModel.cc just above the ConsoleMethods.
S32 **FillGridModel::createArray()
{
S32 **a;
a = new S32*[mySizeX];
for( S32 col = 0; col < mySizeX; ++col )
a[col] = new S32[mySizeY];
return a;
}
void FillGridModel::initArray( S32 **a, S32 value )
{
for( S32 col = 0; col < mySizeX; ++col )
for( S32 row = 0; row < mySizeY; ++row )
a[col][row] = value;
}
void FillGridModel::assignArray( S32 **into, S32 **from )
{
for( S32 col = 0; col < mySizeX; ++col )
for( S32 row = 0; row < mySizeY; ++row )
into[col][row] = from[col][row];
}
void FillGridModel::deleteArray( S32 **a )
{
for( S32 col = 0; col < mySizeX; ++col )
delete[] a[col];
delete[] a;
}
[edit] Use the ConvenienceNow we can change the "cleanup" function to the following:
void FillGridModel::cleanup()
{
if( myColor != NULL )
{
deleteArray( myColor );
myColor = NULL;
}
if( myOwner != NULL )
{
deleteArray( myOwner );
myOwner = NULL;
}
}
"initialize" will now look like:
void FillGridModel::initialize( S32 x, S32 y )
{
// In case we were already initialized, clean up the old stuff.
cleanup();
mySizeX = x;
mySizeY = y;
// Create an array for colors.
myColor = createArray();
// Assign random colors to the array.
for( S32 col = 0; col < mySizeX; ++col )
for( S32 row = 0; row < mySizeY; ++row )
myColor[col][row] = gRandGen.randI( 0, 5 );
// Create an array for owners.
myOwner = createArray();
// Assign "-1" to all owners to show a lack of ownership.
initArray( myOwner, -1 );
// Reset the scores.
myScore[0] = 0;
myScore[1] = 0;
// An easy way to assign the initial ownership and scores.
floodFill( 0, myColor[0][0] );
floodFill( 1, myColor[mySizeX-1][mySizeY-1] );
}
We can also touch up "floodFill".
void FillGridModel::floodFill( S32 owner, S32 color )
{
// Create a "visited" array to keep track of where we've been.
S32 **visited = createArray();
// Initialize the "visited" array to false.
initArray( visited, false );
...
// Switch owners
// Initialize the "visited" array to false.
initArray( visited, false );
...
// Delete the visited array.
deleteArray( visited );
}
You'll notice that we changed the visited array from bool to S32. An unfortunate effect of almost all C++ implementations is that bools take up the same size as a S32 (32 bits to be specific), so we're not losing out on anything by the switch and we gain the nifty functions! (I was going to use function templates, but decided to stick with simplicity.) [edit] Complete! |



