1. Home
  2. Computing & Technology
  3. Visual Basic
Programming The Tic Tac Toe Game
Part 3: Making A Move
 More of this Feature
• Part 1: Building the GUI
• Part 2: Initialization
• 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
 

If any part of the system can be thought of as 'the heart', it's subroutine lblPlayGround_Click. This subroutine is called every time a player clicks the playing grid. (Clicks must be inside one of the nine lblPlayGround elements.) Notice that this subroutine has an argument: (Index As Integer). Most of the other 'event subroutines', like cmdNewGame_Click() do not. Index indicates which label object has been clicked. For example: Index would contain the value zero for the top-left corner of the grid and the value eight for the bottom-right corner.

After a player clicks a square in the game grid, the command button to start another game, cmdNewGame, is "turned on' by making it visible. The state of this command button does double duty because it's also used as a boolean decision variable later in the program. Using a property value as a decision variable is usually discouraged because if it ever becomes necessary to change the program (say, for example, to make the cmdNewGame command button visible all the time), then the program will unexpectedly fail because you might not remember that it's also used as part of the program logic. For this reason, it's always a good idea to search through program code and check the use of anything you change when doing program maintenance, even property values. This program violates the rule partly to make this point and partly because this is a relatively simple piece of code where it's easier to see what is being done and avoid problems later.

A player selection of a game square is processed by calling the GamePlay subroutine with Index as the argument.

Processing the Move

First, we check to see if an unoccupied square was clicked.

If lblPlayGround(xo_Move).Caption = "" Then

Once we're sure this is a legitimate move, the move counter (iMove) is incremented. The next two lines are very interesting since they translate the coordinates from the one-dimensional If lblPlayGround component array to two-dimensional indexes that we can use in either iXPos or iOPos. Mod and integer division (the 'backslash') are mathematical operations that you don't use everyday, but here's a great example showing how they can be very useful.

 If lblPlayGround(xo_Move).Caption = "" Then
  iMove = iMove + 1
  x = Int(xo_Move / 3) + 1
  y = (xo_Move Mod 3) + 1

The xo_Move value 0 will be translated to (1, 1), 1 to (1, 2) ... 3 to (2, 1) ... 8 to (3, 3).

The value in sPlaySign, a variable with module scope, keeps track of which player made the move. Once the move arrays are updated, the label components in the playing grid can be updated with the appropriate sign.

  If sPlaySign = "O" Then
   iOPos(x, y) = 1
   iWin = CheckWin(iOPos())
  Else
   iXPos(x, y) = 1
   iWin = CheckWin(iXPos())
  End If
  lblPlayGround(xo_Move).Caption = sPlaySign

For example, when the X player clicks the top left corner of the grid, variables will have following values:

Player X Move

The values changes when the O player clicks the center square of the grid:

Player O Move

Now that we know where a player clicked, and which player did the clicking (using the value in sPlaySign), all we have to do is find out if someone won a game and figure out how to show that in the display. All of this will be revealed on the next page!

Next page > Finding a Winner > 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.