How to Create a Simple Tic Tack Toe Game using VB.Net

How to Create a Simple Tic Tack Toe Game using VB.Net

This tutorial is all about on How Create a Tic Tack Toe Game using VB.Net
Tic Tack Toe Game using VB.Net is a game in which players draw Xs and Os inside a set of nine squares. This tutorial about Tic Tack Toe Game is a simple game that you can create using Visual Basic.Net.

What is Visual Basic’s purpose?

The third-generation programming language was created to aid developers in the creation of Windows applications. It has a programming environment that allows programmers to write code in.exe or executable files. They can also utilize it to create in-house front-end solutions for interacting with huge databases. Because the language allows for continuing changes, you can keep coding and revising your work as needed.

However, there are some limits to the Microsoft Visual Basic download. If you want to make applications that take a long time to process, this software isn’t for you. That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space. Furthermore, the language is limited to Microsoft and does not support other operating systems.

Introduction

It’s actually quite simple to make a Tic Tac Toe game. All you need is a little logic and Visual Basic to get started. Today, you will make such a game and, perhaps, you will agree that it is simple to make. Let’s have a look at it!

Tic Tac Toe is a game that involves playing a game of tic

Tic Tac Toe, also known as naught and crosses or Xs and Os, is a basic game made up of 9 (nine) squares or blocks. A player can draw either an X or an O in each block. To win, you must get all of your Xs or Os in a row (vertically, horizontally, or diagonally), while the opposing player’s goal is to prevent you from doing so. This Wikipedia page discusses how the game works, as well as its variations and origins.

So let’s get started.

  • First is open the Visual Basic, Select File on the menu, then click New and create a new project. Then a New Project Dialog will appear. You can rename your project, depending on what you like to name it. After that click OK
  • Then, design your form like this just like what I’ve shown you below.
    Add 12 Buttons, 2 Labels, and a Timer from the toolbox.
    Set the Property Enable of Timer to True and change its interval to 1.
    Set the Property Visible of the Labels to False.

  • After designing your form. Go to Code view and add this following declaration below the Public class Form1

[vbnet]

Dim random As String = 1

[/vbnet]

  • Then add this code for the Timer.[vbnet]Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    random += 1
    If random = 10 Then
    random = 1
    End IfEnd Sub

     

    [/vbnet]

  • Add this Following Sub so that the computer can make its own move.
    [vbnet]
    Public Sub PC()If random = 1 And Button1.Enabled = True Then
    Button1.Text = “O”
    Button1.Enabled = False
    End If
    If random = 2 And Button2.Enabled = True Then
    Button2.Text = “O”
    Button2.Enabled = False
    End If
    If random = 3 And Button3.Enabled = True Then
    Button3.Text = “O”
    Button3.Enabled = False
    End If
    If random = 4 And Button4.Enabled = True Then
    Button4.Text = “O”
    Button4.Enabled = False
    End If
    If random = 5 And Button5.Enabled = True Then
    Button5.Text = “O”
    Button5.Enabled = False
    End If
    If random = 6 And Button6.Enabled = True Then
    Button6.Text = “O”
    Button6.Enabled = False
    End If
    If random = 7 And Button7.Enabled = True Then
    Button7.Text = “O”
    Button7.Enabled = False
    End If
    If random = 8 And Button8.Enabled = True Then
    Button8.Text = “O”
    Button8.Enabled = False
    End If
    If random = 9 And Button9.Enabled = True Then
    Button9.Text = “O”
    Button9.Enabled = False
    End If
    End Sub
    [/vbnet]
  • Then after that, this following codes.
    [vbnet]Public Sub Options()If Label1.Text = 1 Then
    If Button2.Text = “X” And Button3.Enabled = True Then
    With Button3
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button3.Text = “X” And Button2.Enabled = True Then
    With Button2
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button5.Text = “X” And Button9.Enabled = True Then
    With Button9
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button9.Text = “X” And Button5.Enabled = True Then
    With Button5
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button4.Text = “X” And Button7.Enabled = True Then
    With Button7
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button7.Text = “X” And Button4.Enabled = True Then
    With Button4
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button8.Text = “X” Or Button6.Text = “X” Then
    hit()
    End If
    End If
    If Label1.Text = 2 Then
    If Button1.Text = “X” And Button3.Enabled = True Then
    With Button3
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button1.Text = “X” And Button1.Enabled = True Then
    With Button1
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button5.Text = “X” And Button8.Enabled = True Then
    With Button8
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button8.Text = “X” And Button5.Enabled = True Then
    With Button5
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button6.Text = “X” Or Button4.Text = “X” Or Button7.Text = “X” Or Button9.Text = “X” Then
    hit()
    End If

     

    End If
    If Label1.Text = 3 Then
    If Button1.Text = “X” And Button2.Enabled = True Then
    With Button2
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button2.Text = “X” And Button1.Enabled = True Then
    With Button1
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button6.Text = “X” And Button9.Enabled = True Then
    With Button9
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button9.Text = “X” And Button6.Enabled = True Then
    With Button6
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button5.Text = “X” And Button7.Enabled = True Then
    With Button7
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button7.Text = “X” And Button5.Enabled = True Then
    With Button5
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button8.Text = “X” Or Button4.Text = “X” Then
    hit()
    End If

    End If
    If Label1.Text = 4 Then
    If Button1.Text = “X” And Button7.Enabled = True Then
    With Button7
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button7.Text = “X” And Button1.Enabled = True Then
    With Button1
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button5.Text = “X” And Button6.Enabled = True Then
    With Button6
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button6.Text = “X” And Button5.Enabled = True Then
    With Button5
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button2.Text = “X” Or Button3.Text = “X” Or Button8.Text = “X” Or Button9.Text = “X” Then
    hit()
    End If

    End If
    If Label1.Text = 5 Then
    If Button1.Text = “X” And Button9.Enabled = True Then
    With Button9
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button9.Text = “X” And Button1.Enabled = True Then
    With Button1
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button2.Text = “X” And Button8.Enabled = True Then
    With Button8
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button8.Text = “X” And Button2.Enabled = True Then
    With Button2
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button3.Text = “X” And Button7.Enabled = True Then
    With Button7
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button7.Text = “X” And Button3.Enabled = True Then
    With Button3
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button6.Text = “X” And Button4.Enabled = True Then
    With Button4
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button4.Text = “X” And Button6.Enabled = True Then
    With Button6
    .Text = “O”
    .Enabled = False
    End With
    End If

    End If
    If Label1.Text = 6 Then
    If Button3.Text = “X” And Button9.Enabled = True Then
    With Button9
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button9.Text = “X” And Button3.Enabled = True Then
    With Button3
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button5.Text = “X” And Button4.Enabled = True Then
    With Button4
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button4.Text = “X” And Button5.Enabled = True Then
    With Button5
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button1.Text = “X” Or Button2.Text = “X” Or Button7.Text = “X” Or Button8.Text = “X” Then
    hit()
    End If

    End If

    If Label1.Text = 7 Then
    If Button1.Text = “X” And Button4.Enabled = True Then
    With Button4
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button4.Text = “X” And Button1.Enabled = True Then
    With Button1
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button5.Text = “X” And Button3.Enabled = True Then
    With Button3
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button3.Text = “X” And Button5.Enabled = True Then
    With Button5
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button8.Text = “X” And Button9.Enabled = True Then
    With Button9
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button9.Text = “X” And Button8.Enabled = True Then
    With Button8
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button6.Text = “X” Or Button2.Text = “X” Then
    hit()
    End If

    End If

    If Label1.Text = 8 Then
    If Button2.Text = “X” And Button5.Enabled = True Then
    With Button5
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button5.Text = “X” And Button2.Enabled = True Then
    With Button2
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button9.Text = “X” And Button7.Enabled = True Then
    With Button7
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button7.Text = “X” And Button9.Enabled = True Then
    With Button9
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button6.Text = “X” Or Button3.Text = “X” Or Button1.Text = “X” Or Button4.Text = “X” Then
    hit()
    End If

    End If
    If Label1.Text = 9 Then
    If Button6.Text = “X” And Button3.Enabled = True Then
    With Button3
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button3.Text = “X” And Button6.Enabled = True Then
    With Button6
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button5.Text = “X” And Button1.Enabled = True Then
    With Button1
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button1.Text = “X” And Button5.Enabled = True Then
    With Button5
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button8.Text = “X” And Button7.Enabled = True Then
    With Button7
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button7.Text = “X” And Button8.Enabled = True Then
    With Button8
    .Text = “O”
    .Enabled = False
    End With
    ElseIf Button2.Text = “X” Or Button4.Text = “X” Then
    hit()
    End If

    End If

    End Sub
    [/vbnet]

  • This following code will check if there is a winner and will show the result on a message box.
    [vbnet]Public Sub CheckIfComputerWins()
    If Button1.Text = “O” And Button2.Text = “O” And Button3.Text = “O” _
    Or Button4.Text = “O” And Button5.Text = “O” And Button6.Text = “O” _
    Or Button7.Text = “O” And Button8.Text = “O” And Button9.Text = “O” _
    Or Button1.Text = “O” And Button4.Text = “O” And Button7.Text = “O” _
    Or Button2.Text = “O” And Button5.Text = “O” And Button8.Text = “O” _
    Or Button3.Text = “O” And Button6.Text = “O” And Button9.Text = “O” _
    Or Button1.Text = “O” And Button5.Text = “O” And Button9.Text = “O” _
    Or Button7.Text = “O” And Button5.Text = “O” And Button3.Text = “O” Then
    MsgBox(“Computer Wins! You lose”)
    Button1.Text = “”
    Button1.Enabled = True
    Button2.Text = “”
    Button2.Enabled = True
    Button3.Text = “”
    Button3.Enabled = True
    Button4.Text = “”
    Button4.Enabled = True
    Button5.Text = “”
    Button5.Enabled = True
    Button6.Text = “”
    Button6.Enabled = True
    Button7.Text = “”
    Button7.Enabled = True
    Button8.Text = “”
    Button8.Enabled = True
    Button9.Text = “”
    Button9.Enabled = TrueElseIf Button1.Text = “X” And Button2.Text = “X” And Button3.Text = “X” _
    Or Button4.Text = “X” And Button5.Text = “X” And Button6.Text = “X” _
    Or Button7.Text = “X” And Button8.Text = “X” And Button9.Text = “X” _
    Or Button1.Text = “X” And Button4.Text = “X” And Button7.Text = “X” _
    Or Button2.Text = “X” And Button5.Text = “X” And Button8.Text = “X” _
    Or Button3.Text = “X” And Button6.Text = “X” And Button9.Text = “X” _
    Or Button1.Text = “X” And Button5.Text = “X” And Button9.Text = “X” _
    Or Button7.Text = “X” And Button5.Text = “X” And Button3.Text = “X” Then
    MsgBox(“Congratulations! You Win :)”)
    Button1.Text = “”
    Button1.Enabled = True
    Button2.Text = “”
    Button2.Enabled = True
    Button3.Text = “”
    Button3.Enabled = True
    Button4.Text = “”
    Button4.Enabled = True
    Button5.Text = “”
    Button5.Enabled = True
    Button6.Text = “”
    Button6.Enabled = True
    Button7.Text = “”
    Button7.Enabled = True
    Button8.Text = “”
    Button8.Enabled = True
    Button9.Text = “”
    Button9.Enabled = True
    End If
    End Sub

     

    [/vbnet]

  • Afterwards, we can now program the buttons of the game. Just add this following codes. it will handle the 9 buttons click event:[vbnet]Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, _
    Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button1.Click

     

    sender.Text = “X”
    sender.Enabled = False
    Timer1.Stop()
    Label1.Text = sender.name
    Label1.Text = Label1.Text.Replace(“Button”, “”)
    If random = Label1.Text Then
    random = random + 1
    End If
    If random > 0 Then
    PC()
    random = 0
    ElseIf random = 0 Then
    Options()
    End If
    Label2.Text = random
    CheckIfComputerWins()
    End Sub

    [/vbnet]

  • We also need to add a code when the user doesn’t make a proper move.
    Just add this codes.
    [vbnet]
    Public Sub hit()
    For Each ctl As Control In Me.Controls
    If Label1.Text < 9 Then
    If (ctl.Name.StartsWith(“Button” & Label1.Text + 1)) Then
    Dim btn As Button = DirectCast(ctl, Button)
    If btn.Enabled = True Then
    btn.Text = “O”
    btn.Enabled = False
    ElseLabel1.Text = Label1.Text + 1
    End IfEnd If
    Else
    If (ctl.Name.StartsWith(“Button1”)) Then
    Dim btn As Button = DirectCast(ctl, Button)
    If btn.Enabled = True Then
    btn.Text = “O”
    btn.Enabled = False
    Else

     

    Label1.Text = Label1.Text + 1
    End If

    End If
    End If

    Next

    End Sub
    [/vbnet]

  • Now, we program the PC START Button. Add this codes.
    [vbnet]
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    Timer1.Stop()
    Button1.Text = “O”
    Label1.Text = 1Button1.Enabled = False
    Button10.Enabled = FalseEnd Sub
    [/vbnet]
  • Finally, add this following codes to the Restart Button.
    [vbnet]
    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    Button1.Text = “”
    Button1.Enabled = True
    Button2.Text = “”
    Button2.Enabled = True
    Button3.Text = “”
    Button3.Enabled = True
    Button4.Text = “”
    Button4.Enabled = True
    Button5.Text = “”
    Button5.Enabled = True
    Button6.Text = “”
    Button6.Enabled = True
    Button7.Text = “”
    Button7.Enabled = True
    Button8.Text = “”
    Button8.Enabled = True
    Button9.Text = “”
    Button9.Enabled = True
    Button10.Enabled = True
    Timer1.Start()End Sub
    [/vbnet]
  • Click F5 to run the program and to play the game.

Download How to Create a Tic Tack Toe Game using VB.Net Source code Here

If you have any questions or suggestions about How to Create a Tic Tack Toe Game using VB.Net please contact me through our contact page.

Leave a Comment