WPF Tic Tac Toe Game in VB.NET

Tic Tac Toe is a classical game which is also known as "wick wack woe" or "Noughts and Crosses". Here, you can learn how to implement Tic Tac Toe in WPF using Visual Basic.
  • 3777
 

Tic Tac Toe is a classical well known interesting game which can be played between two persons at once.

How this application works ?

tictactoe.png 

  • 9 buttons are available in the game area, as shown in above image. An image control is also available behind each button. As a player click on a button, it is hidden and an image appears (displays either cross (for player 1) or zero (for player 2)). A 2d-array of integer type is used to keep the information about each button pressed, i.e. each move either by player 1 or player 2.
    A function checkresult() is called to calculate the result each time you press a button (if number of moves are greater then 4).

     
  • Two buttons (1. Reset 2. Exit) and one label controls are also present in application interface. 

    Reset - Resets the properties of controls, reassign values to variables, etc. to play the game again and to avoid the close and reopen of application to play.

    Exit - To quit the game.

    Label - It is used to display the result of the game.

 tictactoe1.gif

As you can see in above image, 8 rectangles are used to display all possible wins. You can see only one rectangle as a player wins and these rectangles are hidden at the time of application startup.

Code:

Class MainWindow
    Dim var(3, 3) As Integer
    Dim counter As Integer = 0
    Dim bmpcross As New BitmapImage(New Uri("pack://application:,,,/TicTacToe;component/Images/cross.png"))
    Dim bmpzero As New BitmapImage(New Uri("pack://application:,,,/TicTacToe;component/Images/zero.png"))

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Dim i As Integer
        Dim j As Integer
        For i = 0 To 2 Step 1
            For j = 0 To 2 Step 1
                var(i, j) = 0
            Next
        Next
        HideRectangles()
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image1.Source = bmpzero
            var(0, 0) = 2
        Else
            Image1.Source = bmpcross
            var(0, 0) = 1
        End If
        Button1.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image2.Source = bmpzero
            var(0, 1) = 2
        Else
            Image2.Source = bmpcross
            var(0, 1) = 1
        End If
        Button2.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button3.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image3.Source = bmpzero
            var(0, 2) = 2
        Else
            Image3.Source = bmpcross
            var(0, 2) = 1
        End If
        Button3.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button4.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image4.Source = bmpzero
            var(1, 0) = 2
        Else
            Image4.Source = bmpcross
            var(1, 0) = 1
        End If
        Button4.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button5.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image5.Source = bmpzero
            var(1, 1) = 2
        Else
            Image5.Source = bmpcross
            var(1, 1) = 1
        End If
        Button5.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button6.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image6.Source = bmpzero
            var(1, 2) = 2
        Else
            Image6.Source = bmpcross
            var(1, 2) = 1
        End If
        Button6.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button7.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image7.Source = bmpzero
            var(2, 0) = 2
        Else
            Image7.Source = bmpcross
            var(2, 0) = 1
        End If
        Button7.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button8.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image8.Source = bmpzero
            var(2, 1) = 2
        Else
            Image8.Source = bmpcross
            var(2, 1) = 1
        End If
        Button8.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button9.Click
        counter += 1
        If counter Mod 2 = 0 Then
            Image9.Source = bmpzero
            var(2, 2) = 2
        Else
            Image9.Source = bmpcross
            var(2, 2) = 1
        End If
        Button9.Visibility = Windows.Visibility.Hidden
        checkresult()
    End Sub
    Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Reset.Click
        counter = 0
        Label1.Content = "Winner"
        Dim i As Integer
        Dim j As Integer
        For i = 0 To 2 Step 1
            For j = 0 To 2 Step 1
                var(i, j) = 0
            Next
        Next
        VisibleButtons()
        EnableButtons()
        HideRectangles()
        Image1.Source = Nothing
        Image2.Source = Nothing
        Image3.Source = Nothing
        Image4.Source = Nothing
        Image5.Source = Nothing
        Image6.Source = Nothing
        Image7.Source = Nothing
        Image8.Source = Nothing
        Image9.Source = Nothing
    End Sub

    Sub VisibleButtons()
        Button1.Visibility = Windows.Visibility.Visible
        Button2.Visibility = Windows.Visibility.Visible
        Button3.Visibility = Windows.Visibility.Visible
        Button4.Visibility = Windows.Visibility.Visible
        Button5.Visibility = Windows.Visibility.Visible
        Button6.Visibility = Windows.Visibility.Visible
        Button7.Visibility = Windows.Visibility.Visible
        Button8.Visibility = Windows.Visibility.Visible
        Button9.Visibility = Windows.Visibility.Visible
    End Sub

    Sub EnableButtons()
        Button1.IsEnabled() = True
        Button2.IsEnabled() = True
        Button3.IsEnabled() = True
        Button4.IsEnabled() = True
        Button5.IsEnabled() = True
        Button6.IsEnabled() = True
        Button7.IsEnabled() = True
        Button8.IsEnabled() = True
        Button9.IsEnabled() = True
    End Sub

    Sub HideRectangles()
        Rectangle1.Visibility = Windows.Visibility.Hidden
        Rectangle2.Visibility = Windows.Visibility.Hidden
        Rectangle3.Visibility = Windows.Visibility.Hidden
        Rectangle4.Visibility = Windows.Visibility.Hidden
        Rectangle5.Visibility = Windows.Visibility.Hidden
        Rectangle6.Visibility = Windows.Visibility.Hidden
        Rectangle7.Visibility = Windows.Visibility.Hidden
        Rectangle8.Visibility = Windows.Visibility.Hidden
    End Sub

    Sub checkresult()
        Dim i As Integer
        Dim flag As Integer = 0
        If counter > 4 Then
            For i = 1 To 2 Step 1
                If var(0, 0) = i And var(0, 1) = i And var(0, 2) = i Then
                    ShowRectangle(1)
                    flag += 1
                    Exit For
                ElseIf var(1, 0) = i And var(1, 1) = i And var(1, 2) = i Then
                    ShowRectangle(2)
                    flag += 1
                    Exit For
                ElseIf var(2, 0) = i And var(2, 1) = i And var(2, 2) = i Then
                    ShowRectangle(3)
                    flag += 1
                    Exit For
                ElseIf var(0, 0) = i And var(1, 0) = i And var(2, 0) = i Then
                    ShowRectangle(4)
                    flag += 1
                    Exit For
                ElseIf var(0, 1) = i And var(1, 1) = i And var(2, 1) = i Then
                    ShowRectangle(5)
                    flag += 1
                    Exit For
                ElseIf var(0, 2) = i And var(1, 2) = i And var(2, 2) = i Then
                    ShowRectangle(6)
                    flag += 1
                    Exit For
                ElseIf var(0, 0) = i And var(1, 1) = i And var(2, 2) = i Then
                    ShowRectangle(8)
                    flag += 1
                    Exit For
                ElseIf var(0, 2) = i And var(1, 1) = i And var(2, 0) = i Then
                    ShowRectangle(7)
                    flag += 1
                    Exit For
                End If
            Next
            If flag > 0 Then
                If i = 1 Then
                    Player1Won()
                ElseIf i = 2 Then
                    Player2Won()
                End If
            End If
            If flag = 0 And counter = 9 Then
                Label1.Content = "No Result ...."
            End If
        End If
    End Sub
    Sub ShowRectangle(ByVal i As Integer)
        Select Case (i)
            Case 1
                Rectangle1.Visibility = Windows.Visibility.Visible
                Exit Sub
            Case 2
                Rectangle2.Visibility = Windows.Visibility.Visible
                Exit Sub
            Case 3
                Rectangle3.Visibility = Windows.Visibility.Visible
                Exit Sub
            Case 4
                Rectangle4.Visibility = Windows.Visibility.Visible
                Exit Sub
            Case 5
                Rectangle5.Visibility = Windows.Visibility.Visible
                Exit Sub
            Case 6
                Rectangle6.Visibility = Windows.Visibility.Visible
                Exit Sub
            Case 7
                Rectangle7.Visibility = Windows.Visibility.Visible
                Exit Sub
            Case 8
                Rectangle8.Visibility = Windows.Visibility.Visible
                Exit Sub
        End Select
    End Sub

    Sub Player1Won()
        Label1.Content = "Player 1 won ..."
        DisableButtons()
    End Sub

    Sub Player2Won()
        Label1.Content = "Player 2 won ..."
        DisableButtons()
    End Sub

    Sub DisableButtons()
        Button1.IsEnabled() = False
        Button2.IsEnabled() = False
        Button3.IsEnabled() = False
        Button4.IsEnabled() = False
        Button5.IsEnabled() = False
        Button6.IsEnabled() = False
        Button7.IsEnabled() = False
        Button8.IsEnabled() = False
        Button9.IsEnabled() = False
    End Sub

    Private Sub Exit_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles [Exit].Click
        End
    End Sub

End Class

Output:

 tictactoe2.gif

---------------------------------------------------------------------------------------------
Nothing rubbish here, each article is valuable for needy

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.