Programming Tic-Tac-Toe In Visual Basic.NET

Written By: Kevin Jordan

- 10 Feb 2006 -

Description: We'll start by using the visual editor to set up our interface and take it all the way into programming the artificial intelligence of the computer opponent.

  1. Creating the User Interface
  2. Starting a Game and Making a Menu
  3. Programming the Game Logic
  4. An Intelligent Opponent

Time for Xs and Os

Well, I've programmed a lot of code, but still can't play tic-tac-toe. Here's where that all changes.

Private Sub lblGrid00_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid00.Click
        Call GridClick(0, 0)
End Sub ' lblGrid00_Click 

I created a subroutine for each label in the 3x3 grid that looks pretty close to this. Seems pretty simple, but let's take a look at what GridClick() does.

Programming the Meat

The first thing to do when a square is clicked, is make sure that the square isn't occupied and also that a winner hasn't already been declared.

If (arrGrid(intRow, intCol) = 0) And (intWhosWinner = 0) Then
 
End If

If the spot on the grid is empty and the game is still being played, then we'll give the square to whoever's turn it is.

If intWhosTurn = 1 Then
        arrGrid(intRow, intCol) = 1
        intWhosTurn = 2
ElseIf intWhosTurn = 2 Then
        arrGrid(intRow, intCol) = 2
        intWhosTurn = 1
End If
 
Call DrawGrid()

After that the display is redrawn by calling the DrawGrid() subroutine.

Call CheckWinLose()
 
If (intWhosTurn = 2) And (intWhosWinner = 0) Then
        Call AIMove()
End If

Near the end of each move, the program will need to check whether someone has got three in a row. CheckWinLose() will handle this and I'll show you the code in just a minute. If there isn't a winner and the player just moved, then it's time for the computer opponent. Once again, if you want this to be two players rather than playing a computer, just remove this line.

' Declare the winner
If intWhosWinner = 3 Then
        MessageBox.Show("It's a draw")
ElseIf intWhosWinner = 2 Then
        MessageBox.Show("You Lose")
ElseIf intWhosWinner = 1 Then
        MessageBox.Show("You Win")
End If

After CheckWinLose() is called, the program knows if we have a winner, tie, or whether to keep playing.

Have I Won Yet?

CheckWinLose() is the main reason I programmed this with a 3x3 array instead of just using the labels. Rather than having to do comparisons for every possible way to win, which is eight by the way, I was able to divide it out into three directions.

Dim intOCount As Integer = 0
Dim intXCount As Integer = 0
Dim intTotalCount As Integer = 0

First I'll introduce the counters. intOCount will let the game know if player 1 is the winner, intXCount player 2, and intTotalCount if there's a tie.

' Check horizontal for win or lose
For i As Integer = 0 To 2
        For j As Integer = 0 To 2
                If arrGrid(i, j) = 1 Then
                        intOCount += 1
                        intTotalCount += 1
                End If
 
                If arrGrid(i, j) = 2 Then
                        intXCount += 1
                        intTotalCount += 1
                End If
        Next
 
        If intOCount = 3 Then
                intWhosWinner = 1
        ElseIf intXCount = 3 Then
                intWhosWinner = 2
        End If
 
        intOCount = 0
        intXCount = 0
Next

Starting out by checking each row, number of spaces that player one and two have possession of are added up. If either has three, then the game is over. If not, then the counters are reset and the next row is checked. This is nearly the same for the columns. But before the columns are checked, if all squares are full then a tie is declared.

' Check for tie
If (intTotalCount = 9) And (intWhosWinner = 0) Then
        intWhosWinner = 3
Else
        intTotalCount = 0
End If

Since intTotalCount incremented any time there was an X or an O, if there are nine spaces filled then it might be a tie. The program will call it a tie unless a win overrides the assumption. After the check is performed, the program no longer needs to be incrementing intTotalCount.

' Check diagonal for win or lose
If arrGrid(0, 0) = 1 And arrGrid(1, 1) = 1 And arrGrid(2, 2) = 1 Then
        intWhosWinner = 1
ElseIf arrGrid(0, 0) = 2 And arrGrid(1, 1) = 2 And arrGrid(2, 2) = 2 Then
        intWhosWinner = 2
ElseIf arrGrid(0, 2) = 1 And arrGrid(1, 1) = 1 And arrGrid(2, 0) = 1 Then
        intWhosWinner = 1
ElseIf arrGrid(0, 2) = 2 And arrGrid(1, 1) = 2 And arrGrid(2, 0) = 2 Then
        intWhosWinner = 2
End If

After it checks for a tie, it'll need see if any of the players won vertically by using the same method as for horizontal winners. After that, the game checks to see if a diagonal winner exists.

At this point, if you removed the AIMove() lines, then you have a perfectly good version of two player tic-tac-toe. But if you want to play by yourself, then in the next section I'll explain how each one of the difficulty levels is programmed.

<< Previous

Next >>