developer's mag
main page
article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
|
2 - Program Design
Although hacking a program together is often
an easy way to generate code, it always helps
to deal with the program design and actual
device implementation separately, at least
until many of the details are fleshed out.
While not really an issue with the small
example program considered here (Tic-Tac-Toe),
it still is a good habit to get into.
While the game itself needs no explanation,
the rough design details do:
- We keep one array for cell position status, called
m_owner. To make life simpler for us, we
assume that player one ('X') is a value
of
one, player two ('O') is two, and an unused
(available) square is zero.
- We have one routine to check the game state,
CheckGame(). Although not good programming
practice, for simplicity we overload the
return value - zero indicates an unfinished game,
-1 is a draw, while 1 and 2 indicate the
game's
winner.
- Keeping track of player moves is done with
the variable 'm_move', which we increment
after each turn. Since the game is two
player,
the m_move variable also allows us to determine
the current player easily (with m_move&1).
- Cell selection requests (via a function call)
give us the opportunity to check for game
completion, and display the result (win
or
draw).
- We use a state variable to determine where
we are in each game, called m_state.
Previous Section
Next Section
|