Creating a Tic Tac Toe Game in Visual Basic

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

Creating a Tic Tac Toe game is actually pretty straightforward. All you need is a bit of logic and Visual Basic. Today, you will create such a game and hopefully agree on how easy it is to create. Let’s have a look!

Tic Tac Toe

Tic Tac Toe, Naught and crosses, or Xs and Os is a very simple game comprised of 9 (nine) squares or blocks. In each block, an X or an O can be drawn by a player. The object of the game is to get your Xs or Os in a row (vertically, horizontally, or diagonally) to win and the other player’s goal is to prevent you from doing that. This Wikipedia article explains how the game works, its variances, and even its origin.

Our Project

Design

Create a new Visual Basic Windows Forms application. You may name it anything you like. Design your form to resemble Figure 1. Please also keep in mind that my object names may differ from yours.

Tic1
Figure 1: Our Design

The design basically consists of 10 buttons. Nine are for playing the actual game. To get the desired effect, you need to set your Buttons’ FlatStyle property to Flat.

Code

Add the following Modular variables:

   Private intRand As Integer = 1
   Private intButtonID As Integer = 0

intRand will be used as a random number that will be generated automatically. intButtonID represents the particular button that has been pressed.

Add the following code:

      Private Sub btnStart_Click(sender As Object, e As EventArgs) _
         Handles btnStart.Click

         Reset()

         btnPlay1.Text = "O"
         intButtonID = 1
         Timer1.Start()

      End Sub

In the Start button’s event, a Sub named Reset is called (I will demonstrate this sub a bit later), the Button ID variable is set to 1, and its text is set to 0. Finally, the Timer gets started.

Let’s add the Reset sub now:

      Private Sub Reset()   'Reset Defaults'

         For Each ctl In Controls

            If TypeOf ctl Is Button Then

               ctl.Text = ""

               ctl.Enabled = True

            End If

            Next ctl

            btnStart.Text = "Start / Restart"
      End Sub

The Reset sub clears all the text from all the buttons on the Form using a loop that loops through each object on the form, so that the game can start over again. Let’s add the Timer’s Tick event:

      Private Sub Timer1_Tick(sender As Object, e As EventArgs) _
         Handles Timer1.Tick

         intRand += 1

         If intRand = 10 Then

            intRand = 1

         End If

      End Sub

The Timer_Tick event is responsible for giving the intRand variable an almost random value. Add the next event:

      Private Sub Button1_Click(ByVal sender As System.Object,_
            ByVal e As System.EventArgs) Handles btnPlay1.Click, _
            btnPlay2.Click, btnPlay3.Click, btnPlay4.Click, _
            btnPlay5.Click, btnPlay6.Click, btnPlay7.Click, _
            btnPlay8.Click, btnPlay9.Click

         sender.Text = "X"
         sender.Enabled = False

         Timer1.Stop()

         intButtonID = Int32.Parse(sender.tag)

         If intRand = intButtonID Then

            intRand = intRand + 1

         End If

         If intRand > 0 Then

            ComputerPlayer()

            intRand = 0

         ElseIf intRand = 0 Then

            Choices()

         End If


         WhoWins()

      End Sub

This event fires whenever any button in play gets clicked. It sets the current button’s Text to ‘X’ and determines whose turn it is. It also calls sub procedures named ComputerPlayer, Choices, and WhoWins. Let’s add the ComputerPlayer sub now:

      Public Sub ComputerPlayer()   'Computer Player'

         Select Case intRand

            Case 1

               btnPlay1.Text = "O"
               btnPlay1.Enabled = False

            Case 2

               btnPlay1.Text = "O"
               btnPlay2.Enabled = False

            Case 3

               btnPlay1.Text = "O"
               btnPlay3.Enabled = False

            Case 4

               btnPlay1.Text = "O"
               btnPlay4.Enabled = False

            Case 5

               btnPlay1.Text = "O"
               btnPlay5.Enabled = False

            Case 6

               btnPlay1.Text = "O"
               btnPlay6.Enabled = False

            Case 7

               btnPlay1.Text = "O"
               btnPlay7.Enabled = False

            Case 8

               btnPlay1.Text = "O"
               btnPlay8.Enabled = False

            Case 9

               btnPlay1.Text = "O"
               btnPlay9.Enabled = False

         End Select

      End Sub

Add the Choices sub:

      'checks which buttons were pressed,'
      'then set all properties accordingly'
      Public Sub Choices()

         Select Case intButtonID
            Case 1

               If btnPlay2.Text = "X" And _
                  btnPlay3.Enabled = True Then

                  btnPlay3.Text = "O"
                  btnPlay3.Enabled = False

               ElseIf btnPlay3.Text = "X" And _
                  btnPlay2.Enabled = True Then

                  btnPlay2.Text = "O"
                  btnPlay2.Enabled = False

               ElseIf btnPlay5.Text = "X" And _
                  btnPlay9.Enabled = True Then

                  btnPlay9.Text = "O"
                  btnPlay9.Enabled = False

               ElseIf btnPlay9.Text = "X" And _
                  btnPlay5.Enabled = True Then

                  btnPlay5.Text = "O"
                  btnPlay5.Enabled = False

               ElseIf btnPlay4.Text = "X" And _
                  btnPlay7.Enabled = True Then

                  btnPlay7.Text = "O"
                  btnPlay7.Enabled = False

               ElseIf btnPlay7.Text = "X" And _
                  btnPlay4.Enabled = True Then

                  btnPlay4.Text = "O"
                  btnPlay4.Enabled = False

               ElseIf btnPlay8.Text = "X" Or _
                  btnPlay6.Text = "X" Then

                  Play()

               End If

            Case 2

               If btnPlay1.Text = "X" And _
                  btnPlay3.Enabled = True Then

                  btnPlay3.Text = "O"
                  btnPlay3.Enabled = False

               ElseIf btnPlay1.Text = "X" And _
                  btnPlay1.Enabled = True Then

                  btnPlay1.Text = "O"
                  btnPlay1.Enabled = False

               ElseIf btnPlay5.Text = "X" And _
                  btnPlay8.Enabled = True Then

                  btnPlay8.Text = "O"
                  btnPlay8.Enabled = False

               ElseIf btnPlay8.Text = "X" And _
                  btnPlay5.Enabled = True Then

                  btnPlay5.Text = "O"
                  btnPlay5.Enabled = False

               ElseIf btnPlay6.Text = "X" Or _
                  btnPlay4.Text = "X" Or _
                  btnPlay7.Text = "X" Or _
                  btnPlay9.Text = "X" Then

                  Play()

               End If

            Case 3

               If btnPlay1.Text = "X" And _
                  btnPlay2.Enabled = True Then

                  btnPlay2.Text = "O"
                  btnPlay2.Enabled = False

               ElseIf btnPlay2.Text = "X" And _
                  btnPlay1.Enabled = True Then

                  btnPlay1.Text = "O"
                  btnPlay1.Enabled = False

               ElseIf btnPlay6.Text = "X" And _
                  btnPlay9.Enabled = True Then

                  btnPlay9.Text = "O"
                  btnPlay9.Enabled = False

               ElseIf btnPlay9.Text = "X" And _
                  btnPlay6.Enabled = True Then

                  btnPlay6.Text = "O"
                  btnPlay6.Enabled = False

               ElseIf btnPlay5.Text = "X" And _
                  btnPlay7.Enabled = True Then

                  btnPlay7.Text = "O"
                  btnPlay7.Enabled = False

               ElseIf btnPlay7.Text = "X" And _
                  btnPlay5.Enabled = True Then

                  btnPlay5.Text = "O"
                  btnPlay5.Enabled = False

               ElseIf btnPlay8.Text = "X" Or _
                  btnPlay4.Text = "X" Then

                  Play()

               End If

            Case 4

               If btnPlay1.Text = "X" And _
                  btnPlay7.Enabled = True Then

                  btnPlay7.Text = "O"
                  btnPlay7.Enabled = False

               ElseIf btnPlay7.Text = "X" And _
                  btnPlay1.Enabled = True Then

                  btnPlay1.Text = "O"
                  btnPlay1.Enabled = False

               ElseIf btnPlay5.Text = "X" And _
                  btnPlay6.Enabled = True Then

                  btnPlay6.Text = "O"
                  btnPlay6.Enabled = False

               ElseIf btnPlay6.Text = "X" And _
                  btnPlay5.Enabled = True Then

                  btnPlay5.Text = "O"
                  btnPlay5.Enabled = False

               ElseIf btnPlay2.Text = "X" Or _
                  btnPlay3.Text = "X" Or _
                  btnPlay8.Text = "X" Or _
                  btnPlay9.Text = "X" Then

                  Play()

               End If

            Case 5

               If btnPlay1.Text = "X" And _
                  btnPlay9.Enabled = True Then

                  btnPlay9.Text = "O"
                  btnPlay9.Enabled = False

               ElseIf btnPlay9.Text = "X" And _
                  btnPlay1.Enabled = True Then

                  btnPlay1.Text = "O"
                  btnPlay1.Enabled = False

               ElseIf btnPlay2.Text = "X" And _
                  btnPlay8.Enabled = True Then

                  btnPlay8.Text = "O"
                  btnPlay8.Enabled = False

               ElseIf btnPlay8.Text = "X" And _
                  btnPlay2.Enabled = True Then

                  btnPlay2.Text = "O"
                  btnPlay2.Enabled = False

               ElseIf btnPlay3.Text = "X" And _
                  btnPlay7.Enabled = True Then

                  btnPlay7.Text = "O"
                  btnPlay7.Enabled = False

               ElseIf btnPlay7.Text = "X" And _
                  btnPlay3.Enabled = True Then

                  btnPlay3.Text = "O"
                  btnPlay3.Enabled = False

               ElseIf btnPlay6.Text = "X" And _
                  btnPlay4.Enabled = True Then

                  btnPlay4.Text = "O"
                  btnPlay4.Enabled = False

               ElseIf btnPlay4.Text = "X" And _
                  btnPlay6.Enabled = True Then

                  btnPlay6.Text = "O"
                  btnPlay6.Enabled = False

               End If

            Case 6

               If btnPlay3.Text = "X" And _
                  btnPlay9.Enabled = True Then

                  btnPlay9.Text = "O"
                  btnPlay9.Enabled = False

               ElseIf btnPlay9.Text = "X" And _
                  btnPlay3.Enabled = True Then

                  btnPlay3.Text = "O"
                  btnPlay3.Enabled = False

               ElseIf btnPlay5.Text = "X" And _
                  btnPlay4.Enabled = True Then

                  btnPlay4.Text = "O"
                  btnPlay4.Enabled = False

               ElseIf btnPlay4.Text = "X" And _
                  btnPlay5.Enabled = True Then

                  btnPlay5.Text = "O"
                  btnPlay5.Enabled = False

               ElseIf btnPlay1.Text = "X" Or _
                  btnPlay2.Text = "X" Or _
                  btnPlay7.Text = "X" Or _
                  btnPlay8.Text = "X" Then

                  Play()

               End If

            Case 7

               If btnPlay1.Text = "X" And _
                  btnPlay4.Enabled = True Then

                  btnPlay4.Text = "O"
                  btnPlay4.Enabled = False

               ElseIf btnPlay4.Text = "X" And _
                  btnPlay1.Enabled = True Then

                  btnPlay1.Text = "O"
                  btnPlay1.Enabled = False

               ElseIf btnPlay5.Text = "X" And _
                  btnPlay3.Enabled = True Then

                  btnPlay3.Text = "O"
                  btnPlay3.Enabled = False

               ElseIf btnPlay3.Text = "X" And _
                  btnPlay5.Enabled = True Then

                  btnPlay5.Text = "O"
                  btnPlay5.Enabled = False

               ElseIf btnPlay8.Text = "X" And _
                  btnPlay9.Enabled = True Then

                  btnPlay9.Text = "O"
                  btnPlay9.Enabled = False

               ElseIf btnPlay9.Text = "X" And _
                  btnPlay8.Enabled = True Then

                  btnPlay8.Text = "O"
                  btnPlay8.Enabled = False

               ElseIf btnPlay6.Text = "X" Or _
                  btnPlay2.Text = "X" Then

                  Play()

               End If

            Case 8

               If btnPlay2.Text = "X" And _
                  btnPlay5.Enabled = True Then

                  btnPlay5.Text = "O"
                  btnPlay5.Enabled = False

               ElseIf btnPlay5.Text = "X" And _
                  btnPlay2.Enabled = True Then

                  btnPlay2.Text = "O"
                  btnPlay2.Enabled = False

               ElseIf btnPlay9.Text = "X" And _
                  btnPlay7.Enabled = True Then

                  btnPlay7.Text = "O"
                  btnPlay7.Enabled = False

               ElseIf btnPlay7.Text = "X" And _
                  btnPlay9.Enabled = True Then

                  btnPlay9.Text = "O"
                  btnPlay9.Enabled = False

               ElseIf btnPlay6.Text = "X" Or _
                  btnPlay3.Text = "X" Or _
                  btnPlay1.Text = "X" Or _
                  btnPlay4.Text = "X" Then

                  Play()

               End If

            Case 9

               If btnPlay6.Text = "X" And _
                  btnPlay3.Enabled = True Then

                  btnPlay3.Text = "O"
                  btnPlay3.Enabled = False

               ElseIf btnPlay3.Text = "X" And _
                  btnPlay6.Enabled = True Then

                  btnPlay6.Text = "O"
                  btnPlay6.Enabled = False

               ElseIf btnPlay5.Text = "X" And _
                  btnPlay1.Enabled = True Then

                  btnPlay1.Text = "O"
                  btnPlay1.Enabled = False

               ElseIf btnPlay1.Text = "X" And _
                  btnPlay5.Enabled = True Then

                  btnPlay5.Text = "O"
                  btnPlay5.Enabled = False

               ElseIf btnPlay8.Text = "X" And _
                  btnPlay7.Enabled = True Then

                  btnPlay7.Text = "O"
                  btnPlay7.Enabled = False
               ElseIf btnPlay7.Text = "X" And _
                  btnPlay8.Enabled = True Then

                  btnPlay8.Text = "O"
                  btnPlay8.Enabled = False

               ElseIf btnPlay2.Text = "X" Or _
                  btnPlay4.Text = "X" Then

                  Play()

               End If

         End Select

      End Sub

Oy! That was a long one! The preceding sub was so long because it had to check all possible combinations and then decide which move to make. Inside this sub, I have called a sub named Play. Let’s add this now:

      Public Sub Play()   'Play'

         For Each ctl As Control In Me.Controls

            If intButtonID < 9 Then

               If (ctl.Name.StartsWith("Button" _
                  & intButtonID + 1)) Then

                  Dim btn As Button = DirectCast(ctl, Button)

               If btn.Enabled = True Then

                  btn.Text = "O"
                  btn.Enabled = False

               Else

                  intButtonID = intButtonID + 1

               End If

            End If

            Else

               If (ctl.Name.StartsWith("btnPlay1")) Then

                  Dim btn As Button = DirectCast(ctl, Button)

                  If btn.Enabled = True Then

                     btn.Text = "O"
                     btn.Enabled = False

                  Else

                     intButtonID = intButtonID + 1

                  End If

               End If

            End If

         Next

      End Sub

The Play sub is responsible for the actual game play, which is nothing more than setting the button’s enabled property to False. This seems trivial because all the hard work has already been done in the Choices and ComputerPlayer subs. All that is left now is to determine who wins the game:

      'Checks who wins between player and computer'
      Public Sub WhoWins()

         If (btnPlay1.Text = "O" And btnPlay2.Text = "O" _
            And btnPlay3.Text = "O") _
            Or (btnPlay4.Text = "O" And btnPlay5.Text = "O" _
            And btnPlay6.Text = "O") _
            Or (btnPlay7.Text = "O" And btnPlay8.Text = "O" _
            And btnPlay9.Text = "O") _
            Or (btnPlay1.Text = "O" And btnPlay4.Text = "O" _
            And btnPlay7.Text = "O") _
            Or (btnPlay2.Text = "O" And btnPlay5.Text = "O" _
            And btnPlay8.Text = "O") _
            Or (btnPlay3.Text = "O" And btnPlay6.Text = "O" _
            And btnPlay9.Text = "O") _
            Or (btnPlay1.Text = "O" And btnPlay5.Text = "O" _
            And btnPlay9.Text = "O") _
            Or (btnPlay7.Text = "O" And btnPlay5.Text = "O" _
            And btnPlay3.Text = "O") Then

            MessageBox.Show("You Lose")

            Reset()

         ElseIf (btnPlay1.Text = "X" And btnPlay2.Text = "X" _
            And btnPlay3.Text = "X") _
            Or (btnPlay4.Text = "X" And btnPlay5.Text = "X" _
            And btnPlay6.Text = "X") _
            Or (btnPlay7.Text = "X" And btnPlay8.Text = "X" _
            And btnPlay9.Text = "X") _
            Or (btnPlay1.Text = "X" And btnPlay4.Text = "X" _
            And btnPlay7.Text = "X") _
            Or (btnPlay2.Text = "X" And btnPlay5.Text = "X" _
            And btnPlay8.Text = "X") _
            Or (btnPlay3.Text = "X" And btnPlay6.Text = "X" _
            And btnPlay9.Text = "X") _
            Or (btnPlay1.Text = "X" And btnPlay5.Text = "X" _
            And btnPlay9.Text = "X") _
            Or (btnPlay7.Text = "X" And btnPlay5.Text = "X" _
            And btnPlay3.Text = "X") Then

            MessageBox.Show("You Win")

            Reset()

         End If

      End Sub

The WhoWins Sub simply checks who won the game.

Tic2
Figure 2: Our game in action

Conclusion

This was another easy game without any advanced graphics and methods (although in an update, adding those would be nice). You do not have to be a rocket scientist to create decent and fun games. All you need is a bit of logic.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read