2006-04-16

Mu Torere, the Playing Board

Step 3,
As a starting point, the Board class is a good one. Once this class is set-up, the View can be set to right size and to draw. There are two of the class's functions right there, getSize(void) const const and Draw(CDC *pDC).

The Board class will use the Singleton pattern, so getInstance(void) and Release(void) will need to be added.
Other functions needed for the game are getPlace(const int&) const to return a rectangle for drawing playing pieces, whatIndex(const CPoint&) const for determining valid mouse clicks, and canMove(const int& from, const int& to) const for checking for valid neighbours.

In addition to the Singleton's instance, the Board class also needs an array for the program's playable CRect's, places[CBoard::NUM_PLACES].
The named constants are OFF_BOARD = -1 (a public constant, not really needed in the Board class but a habit), NUM_PLACES = 9 (private, used for readability), and BORDER = 20 (private).

Now all of the numbers used in this class were determined by hand because of the general awkwardness of calculating the positions of the 8 points of the star plus the indents. (Actually I use a positioning kluge to come up with the right numbers, but I will not subject the readers to that mess.)


Constructor
CBoard::CBoard(void)
places[0].SetRect(80,20,130,70);
places[1].SetRect(160,20,210,70);
places[2].SetRect(220,95,270,145);
places[3].SetRect(220,180,270,230);
places[4].SetRect(160,250,210,300);
places[5].SetRect(80,250,130,300);
places[6].SetRect(20,180,70,230);
places[7].SetRect(20,95,70,145);
places[8].SetRect(120,130,175,185);


For the Singleton pattern;

CBoard* CBoard::getInstance(void)
Is instance NULL?
{
Try allocating new CBoard instance.

If allocation fails, set instance to NULL.
}
Return instance.


void CBoard::Release(void)
Is instance not-NULL?
{
Delete instance.

Set instance to NULL.
}


For the View;
CSize CBoard::getSize(void) const
Pre-Condition:
instance is not-NULL.
Is Return CSize(right-most point+CBoard::BORDER,bottom-most point+CBoard::BORDER)


void CBoard::Draw(CDC* pDC)
Pre-Condition:
instance is not-NULL.
Create a solid brush with the colour of the window's background.
Create a pen with the colour of the menu's text.
(This should provide enough contrast.)

Select into drawing context the pen and brush.

Draw a circle in the rectangle given by place[8].

Move to center of place[0].
Draw line to (145,95).
Draw line to center of places[1].
Draw line to (185,120).
Draw line to center of places[2].
Draw line to (205,160).
Draw line to center of places[3].
Draw line to (185,205).
Draw line to center of places[4]
Draw line to (145,230).
Draw line to center of places[5].
Draw line to (105,205).
Draw line to center of places[6].
Draw line to (90,160).
Draw line to center of places[7].
Draw line to (105,120).
Draw line to center of places[0].

Select into drawing context the old pen and brush.

Note: As an alternative playing field, a 8-spoked wheel can also be draw. Just draw the center last to keep it from being over-drawn.

No comments: