| home | |||
|
|||
| links |
|
||
|
developer's mag main page article part 1 part 2 part 3 part 4 part 5 part 6 part 7 |
5 - Implementation DetailsHandling menu options is very easy in this program - with only two options (new game and exit), the initialization and handling code is quite small:m_menu = new CMenu(ID_MENU,0,32767,0,this); int row=-1; m_menu->SetNumRows( 3 ); m_menu->SetRow( ++row, IDM_NEWGAME, "New Game"); m_menu->SetSeparatorRow(++row); m_menu->SetRow( ++row, IDM_EXIT, "Exit");The row variable is a simple program maintenance trick; as I add menu items, I don't have to edit older entries to adjust rows. However, I do need to remember to adjust the value passed to SetNumRows(), and for this reason I recommend placing an assertion here. Its companion routine, HandleMenu(), simply deals with the two valid menu options:
void paTicTacToe::HandleMenu(CViewable *from,S32 data)
{
switch ((U16) data)
{
case IDM_EXIT:
Close();
break;
case IDM_NEWGAME:
NewGame(true);
break;
default:
break;
}
}
Exiting closes the program, and new game
starts up a new game. NewGame() simply clears
all the variables and arrays: void paTicTacToe::NewGame(bool brandNew)
{
int i=0;
for (i=0;i<9;i++)
m_owner[i]=0;
m_state=STATE_GAME_START;
m_move=0;
GUI_NeedUpdate(); // indirectly calls Draw() to display cleared game
}
A new game has the grid cleared, m_move set
to zero, state reset, etc. But what of the
boolean 'brandNew'? While not needed in this
game, many games have two styles of resetting
- playing a brand new game, and replaying
the current game from the beginning. In my
experience, the code for each is very similar,
and putting it in one function with a flag
makes for simpler game code. The reason is
that games often use a seed value to begin
randomization - a brand new game uses a new
seed, while replaying the current game simply
means reusing the current seed. Since this
is the common way to code this function in
my games, I just reused the function with
the unused parameter (you'll notice the program
has an unused destructor function for exactly
the same reason). One key to quick programming
is to reuse code whenever possible.Two more implementation details are apparent in the Initialize() call. For one, we set up a pointer to a font for display - we chose a large bold font, and saved the pointer rather than generating it each time we plot an X or O. This avoids using extra cycles in our drawing code. The second point involves initializing the m_rect array, and segues nicely into our pen message handler. Previous Section Next Section |
||
| Copyright © 2001-2006 ebmDevMag.com - Legal Notice | |||