home | |||
links |
|
||
developer's mag main page article part 1 part 2 part 3 part 4 part 5 part 6 part 7 part 8 part 9 |
3 - Civil CodeA polite program needs to know the difference between a game in progress and a new game. Obviously, if you haven't started the game, why ask to end it? On the other hand, customers complain (and rightly so) if an errant menu selection quits the game by accident. In our case, we already have enough information to determine if a game is in progress; the 'm_move' variable is zero before a game starts, and non-zero thereafter. On the other hand, the 'm_state' value is STATE_GAME_START, and becomes STATE_GAME_OVER only at the end. Taken together, the two tests let us know when we are in the middle of a game:bool paTicTacToe::CurrentlyPlaying(void) { // simple test - if we haven't moved, or game over, // not playing; otherwise, we are into it return ( m_move && STATE_GAME_START==m_state ); }Although it needn't be separated out, I've made it a function so that as the game expands the condition we are testing remains the same, although the flags and settings may vary (the state values, for instance). Localizing tests such as this into functions eases code modification. Previous Section Next Section |
||
Copyright © 2001-2006 ebmDevMag.com - Legal Notice |