1. Home
  2. Computing & Technology
  3. Visual Basic
Programming The Tic Tac Toe Game
Part 2: Initialization
 More of this Feature
• Part 1: Building the GUI
• Part 3: Making A Move
• Part 4: Finding a Winner
 
 Related Resources
• Upgrading the Tic Tac Toe program to VB.NET
• A GDI+ Introduction for Visual Basic 6 Programmers
• A Complete Rewrite of the program for VB.NET/GDI+
• Download the source code for this program
 

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.

Variables

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.

Module Variables

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.

One of the critical skills of a programmer is the ability to use the debugging facilities to understand what the code is doing. You can use this program to try
  • Stepping through the code with the F8 key
  • Setting a watch on key variables, such as sPlaySign or iMove
  • Setting a breakpoint and querying the value of variables. For example, in the inner loop of the 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
Line1.Visible = False
Line2.Visible = False
Line3.Visible = False
Line4.Visible = False
Line5.Visible = False
Line6.Visible = False
Line7.Visible = False

instead of this:

For i = 0 To 7
linWin(i).Visible = False
Next i

Next page > Making A Move > Page 1, 2, 3, 4
Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.