Creating a Tic Tac Toe Game in Visual Basic

WEBINAR:On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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.

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.

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:

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:

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.

About the Author

Hannes DuPreez

Hannes du Preez has been a Microsoft MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978@gmail.com