2006-03-16

The Set-up functions

Step 3: The first set-up function is used to allocate the Rectangle array places and the int array states. It is only called when the size of the playing area or of the color cycle needs changing. of course the program start up calls this function as well.

boolean setUp(void)

// preconditions
Assert that the Board instance pointer is not NULL. ( redundant, but just in case )
Assert that the Board size value is within specified limits

If the places array exists, delete it.
If the states array exists, delete it.

Create the new Rectangle array using the Board's size value (squared) and asign it to places
Create the new Rectangle array using the Board's size value (squared) and asign it to states

Assert that places is not NULL. (Debug)
If places is NULL, return failed. (Release)

Using the constant values for the border, cell spacing and cell size, create in places an x-by-x square of Rectangles.

// post conditions
Assert that the Board instance is still not NULL.

Return success.

Step 4: The other set-up function is a reset or initialization function for new games. As everything else is set up all this function needs to do is to set the values in the states array to 0 (Black) and initialize any other game values in the Board instance.
The pre-conditions are that the Board instance exists, and that there is a valide states pointer.
The post-condition is that the Board instance still exists.

Step 5: Of course a function is needed to set the Board instance's field size and number of colours variables.

Step 6: As I am using the Microsoft View-Document model, I put the set-up calls in the Document class's OnNewDocument function.

// the variables denoting a change in the Board instance were set to true in the Document's constructor
If there was a change in the size or number of colours do{

Set the Board instance's size and number of colours.

CBoard::getInstance()->setUp();

Set the Documents variables for a changed Board to false.
}

Reset the Board instance to game starting values.

Of course, if the Board instance's setUp() failed, this will have to be passed on to the Document's base class for handling as an error.

No comments: