Tic-Tac-Toe

So I made a Tic-Tac-Toe game in VB.NET (Probably one of my very least favorite languages, but I’m in “educational mentorship” at the moment (the class where I teach VB to other students that are actually enrolled in the VB class at my school) and so I figured I’d do this as an example for them.

It’s not perfect; made it in about an hour so let me know if you find any errors.

You can download it here.

Also, feel free to critique my code:

[code]Public Class Form1

Dim blocks(8) As Integer
Dim difficulty As Integer = 3

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    setup()
    btnImpossible.BackColor = Color.BlueViolet
    btnHard.BackColor = Color.Green
    btnMedium.BackColor = Color.Green
    btnEasy.BackColor = Color.Green
    difficulty = 3
End Sub

Public Sub setup()
    For x = 0 To 8
        blocks(x) = 0
    Next
End Sub

Public Sub display()
    'Top Left
    If blocks(0) = 0 Then
        tl.Text = ""
    ElseIf blocks(0) = 1 Then
        tl.Text = "X"
    ElseIf blocks(0) = 2 Then
        tl.Text = "O"
    End If

    'Top Center
    If blocks(1) = 0 Then
        tc.Text = ""
    ElseIf blocks(1) = 1 Then
        tc.Text = "X"
    ElseIf blocks(1) = 2 Then
        tc.Text = "O"
    End If

    'Top Right
    If blocks(2) = 0 Then
        tr.Text = ""
    ElseIf blocks(2) = 1 Then
        tr.Text = "X"
    ElseIf blocks(2) = 2 Then
        tr.Text = "O"
    End If


    'Middle Left
    If blocks(3) = 0 Then
        ml.Text = ""
    ElseIf blocks(3) = 1 Then
        ml.Text = "X"
    ElseIf blocks(3) = 2 Then
        ml.Text = "O"
    End If

    'Middle Center
    If blocks(4) = 0 Then
        mc.Text = ""
    ElseIf blocks(4) = 1 Then
        mc.Text = "X"
    ElseIf blocks(4) = 2 Then
        mc.Text = "O"
    End If

    'Middle Right
    If blocks(5) = 0 Then
        mr.Text = ""
    ElseIf blocks(5) = 1 Then
        mr.Text = "X"
    ElseIf blocks(5) = 2 Then
        mr.Text = "O"
    End If



    'Bottom Left
    If blocks(6) = 0 Then
        bl.Text = ""
    ElseIf blocks(6) = 1 Then
        bl.Text = "X"
    ElseIf blocks(6) = 2 Then
        bl.Text = "O"
    End If

    'Bottom Center
    If blocks(7) = 0 Then
        bc.Text = ""
    ElseIf blocks(7) = 1 Then
        bc.Text = "X"
    ElseIf blocks(7) = 2 Then
        bc.Text = "O"
    End If

    'Bottom Right
    If blocks(8) = 0 Then
        br.Text = ""
    ElseIf blocks(8) = 1 Then
        br.Text = "X"
    ElseIf blocks(8) = 2 Then
        br.Text = "O"
    End If
End Sub

Public Sub clear()
    For x = 0 To 8
        blocks(x) = 0
    Next

    tl.Text = ""
    tr.Text = ""
    tc.Text = ""

    ml.Text = ""
    mc.Text = ""
    mr.Text = ""

    bl.Text = ""
    bc.Text = ""
    br.Text = ""
End Sub

Public Sub place(ByVal x As Integer)
    If blocks(x) = 0 Then
        Button1.Visible = False
        blocks(x) = 1
        display()
        If Not checkBoard() Then
            logic()
            display()
            checkBoard()
        End If
    End If
End Sub

Public Function checkBoard()
    If Not check() = 0 Then
        If check() = 1 Then
            MsgBox("Human wins!")
        Else
            MsgBox("Computer wins!")
        End If
        reboot()
        Return True
    Else
        For y = 0 To 8
            If blocks(y) = 0 Then
                Exit For
            End If
            If y = 8 Then
                MsgBox("It's a tie!")
                reboot()
                Return True
            End If
        Next
    End If
    Return False
End Function


Public Sub logic()
    Dim playercount As Integer = 0, computercount As Integer = 0
    If Not tryWin() Then
        If Not blockWin() Then
            If Not checkCorners() Then
                For x = 0 To 8
                    If blocks(x) = 1 Then
                        playercount += 1
                    End If
                    If blocks(x) = 2 Then
                        computercount += 1
                    End If
                Next
                If playercount = 1 And computercount = 0 Then
                    If difficulty > 1 Then
                        If blocks(4) = 0 Then
                            blocks(4) = 2
                        Else
                            blocks(0) = 2
                        End If
                    Else
                        randomBlock()
                    End If

                ElseIf playercount = 2 And computercount = 1 Then
                    If blocks(4) = 1 Then
                        If difficulty > 1 Then
                            choseCorner()
                        Else
                            randomBlock()
                        End If

                    Else
                        If Not choseCornerTouchingX() Then
                            For y = 1 To 8 Step 2
                                If blocks(y) = 0 Then
                                    blocks(y) = 2
                                    Exit For
                                End If
                            Next
                        End If
                    End If
                    Else
                        choseCorner()
                    End If
            End If
        End If
    End If
End Sub

Public Sub randomBlock()
    For x = 0 To 8
        If blocks(x) = 0 Then
            blocks(x) = 2
            Exit For
        End If
    Next
End Sub
Public Function choseCornerTouchingX()
    If difficulty > 2 Then
        If blocks(0) = 0 And (blocks(1) = 1 Or blocks(3) = 1) Then
            blocks(0) = 2
        ElseIf blocks(2) = 0 And (blocks(1) = 1 Or blocks(5) = 1) Then
            blocks(2) = 2
        ElseIf blocks(6) = 0 And (blocks(3) = 1 Or blocks(7) = 1) Then
            blocks(6) = 2
        ElseIf blocks(8) = 0 And (blocks(7) = 1 Or blocks(5) = 1) Then
            blocks(8) = 2
        Else
            Return False
        End If
        Return True
    Else
        Return False
    End If
End Function
Public Sub choseCorner()
    If Not checkCorners() Then
        If blocks(0) = 0 Then
            blocks(0) = 2
        ElseIf blocks(2) = 0 Then
            blocks(2) = 2
        ElseIf blocks(6) = 0 Then
            blocks(6) = 2
        ElseIf blocks(8) = 0 Then
            blocks(8) = 2
        Else
            randomBlock()
        End If
    End If
End Sub

Public Function checkCorners()
    If difficulty > 2 Then
        If blocks(1) = 1 And blocks(3) = 1 And blocks(0) = 0 Then
            blocks(0) = 2
        ElseIf blocks(1) = 1 And blocks(5) = 1 And blocks(2) = 0 Then
            blocks(2) = 2
        ElseIf blocks(3) = 1 And blocks(7) = 1 And blocks(6) = 0 Then
            blocks(6) = 2
        ElseIf blocks(5) = 1 And blocks(7) = 1 And blocks(8) = 0 Then
            blocks(8) = 2
        Else
            Return False
        End If
        Return True
    Else
        Return False
    End If
End Function

Public Function blockWin()
    If difficulty > 0 Then
        For x = 0 To 8
            If blocks(x) = 0 Then
                blocks(x) = 1
                If check() = 1 Then
                    blocks(x) = 2
                    Return True
                Else
                    blocks(x) = 0
                End If
            End If
        Next
    End If
    Return False
End Function

Public Function tryWin()
    If difficulty > 0 Then
        For x = 0 To 8
            If blocks(x) = 0 Then
                blocks(x) = 2
                If check() = 2 Then
                    blocks(x) = 2
                    Return True
                Else
                    blocks(x) = 0
                End If
            End If
        Next
    End If
    Return False
End Function

Public Function check()
    Dim x, y, z As Integer
    'check if either player won
    For x = 0 To 8 Step 1
        For y = 0 To 8
            For z = 0 To 8
                If x = 0 Then 'check for combos from top left
                    If y = 1 Then
                        If z = 2 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    ElseIf y = 3 Then
                        If z = 6 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    ElseIf y = 4 Then
                        If z = 8 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    End If
                ElseIf x = 1 Then 'check for combos from top center
                    If y = 4 Then
                        If z = 7 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    End If
                ElseIf x = 2 Then 'check for combos from top right
                    If y = 1 Then
                        If z = 0 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    ElseIf y = 4 Then
                        If z = 6 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    ElseIf y = 5 Then
                        If z = 8 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    End If
                ElseIf x = 3 Then 'check for combos from middle left
                    If y = 4 Then
                        If z = 5 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    End If
                ElseIf x = 6 Then 'check for combos from bottom left
                    If y = 7 Then
                        If z = 8 Then
                            If blocks(x) = blocks(y) Then
                                If blocks(x) = blocks(z) Then
                                    If Not blocks(x) = 0 Then
                                        Return blocks(x)
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            Next
        Next
    Next
    Return 0
End Function

Public Sub reboot()
    For x = 0 To 8
        blocks(x) = 0
    Next
    display()
    Button1.Visible = True
End Sub

Private Sub tl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tl.Click
    place(0)
End Sub

Private Sub tc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tc.Click
    place(1)
End Sub

Private Sub tr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tr.Click
    place(2)
End Sub

Private Sub ml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ml.Click
    place(3)
End Sub

Private Sub mc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mc.Click
    place(4)
End Sub

Private Sub mr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mr.Click
    place(5)
End Sub

Private Sub bl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bl.Click
    place(6)
End Sub

Private Sub bc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bc.Click
    place(7)
End Sub

Private Sub br_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles br.Click
    place(8)
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Visible = False
    logic()
    display()
End Sub

Private Sub btnEasy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEasy.Click
    btnImpossible.BackColor = Color.Green
    btnHard.BackColor = Color.Green
    btnMedium.BackColor = Color.Green
    btnEasy.BackColor = Color.BlueViolet
    difficulty = 0
End Sub

Private Sub btnMedium_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMedium.Click
    btnImpossible.BackColor = Color.Green
    btnHard.BackColor = Color.Green
    btnMedium.BackColor = Color.BlueViolet
    btnEasy.BackColor = Color.Green
    difficulty = 1
End Sub

Private Sub btnHard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHard.Click
    btnImpossible.BackColor = Color.Green
    btnHard.BackColor = Color.BlueViolet
    btnMedium.BackColor = Color.Green
    btnEasy.BackColor = Color.Green
    difficulty = 2
End Sub

Private Sub btnImpossible_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImpossible.Click
    btnImpossible.BackColor = Color.BlueViolet
    btnHard.BackColor = Color.Green
    btnMedium.BackColor = Color.Green
    btnEasy.BackColor = Color.Green
    difficulty = 3
End Sub

End Class
[/code]

It’s not nearly as commented as it could be, and it’s quite messy (hey, I said I did it in an hour :stuck_out_tongue: ) but it works, so yeah.

Also, if anyone can beat ‘impossible’ in a legitimate way, tell me how you did it and you’ll get a surprise =]

what about loops for thing like this:

[code] Public Sub display()
'Top Left
If blocks(0) = 0 Then
tl.Text = “”
ElseIf blocks(0) = 1 Then
tl.Text = “X”
ElseIf blocks(0) = 2 Then
tl.Text = “O”
End If

    'Top Center
    If blocks(1) = 0 Then
        tc.Text = ""
    ElseIf blocks(1) = 1 Then
        tc.Text = "X"
    ElseIf blocks(1) = 2 Then
        tc.Text = "O"
    End If

    'Top Right
    If blocks(2) = 0 Then
        tr.Text = ""
    ElseIf blocks(2) = 1 Then
        tr.Text = "X"
    ElseIf blocks(2) = 2 Then
        tr.Text = "O"
    End If


    'Middle Left
    If blocks(3) = 0 Then
        ml.Text = ""
    ElseIf blocks(3) = 1 Then
        ml.Text = "X"
    ElseIf blocks(3) = 2 Then
        ml.Text = "O"
    End If

    'Middle Center
    If blocks(4) = 0 Then
        mc.Text = ""
    ElseIf blocks(4) = 1 Then
        mc.Text = "X"
    ElseIf blocks(4) = 2 Then
        mc.Text = "O"
    End If

    'Middle Right
    If blocks(5) = 0 Then
        mr.Text = ""
    ElseIf blocks(5) = 1 Then
        mr.Text = "X"
    ElseIf blocks(5) = 2 Then
        mr.Text = "O"
    End If



    'Bottom Left
    If blocks(6) = 0 Then
        bl.Text = ""
    ElseIf blocks(6) = 1 Then
        bl.Text = "X"
    ElseIf blocks(6) = 2 Then
        bl.Text = "O"
    End If

    'Bottom Center
    If blocks(7) = 0 Then
        bc.Text = ""
    ElseIf blocks(7) = 1 Then
        bc.Text = "X"
    ElseIf blocks(7) = 2 Then
        bc.Text = "O"
    End If

    'Bottom Right
    If blocks(8) = 0 Then
        br.Text = ""
    ElseIf blocks(8) = 1 Then
        br.Text = "X"
    ElseIf blocks(8) = 2 Then
        br.Text = "O"
    End If
End Sub

[/code]

isn’t there something like “for(i=…”
so you could do this that way:

for(i=0;i<8;i++)
{
        If blocks(i) = 0 Then
            br.Text = ""
        ElseIf blocks(i) = 1 Then
            br.Text = "X"
        ElseIf blocks(i) = 2 Then
            br.Text = "O"
        End If
}

I would, except specific button names are used in each of those iterations, and cannot be changed per loop iteration.

Computer goes first, I hit left middle, top middle, middle middle, and then bottom middle. It ends up like

OXO
XXO
OX-

Oh my, you have defeated me! Ok well since you’re the first person to defeat me I’ll send you 50 cents in paypal (message me) after I update it to v2 then the person to beat that will get 1 dollar =]

50 cents! woohoo. He must feel awesome that he made that much money.

I think it will cost more money sending it to him than what you are giving him. lol.

=D maybe I’m a secret employee of paypal :3

Founded in 2004, Leakfree.org became one of the first online communities dedicated to Valve’s Source engine development. It is more famously known for the formation of Black Mesa: Source under the 'Leakfree Modification Team' handle in September 2004.