| Programming The Tic Tac Toe Game | |
| Part 2: Initialization | |
|
In this page, we finally start coding our program. If you haven't already, you might want to download the source code to follow along as the operation of the program is explained. One of the first design decisions to make is how to keep track of the current 'state' of the game. In other words, what are the current X's and O's on the playing grid and who moves next. The concept of 'state' is critical in a lot of programming, and in particular, it's important in programming ASP and ASP.NET for the web There are several ways that this could be done, so it's a critical step in the analysis. If you were solving this problem on your own, you might want to draw a flow chart and try out different options with 'scratch paper' before starting any coding. Our solution uses two 'two dimensional arrays' because that helps keep track of 'state' by simply changing the array indexes in program loops. The state of the top-left corner will be in the array element with index (1, 1), the top-right corner will be in (1, 3), the bottom-right in (3,3), and so forth. The two arrays that do this are: iXPos(x, y) and iOPos(x, y) There are a lot of different ways this can be done and the final VB.NET solution in this series shows you how to do it with just a single one dimensional array. The programming to translate these arrays into player win decisions and visible displays in the form are on the next page. We also need a few global variables as follows. Notice that these are in the General and Declarations code for the form. This makes them "module level" variables that can be referenced anywhere in the code for this form. For more on this, check Understanding the Scope of Variables in Visual Basic Help. There are two areas where variables are initialized in our program. First, a few variables are initialized while the form frmTicTacToe is loading. Private Sub Form_Load() Second, before each new game, all variables that need to be reset to starting values are assigned in an initialization subroutine. Sub InitPlayGround() Note that the form load initialization also calls the playground initialization. lblPlayGround((i - 1) * 3 + j - 1).Caption = "" Note that this program clearly shows why it's a good programming practice to keep data in arrays whenever possible. If we did not have arrays in this program, we would have to write code something like this:
Line0.Visible = False
For i = 0 To 7 |
|

