Public Class main

    Dim i As Byte
    Dim saveload As New saveload
    Friend check(8) As Boolean
    Friend box(8) As Byte
    Friend player As Byte = 1
    Friend win As Byte = 0


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        clear()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        saveload.save()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        saveload.loadSaves()
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
    PictureBox1.Click, _
    PictureBox2.Click, _
    PictureBox3.Click, _
    PictureBox4.Click, _
    PictureBox5.Click, _
    PictureBox6.Click, _
    PictureBox7.Click, _
    PictureBox8.Click, _
    PictureBox9.Click

        If win <> 1 Then
            If sender Is PictureBox1 Then
                If check(0) = False Then
                    box(0) = player
                    fillBox(sender)
                    check(0) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            ElseIf sender Is PictureBox2 Then
                If check(1) = False Then
                    box(1) = player
                    fillBox(sender)
                    check(1) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            ElseIf sender Is PictureBox3 Then
                If check(2) = False Then
                    box(2) = player
                    fillBox(sender)
                    check(2) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            ElseIf sender Is PictureBox4 Then
                If check(3) = False Then
                    box(3) = player
                    fillBox(sender)
                    check(3) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            ElseIf sender Is PictureBox5 Then
                If check(4) = False Then
                    box(4) = player
                    fillBox(sender)
                    check(4) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            ElseIf sender Is PictureBox6 Then
                If check(5) = False Then
                    box(5) = player
                    fillBox(sender)
                    check(5) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            ElseIf sender Is PictureBox7 Then
                If check(6) = False Then
                    box(6) = player
                    fillBox(sender)
                    check(6) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            ElseIf sender Is PictureBox8 Then
                If check(7) = False Then
                    box(7) = player
                    fillBox(sender)
                    check(7) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            ElseIf sender Is PictureBox9 Then
                If check(8) = False Then
                    box(8) = player
                    fillBox(sender)
                    check(8) = True
                Else
                    MsgBox("This box has already been filled")
                End If

            End If
        Else
            MsgBox("There is already a winner")
        End If

    End Sub

    Private Sub fillBox(ByVal sender As PictureBox)
        If player = 1 Then
            sender.ImageLocation = "knot.jpg"
            sender.Load()
            checkForWin()
            player += 1
        ElseIf player = 2 Then
            sender.ImageLocation = "cross.jpg"
            sender.Load()
            checkForWin()
            player -= 1
        End If
    End Sub
    Public Sub checkForWin()
        ' check for wins horizontally
        If box(0) = box(1) And box(0) = box(2) And box(0) <> 0 Then
            sayWinner()
        ElseIf box(3) = box(4) And box(3) = box(5) And box(3) <> 0 Then
            sayWinner()
        ElseIf box(6) = box(7) And box(6) = box(8) And box(6) <> 0 Then
            sayWinner()

            ' check for wins vertically
        ElseIf box(0) = box(3) And box(0) = box(6) And box(0) <> 0 Then
            sayWinner()
        ElseIf box(1) = box(4) And box(1) = box(7) And box(1) <> 0 Then
            sayWinner()
        ElseIf box(2) = box(5) And box(2) = box(8) And box(2) <> 0 Then
            sayWinner()

            ' check for wins diagonally
        ElseIf box(0) = box(4) And box(0) = box(8) And box(0) <> 0 Then
            sayWinner()
        ElseIf box(6) = box(4) And box(6) = box(2) And box(6) <> 0 Then
            sayWinner()

        End If

    End Sub
    Private Sub sayWinner()
        Dim type As String
        type = 0
        If player = 1 Then
            type = "knots"
        ElseIf player = 2 Then
            type = "crosses"
        End If
        MsgBox("Player " & player & " (" & type & ") is the winner!")
        win = 1
    End Sub


    Public Sub clear()

        Dim i As Byte = 0
        While i < 9
            check(i) = False
            box(i) = 0
            i += 1
        End While

        win = 0
        player = 1

        PictureBox1.ImageLocation = "clear.jpg"
        PictureBox1.Load()

        PictureBox2.ImageLocation = "clear.jpg"
        PictureBox2.Load()

        PictureBox3.ImageLocation = "clear.jpg"
        PictureBox3.Load()

        PictureBox4.ImageLocation = "clear.jpg"
        PictureBox4.Load()

        PictureBox5.ImageLocation = "clear.jpg"
        PictureBox5.Load()

        PictureBox6.ImageLocation = "clear.jpg"
        PictureBox6.Load()

        PictureBox7.ImageLocation = "clear.jpg"
        PictureBox7.Load()

        PictureBox8.ImageLocation = "clear.jpg"
        PictureBox8.Load()

        PictureBox9.ImageLocation = "clear.jpg"
        PictureBox9.Load()

    End Sub

  
End Class